crybapp / api

Core service used to handle events from clients
MIT License
36 stars 8 forks source link

Discord auth returns 401 #3

Closed benclarkk closed 5 years ago

benclarkk commented 5 years ago

hi, when logging in with discord, the web app shows An error occured while authenticating. Please go home and Login with Discord again, and the api logs show POST /auth/discord 401 0.738 ms - 12. Console logs: console-export-2019-10-15_19-5-5.txt My web env: https://gist.github.com/benclarkk/0795888dd5361dee7e9913aeb8b34ada My API env: https://gist.github.com/benclarkk/ba602a40abf25e4dc7f28a481fee0532

benclarkk commented 5 years ago

idk if this is a good place to put this but

darnfish commented 5 years ago

Looks like your DISCORD_OAUTH_ORIGINS is incorrect. Try DISCORD_OAUTH_ORIGINS=http://ip:3000.

Let me know if this works or if you have any other issues!

benclarkk commented 5 years ago

I've changed it to DISCORD_OAUTH_ORIGINS=http://myip:3000, however it doesn't look like it's fixed the problem :(

benclarkk commented 5 years ago

bruhmoment here's my redirects on discord if that helps.

smekbo commented 5 years ago

For the life of me I can't figure out why this is happening (something to do with dotenv parsing, or something overwriting the variable? I couldn't find anything), but _process.env.DISCORD_OAUTHORIGINS keeps returning '1,2' for me, regardless of what I set in the .env file

api/.env vOclg8n api/src/controllers/auth.controller.ts 7OAZdgA output oKT3bJv

I fixed it for myself by just changing the name to 'DISCORD_OAUTH_ORIGIN' in the .env file and the auth controller, but that feels kinda kludgy.

darnfish commented 5 years ago

In your Discord Application config, the only Redirect URL you should have is http://localhost:3000/auth/discord if you're running locally. You shouldn't need DISCORD_OAUTH_ORIGIN, and DISCORD_OAUTH_ORIGINS should be set to http://localhost:3000.

Let me know if this helps!

smekbo commented 5 years ago

I'm running it remotely, and I think ben is too. I suspect his issue is the same one I was having.

DISCORD_OAUTH_ORIGINS isn't returning the URI from the .env file when auth.controller asks for it, which causes it to return 401 when the origins don't match.

http://localhost:3000 will work for running locally, but only because it gets pushed to the origins array in the auth controller https://github.com/crybapp/api/blob/master/src/controllers/auth.controller.ts#L12

darnfish commented 5 years ago

I'll try experiment with that to see if there is a better fix available

darnfish commented 5 years ago

Does this fix any issues? https://github.com/crybapp/api/commit/032557e649d61a5a694269930b3ffd30fc41d483 @akunim

smekbo commented 5 years ago

Still throws the 401 if I don't use a different env variable for setting origins

process.env.DISCORD_OAUTH_ORIGINS still seems to be acting strange for me

dumping process.env when api launches, it looks like it's getting set earlier than the rest of the values in the .env file, but I'm not sure the significance of that Capture (1)

.env file https://gist.github.com/akunim/76b33d93edcd2b30fe4c16ebe3c24757

benclarkk commented 5 years ago

032557e fixes the issue for me, so that's strange. but thanks for the fix @williamsthing.

JeDaYoshi commented 5 years ago

Fixed already. For support get to the Cryb Discord server.