2021-03-24 15:03:04 +00:00
|
|
|
import NextAuth from 'next-auth';
|
|
|
|
import Providers from 'next-auth/providers';
|
|
|
|
import prisma from '../../../lib/prisma';
|
|
|
|
import {verifyPassword} from "../../../lib/auth";
|
|
|
|
|
|
|
|
export default NextAuth({
|
|
|
|
session: {
|
|
|
|
jwt: true
|
|
|
|
},
|
2021-03-29 21:01:12 +00:00
|
|
|
pages: {
|
|
|
|
signIn: '/auth/login',
|
|
|
|
signOut: '/auth/logout',
|
|
|
|
error: '/auth/error', // Error code passed in query string as ?error=
|
|
|
|
},
|
2021-03-24 15:03:04 +00:00
|
|
|
providers: [
|
|
|
|
Providers.Credentials({
|
|
|
|
name: 'Calendso',
|
|
|
|
credentials: {
|
|
|
|
email: { label: "Email Address", type: "email", placeholder: "john.doe@example.com" },
|
|
|
|
password: { label: "Password", type: "password", placeholder: "Your super secure password" }
|
|
|
|
},
|
|
|
|
async authorize(credentials) {
|
|
|
|
const user = await prisma.user.findFirst({
|
|
|
|
where: {
|
|
|
|
email: credentials.email
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
throw new Error('No user found');
|
|
|
|
}
|
|
|
|
|
|
|
|
const isValid = await verifyPassword(credentials.password, user.password);
|
|
|
|
|
|
|
|
if (!isValid) {
|
|
|
|
throw new Error('Incorrect password');
|
|
|
|
}
|
|
|
|
|
2021-05-11 13:11:17 +00:00
|
|
|
return {id: user.id, username: user.username, email: user.email, name: user.name, image: user.avatar};
|
2021-03-24 15:03:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
],
|
2021-05-05 20:01:56 +00:00
|
|
|
callbacks: {
|
|
|
|
async jwt(token, user, account, profile, isNewUser) {
|
|
|
|
// Add username to the token right after signin
|
|
|
|
if (user?.username) {
|
2021-05-11 09:21:05 +00:00
|
|
|
token.id = user.id;
|
|
|
|
token.username = user.username;
|
2021-05-05 20:01:56 +00:00
|
|
|
}
|
|
|
|
return token;
|
|
|
|
},
|
|
|
|
async session(session, token) {
|
|
|
|
session.user = session.user || {}
|
2021-05-11 09:21:05 +00:00
|
|
|
session.user.id = token.id;
|
2021-05-05 20:01:56 +00:00
|
|
|
session.user.username = token.username;
|
|
|
|
return session;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|