Authentication plugin for Fastify, powered by Auth.js.
npm install @auth/core fastify-next-auth
import fastify from 'fastify'
import AppleProvider from '@auth/core/providers/apple'
import GoogleProvider from '@auth/core/providers/google'
import AuthPlugin from 'fastify-next-auth'
const app = fastify()
app
.register(AuthPlugin, {
secret: process.env.AUTH_SECRET,
trustHost: process.env.AUTH_TRUST_HOST,
providers: [
AppleProvider({
clientId: process.env.APPLE_ID,
clientSecret: process.env.APPLE_SECRET,
}),
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
}),
],
})
Client Side Functions
import { signIn, signOut } from 'fastify-next-auth/client'
// Redirects to sign in page
signIn()
// Starts OAuth sign-in flow
signIn('google')
// Starts Email sign-in flow
signIn('email', { email: 'hello@mail.com' })
signOut()
Decorators
fastify.get('/api/user', async (req) => {
const { user } = await this.getSession(req)
return user
})
For more info, proceed to the Auth.js docs.
MIT