OAuth login
Besides the local email/password login strategy, the module supports login with OAuth2 providers such as Google, and Github.
Important
- Please note that
redirect.callbackoption is required in order to enable OAuth. - Please note that
emailandnameinformation is required for registration, otherwise a not accessible error message will be returned.
Options
The module can accept multiple OAuth2 providers via oauth config option:
ts
export default defineNuxtConfig({
// ...
auth: {
oauth: {
"<provider>": {
clientId: "",
clientSecret: "",
scopes: "",
authorizeUrl: "",
tokenUrl: "",
userUrl: "",
customParams: {},
},
},
},
// ...
});To login with an OAuth2 provider the module implements this flow:
- Via
authorizeUrl: it requests an authorization code from the provider withscopeto get user info andstateto maintain the redirection path of the previously visited protected page. The provider handles user authentication and consent. - Via
tokenUrl: it requests an access token from the OAuth2 authorization server with the authorizationcodereturned earlier. - Via
userUrl: it requests user info with the access token returned earlier. Thescopeshould permit getting the usernameandemailfields. - The module checks if the user exists (stored in the database), if not it registers them.
- The module issues an access token and a refresh token for this new session. Note the tokens issued by the OAuth provider are omitted, they are only needed to get user info.
The redirect URI to be set on oauth configuration should be the following:
bash
{baseUrl}/api/auth/login/{provider}/callback