A Fastify plugin for easily adding login feature via Auth0's Authorization Code Flow
MIT License
A Fastify plugin for easily adding login feature via Auth0's Authorization Code Flow.
[!NOTE] This demonstrates bare minimum implementation for session-based authentication with a external provider. Since its lack of capabilities, I'd like to recomment not to use for your production. Instead, consider using some other frameworks such as Auth.js or account-js.
First, you need to create a Auth0 application.
Confirm the Domain
, Client ID
, and Client Secret
then set you application's Login URL
, Allowed Callback URL
, and Allowd Web Origins
.
For example to set for https://localhost:3000
,
Then configure your Fastify app with this plugin and @fastify/cookie
like this:
import FastifyCookie from '@fastify/cookie';
import FastifyAuth0Login from 'fastify-auth0-login';
app.register(FastifyCookie, {
secret: COOKIE_SECRET,
});
app.register(FastifyAuth0Login, {
auth0: {
domin: YOUR_AUTH0_DOMAIN,
clientId: YOUR_AUTH0_CLIENT_ID,
clientSecret: YOUR_AUTH0_CLIENT_SECRET,
},
verifySession: (_req, sessionId) => {
return findSession(sessionId);
},
confirmSession: (_req, sessionId, idTokenClaims) => {
return findOrCreateSession(sessionId, idTokenClaims);
},
});
Now you can initiate auth flow by GET /auth/request
.
<!-- In your /login page HTML -->
<!-- This will redirect user to the Auth0 application's auth URL -->
<a href="/auth">Login with Auth0</a>
MIT