107 lines
3.9 KiB
TypeScript
107 lines
3.9 KiB
TypeScript
import NextAuth from "next-auth";
|
|
import Google from "next-auth/providers/google";
|
|
import Credentials from "next-auth/providers/credentials";
|
|
import { verifyPassword } from "@/app/lib/auth";
|
|
import { prisma } from "@/app/lib/prisma";
|
|
|
|
export const { handlers, signIn, signOut, auth } = NextAuth({ // serverside functions
|
|
providers: [
|
|
Google,
|
|
Credentials({ //
|
|
name: "Credentials",
|
|
credentials: {
|
|
email: { label: "Email", type: "email"},
|
|
password: { label: "Password", type: "password"},
|
|
},
|
|
async authorize(credentials) { // function to how to find the account and verify password
|
|
if (!credentials?.email || !credentials?.password) return null;
|
|
|
|
const user = await prisma.users.findUnique({ // finds user
|
|
where: { email: credentials.email as string},
|
|
});
|
|
|
|
if (!user || !user.passwordHash) return null; // checks it found a user with a password
|
|
|
|
const isValid = await verifyPassword(
|
|
credentials.password as string,
|
|
user.passwordHash,
|
|
);
|
|
|
|
if (!isValid) return null; // if input password doesnt match hashed password
|
|
|
|
return { id: user.id, email: user.email, name: user.name} // return obj containing user data, session stores data once authorize is succesful
|
|
},
|
|
|
|
}),
|
|
],
|
|
session: { strategy: "jwt"},
|
|
callbacks: {
|
|
authorized: async ({ auth }) => {
|
|
return !!auth
|
|
},
|
|
signIn: async ({ user, account, profile }) => { // when the above signIn function is called auth.js runs this function to determine if its an allowed signIn
|
|
// ^^ Auth.js passes in an object containing info about sign in attempt which we destructure
|
|
if (account?.provider !== "google"){ // only apply to google sign ins
|
|
return true;
|
|
}
|
|
|
|
if (!user.email || profile?.email_verified !== true) { // the object gives us access to information such as whether their google email is verified
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
const existingGoogleUser = await prisma.users.findUnique({
|
|
where: {
|
|
googleId: account.providerAccountId, // also gives access to their unique google id which doesnt change - unlike an email which can
|
|
},
|
|
});
|
|
|
|
if (existingGoogleUser) {
|
|
return true;
|
|
}
|
|
|
|
const existingEmailUser = await prisma.users.findUnique({
|
|
where: {
|
|
email: user.email,
|
|
},
|
|
});
|
|
|
|
if (existingEmailUser) {
|
|
return false;
|
|
}
|
|
|
|
await prisma.users.create({
|
|
data: {
|
|
email: user.email,
|
|
name: user.name,
|
|
googleId: account.providerAccountId,
|
|
},
|
|
});
|
|
|
|
return true;
|
|
} catch (error) {
|
|
console.error("Google account creation failed:", error);
|
|
return false;
|
|
}
|
|
},
|
|
session: async ({ session }) => { // when session is access this function is called
|
|
|
|
if (!session.user?.email) return session; // if no email then no id
|
|
|
|
const user = await prisma.users.findUnique({
|
|
where: {
|
|
email: session.user.email,
|
|
},
|
|
});
|
|
|
|
if (user) { session.user.id = user.id;} // append user id and role to session
|
|
|
|
return session;
|
|
}
|
|
|
|
},
|
|
})
|
|
|
|
// exports functions from NextAuth to handle google login/out and check session
|
|
// you give next configs and it returns an object of functions we grab from it and export them for use
|