export const authOptions: NextAuthOptions = {
session: {
strategy: 'jwt',
},
secret: 'super-secret',
jwt: {
maxAge: 15 * 24 * 30 * 60, // 15 days
},
pages: {
signIn: '/',
newUser: '/sign-up',
},
// Include user.id on session
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.id = user.id
}
return session
},
jwt: async ({ token, user }) => {
if (user) {
token.id = user.id
token.email = user.email
}
return token
},
},
adapter: PrismaAdapter(prisma),
providers: [
Credentials({
name: 'Credentials',
credentials: {
email: { label: 'Email', type: 'text', placeholder: 'jsmith' },
password: { label: 'Password', type: 'password' },
},
async authorize(credentials, req) {
const creds = await loginSchema.parseAsync(credentials)
const user = await prisma.user.findUnique({
where: {
email: creds.email,
},
})
if (!user) {
throw new Error('User not found')
}
const isValidPassword = await verify(
creds.password,
user.password as string
)
if (!isValidPassword) {
throw new Error('Invalid password')
}
return {
id: user.id,
email: user.email,
}
},
}),
],
}
export default NextAuth(authOptions)
export const authOptions: NextAuthOptions = {
session: {
strategy: 'jwt',
},
secret: 'super-secret',
jwt: {
maxAge: 15 * 24 * 30 * 60, // 15 days
},
pages: {
signIn: '/',
newUser: '/sign-up',
},
// Include user.id on session
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.id = user.id
}
return session
},
jwt: async ({ token, user }) => {
if (user) {
token.id = user.id
token.email = user.email
}
return token
},
},
adapter: PrismaAdapter(prisma),
providers: [
Credentials({
name: 'Credentials',
credentials: {
email: { label: 'Email', type: 'text', placeholder: 'jsmith' },
password: { label: 'Password', type: 'password' },
},
async authorize(credentials, req) {
const creds = await loginSchema.parseAsync(credentials)
const user = await prisma.user.findUnique({
where: {
email: creds.email,
},
})
if (!user) {
throw new Error('User not found')
}
const isValidPassword = await verify(
creds.password,
user.password as string
)
if (!isValidPassword) {
throw new Error('Invalid password')
}
return {
id: user.id,
email: user.email,
}
},
}),
],
}
export default NextAuth(authOptions)