Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discord Authorization in react app not working with flask backend #62

Open
ItsAditya-xyz opened this issue May 29, 2022 · 0 comments
Open

Comments

@ItsAditya-xyz
Copy link

ItsAditya-xyz commented May 29, 2022

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.
image
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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant