fastify-next-auth-test
v0.0.3
Published
NextAuth.js plugin for Fastify.
Downloads
6
Maintainers
Readme
fastify-next-auth
Authentication plugin for Fastify, powered by NextAuth.js.
Requirements:
- fastify-cookie: used to set cookie for tracking sessions.
- fastify-formbody: used to parse content-type
application/x-www-form-urlencoded
.
Installation
npm install next-auth fastify-next-auth
Usage
import fastify from 'fastify'
import cookie from '@fastify/cookie'
import formBody from '@fastify/formbody'
import AppleProvider from 'next-auth/providers/apple'
import GoogleProvider from 'next-auth/providers/google'
import EmailProvider from 'next-auth/providers/email'
import type { NextAuthOptions } from 'fastify-next-auth'
import NextAuth from 'fastify-next-auth'
const app = fastify()
app
.register(cookie)
.register(formBody)
.register(NextAuth, {
secret: process.env.NEXTAUTH_SECRET,
providers: [
// OAuth authentication providers
AppleProvider({
clientId: process.env.APPLE_ID,
clientSecret: process.env.APPLE_SECRET,
}),
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
}),
// Sign in with passwordless email link
EmailProvider({
server: process.env.MAIL_SERVER,
from: '<[email protected]>',
}),
],
} as NextAuthOptions)
Client API
The client library makes it easy to interact with sessions from your frontend.
Example Session Object
{
user: {
name: string
email: string
image: string
},
expires: Date // This is the expiry of the session, not any of the tokens within the session
}
Server Side Example
import {
getCsrfToken,
getProviders,
getSession
} from 'fastify-next-auth/client'
fastify.get('/api/info', async (req, reply) => {
const session = await getSession({ req })
const token = await getCsrfToken({ req })
// Unlike getSession() and getCsrfToken(), when calling getProviders() server side,
// you don't need to pass anything, just as calling it client side.
const providers = await getProviders()
return {
session,
providers,
token
}
})
Client Side Example
import {
getCsrfToken,
getProviders,
getSession,
signIn,
signOut
} from 'fastify-next-auth/client'
async function myFunction() {
const session = await getSession()
const providers = await getProviders()
const token = await getCsrfToken()
// Redirects to sign in page
signIn()
// Starts OAuth sign-in flow
signIn('google')
// Starts Email sign-in flow
signIn('email', { email: '[email protected]' })
signOut()
}
For more info on client side usage, proceed to the NextAuth.js Client API docs page.
License
MIT