cometkim / fastify-auth0-login

A Fastify plugin for easily adding login feature via Auth0's Authorization Code Flow

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fastify-auth0-login

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.

Prerequisites

Usage

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,

Example Callback URLs

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>

LICENSE

MIT

About

A Fastify plugin for easily adding login feature via Auth0's Authorization Code Flow

License:MIT License


Languages

Language:TypeScript 100.0%