55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
import NextAuth from "next-auth";
|
|
import Credentials from "next-auth/providers/credentials";
|
|
import { authenticateUser } from "@/lib/users";
|
|
|
|
export const { handlers, signIn, signOut, auth } = NextAuth({
|
|
trustHost: true,
|
|
providers: [
|
|
Credentials({
|
|
credentials: {
|
|
email: { label: "E-posta", type: "email" },
|
|
password: { label: "Şifre", type: "password" },
|
|
},
|
|
async authorize(credentials) {
|
|
const email = credentials?.email as string | undefined;
|
|
const password = credentials?.password as string | undefined;
|
|
|
|
if (!email || !password) return null;
|
|
|
|
const user = await authenticateUser(email, password);
|
|
if (!user) return null;
|
|
|
|
return {
|
|
id: user.id,
|
|
name: user.name,
|
|
email: user.email,
|
|
role: user.role,
|
|
domains: user.domains,
|
|
};
|
|
},
|
|
}),
|
|
],
|
|
callbacks: {
|
|
async jwt({ token, user }) {
|
|
if (user) {
|
|
token.id = user.id;
|
|
token.role = (user as { role?: string }).role;
|
|
token.domains = (user as { domains?: string[] }).domains;
|
|
}
|
|
return token;
|
|
},
|
|
async session({ session, token }) {
|
|
session.user.id = token.id as string;
|
|
session.user.role = token.role as string;
|
|
session.user.domains = token.domains as string[];
|
|
return session;
|
|
},
|
|
},
|
|
pages: {
|
|
signIn: "/login",
|
|
},
|
|
session: {
|
|
strategy: "jwt",
|
|
},
|
|
});
|