mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
import NextAuth from 'next-auth';
|
|
import Providers from 'next-auth/providers';
|
|
import bcrypt from 'bcryptjs';
|
|
import { query } from '../../../lib/db';
|
|
|
|
export default NextAuth({
|
|
providers: [
|
|
Providers.Credentials({
|
|
name: 'Credentials',
|
|
credentials: {
|
|
username: { label: 'Username', type: 'text' },
|
|
password: { label: 'Password', type: 'password' },
|
|
},
|
|
authorize: async (credentials) => {
|
|
const user = await query('SELECT * FROM users WHERE username = ?', [credentials.username]);
|
|
if (user && bcrypt.compareSync(credentials.password, user.password)) {
|
|
return { id: user.id, name: user.username, role: user.role };
|
|
}
|
|
return null;
|
|
},
|
|
}),
|
|
],
|
|
callbacks: {
|
|
jwt: async (token, user) => {
|
|
if (user) {
|
|
token.role = user.role;
|
|
}
|
|
return token;
|
|
},
|
|
session: async (session, token) => {
|
|
session.user.role = token.role;
|
|
return session;
|
|
},
|
|
},
|
|
pages: {
|
|
signIn: '/login', // Redirect to the login page
|
|
},
|
|
}); |