tobier
tobier

Reputation: 676

How do I use Discord OAuth2 for user management and authentication in my application?

I'm building an application where I want to be able to create and authenticate users using Discord and OAuth2. The reasons are:

The application consists of a client desktop application and backend services. I have a fairly basic understanding on how I authorize the user with Discord:

  1. Client application goes to backend endpoint /oauth/login and the user is redirected to the Discord app approval page
  2. The user confirms and is redirected to the backend callback /oauth/callback with a code that can be used to fetch a pair of access and refresh tokens.

Frankly, from this point I am kind of stumped on how the rest of the authentication should work. I assume at least the following:

But now what? This only authenticates the user against Discord. I want to leverage the fact that the user is authenticated with Discord to be authenticated to my application. Here are some general questions I have:

This all feels like it should be very basic, but I am out of my comfort zone here and need some help to be unblocked.

Thanks for reading!

Upvotes: 1

Views: 3526

Answers (1)

Evert
Evert

Reputation: 99728

Your own application should effectively have its own session system.

The easiest is likely to just use HttpOnly cookie-based sessions, which something like a Redis store (or Memory store if this is a toy project).

The session data on the server should contain information on which user is currently logged in. You should probably store the discord access and refresh token in a database.

The simplest way to deal with refreshing, is to simply call their refresh token endpoint as soon as you get a 401 response. If discord provides information on how long access tokens are valid, you could also preemptively refresh instead of only doing this when you get the 401. Your server does the refreshing, you don't need an endpoint for this.

Generally I can recommend that your server handles all interactions with the discord API, and never your client. (aside from the initial authorization step).

Upvotes: 1

Related Questions