Flask-Discord
Flask-Discord copied to clipboard
Discord Authorization in react app not working with flask backend
So I am trying to set up a flask backend through which i want to do discord authorization in my react app. my flask backed looks like this:
@application.route("/login/")
def login():
discordSession = discord.create_session(scope=["identify"])
return discordSession
# function to redirect to discord oauth page.
@application.route("/callback")
def callback():
try:
data = discord.callback()
redirect_to = data.get("redirect", REDIRECT_URL)
# user = discord.fetch_user()
print(redirect_to)
return redirect_to
except AccessDenied:
return redirect(url_for("index"))
@application.route("/discordInfo")
def discordInfo():
if not discord.authorized:
# return render_template('loginPage.html') # rendering index.html file
return jsonify({"isAuthorized": False})
user = discord.fetch_user()
av = user.avatar_url if user.avatar_url else user.default_avatar_url
userID = str(user.id)
encryptedID = encrypt_message(userID).decode()
data = {
"isAuthorized": True,
"username": user.name + "#" + user.discriminator,
"stringID": encryptedID,
"avatarUrl": av
}
return jsonify(data)
and the frontend code for login button is this:
<button
onClick={async () => {
window.location.replace("http://127.0.0.1:5000/login/");
}>
discord login
</button>
when i click the discord login button, the login session starts.
When i click authorize, I am redirected to the redirect URI I have set but that URL also has some extra queries like this
http://localhost:3000/?code=KqntCqGvR1kWRckoeNZiNoUePQqpvp&state=eyJ0eXipOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfX3N0YXRlX3NlY3JldF8iOiJodk1xcjuwc1dDVlZKMGIzUWdXNkExYTRUZ1I1NWgifQ.As-hU5qrnGZ-q4ghRi2JonTcuuJmg1uMIBcDYgI-zD8
After login, i want to hit /discordInfo
to get user information but it always returns {"isAuthorized": False}
Can anyone help me figuring out what I am doing wrong?