Created
February 11, 2024 17:22
-
-
Save jarrodmedrano/31f0b37d3d501686e088ac6936c332c0 to your computer and use it in GitHub Desktop.
Email & provider auth for next 13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { User as DefaultUser } from '@auth/core/types' | |
import { AdapterUser as DefaultAdapterUser } from '@auth/core/adapters' | |
import Google from 'next-auth/providers/google' | |
import Facebook from 'next-auth/providers/facebook' | |
import Apple from 'next-auth/providers/apple' | |
import EmailProvider, { EmailConfig } from 'next-auth/providers/email' | |
import NextAuth from 'next-auth' | |
import type { NextAuthConfig } from 'next-auth' | |
// import GitHub from 'next-auth/providers/github' | |
import { sendVerificationRequest } from './utils/sendVerificationRequest' | |
import PostgresAdapter from '@auth/pg-adapter' | |
import { Pool } from 'pg' | |
const pool = new Pool({ | |
host: process.env.DATABASE_HOST, | |
user: process.env.DATABASE_USER, | |
port: 5499, | |
password: process.env.DATABASE_SECRET, | |
max: 20, | |
idleTimeoutMillis: 30000, | |
connectionTimeoutMillis: 2000, | |
database: process.env.DATABASE_NAME, | |
}) | |
declare module 'next-auth' { | |
export interface User extends DefaultUser { | |
id: string | |
isAdmin: boolean | |
role: string | |
} | |
export interface Session { | |
user: User | |
} | |
export interface JWT { | |
user: { | |
id: string | |
role: string | |
} | |
} | |
} | |
declare module '@auth/core/adapters' { | |
//@ts-ignore this bullshit | |
export interface AdapterUser extends DefaultAdapterUser { | |
isAdmin: boolean | |
id: string | |
role: string | |
} | |
} | |
export const authConfig: NextAuthConfig = { | |
trustHost: true, // for build | |
debug: true, | |
adapter: PostgresAdapter(pool), | |
providers: [ | |
EmailProvider({ | |
server: { | |
host: process.env.EMAIL_SERVER_HOST, | |
port: process.env.EMAIL_SERVER_PORT as string, | |
auth: { | |
user: process.env.EMAIL_SERVER_USER, | |
pass: process.env.EMAIL_SERVER_PASSWORD, | |
}, | |
}, | |
from: process.env.EMAIL_FROM, | |
sendVerificationRequest, | |
}) as EmailConfig & { options: Record<string, unknown> }, | |
Apple, | |
Facebook, | |
// GitHub, | |
Google, | |
], | |
callbacks: { | |
async jwt({ token, user }) { | |
if (user) token.role = user.role | |
return token | |
}, | |
async session({ session, user }) { | |
if (session?.user) { | |
session.user.id = user.id | |
session.user.role = user.role | |
} | |
return session | |
}, | |
authorized(params) { | |
return !!params.auth?.user | |
}, | |
}, | |
pages: { | |
signIn: '/auth/signin', | |
// signOut: '/auth/signout', | |
// error: '/auth/error', | |
// verifyRequest: '/auth/verify-request', | |
}, | |
session: { | |
strategy: 'jwt', | |
}, | |
} satisfies NextAuthConfig | |
export const { handlers, auth, signIn, signOut } = NextAuth(authConfig) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment