Skip to content
Migrating from NextAuth.js v4? Read our migration guide.
Concepts
How OAuth works

OAuth

💡

Auth.js is designed to work with any OAuth service, it supports OAuth 2.0 and OpenID Connect and has built-in support for most popular sign-in services.

Authentication Providers in Auth.js are predefined OAuth configurations that allow your users to sign in with pre-existing logins at their favorite services. You can use any of our predefined providers, or write your own custom OAuth configuration. For customizing or writing your own OAuth provider, see our configuring OAuth providers guide.

At a high level, the OAuth Authorization Code flow we support generally has 6 parts:

  1. The application requests authorization to access service resources from the user
  2. If the user authorized the request, the application receives an authorization grant
  3. The application requests an access token from the authorization server (API) by presenting authentication of its own identity, and the authorization grant
  4. If the application identity is authenticated and the authorization grant is valid, the authorization server (API) issues an access token to the application. Authorization is complete.
  5. The application requests the resource from the resource server (API) and presents the access token for authentication
  6. If the access token is valid, the resource server (API) serves the resource to the application

Diagrams

Below are two diagrams visually illustrating the same basic flow as described above, the OAuth Authorization Code flow. First is a sequence diagram.

Auth Server (Github)App ServerBrowserAuth Server (Github)App ServerBrowserUser clicks on "Sign in"Sign in optionsare shown the user(Github, Twitter, etc...)User clicks on"Sign in with Github"Sign in options are supplied asquery params(clientId, scope, etc...)User inserts theircredentials in GithubGET"api/auth/signin"Computes the availablesign in providersfrom the "providers" optionRedirects to Sign in pagePOST"api/auth/signin/github"Computes sign inoptions for Github(scopes, callback URL, etc...)GET"github.com/login/oauth/authorize"Shows sign in pagein Github.comto the userGithub validates the inserted credentialsGenerates one time access codeand calls callbackURL defined inApp settingsGET"api/auth/github/callback?code=123"Grabs codeto exchange it foraccess tokenPOST"github.com/login/oauth/access_token"{code: 123}Verifies code isvalid and generatesaccess token{ access_token: 16C7x... }Generates session tokenand stores sessionYou're now logged in!

Next is a swim lane diagram which comes from a great article, Setting up OAuth with Auth.js and SvelteKit by Andrey Mikhaylov of mainmatter.com.

OAuth Flow Diagram

Further Reading

To learn more, check out the following blog posts:

Last updated on
Auth.js © Balázs Orbán and Team - 2025