mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
58 lines
1.7 KiB
JavaScript
58 lines
1.7 KiB
JavaScript
// app/api/auth/[...nextauth]/route.js
|
|
import NextAuth from 'next-auth';
|
|
import CredentialsProvider from 'next-auth/providers/credentials';
|
|
import { validateCredentials } from '../../../actions/auth';
|
|
|
|
export const authOptions = {
|
|
providers: [
|
|
CredentialsProvider({
|
|
name: 'Credentials',
|
|
credentials: {
|
|
username: { label: "Username", type: "text" },
|
|
password: { label: "Password", type: "password" }
|
|
},
|
|
async authorize(credentials) {
|
|
if (!credentials?.username || !credentials?.password) {
|
|
return null;
|
|
}
|
|
|
|
const user = await validateCredentials(
|
|
credentials.username,
|
|
credentials.password
|
|
);
|
|
|
|
return user;
|
|
}
|
|
})
|
|
],
|
|
callbacks: {
|
|
async jwt({ token, user }) {
|
|
if (user) {
|
|
token.id = user.id;
|
|
token.role = user.role;
|
|
token.username = user.username;
|
|
token.teamId = user.teamId;
|
|
}
|
|
return token;
|
|
},
|
|
async session({ session, token }) {
|
|
if (token) {
|
|
session.user = session.user || {};
|
|
session.user.id = token.id;
|
|
session.user.role = token.role;
|
|
session.user.username = token.username;
|
|
session.user.teamId = token.teamId;
|
|
}
|
|
return session;
|
|
}
|
|
},
|
|
pages: {
|
|
signIn: '/login',
|
|
},
|
|
session: {
|
|
strategy: "jwt",
|
|
}
|
|
};
|
|
|
|
const handler = NextAuth(authOptions);
|
|
export { handler as GET, handler as POST }; |