2024-06-26 21:47:18 +02:00
|
|
|
import { NextAuthOptions } from "next-auth";
|
|
|
|
import CredentialsProvider from "next-auth/providers/credentials";
|
2024-06-27 11:12:28 +02:00
|
|
|
import bcrypt from "bcrypt";
|
2024-06-26 21:47:18 +02:00
|
|
|
|
|
|
|
export const authOptions: NextAuthOptions = {
|
|
|
|
providers: [
|
|
|
|
CredentialsProvider({
|
|
|
|
id: "credentials",
|
|
|
|
name: "Credentials",
|
|
|
|
credentials: {
|
|
|
|
username: { label: "Username", type: "text" },
|
|
|
|
password: { label: "Password", type: "password" },
|
|
|
|
},
|
|
|
|
async authorize(credentials: any): Promise<any> {
|
|
|
|
const adminUsername = process.env.ADMIN_USERNAME;
|
2024-06-27 11:12:28 +02:00
|
|
|
const adminPasswordHash = process.env.ADMIN_PASSWORD;
|
2024-06-26 21:47:18 +02:00
|
|
|
|
2024-06-27 11:12:28 +02:00
|
|
|
console.log(adminUsername);
|
|
|
|
console.log(adminPasswordHash);
|
|
|
|
console.log(credentials.username);
|
|
|
|
console.log(credentials.password);
|
|
|
|
if (credentials.username == adminUsername) {
|
|
|
|
const isValidPassword = await bcrypt.compare(
|
|
|
|
credentials.password,
|
|
|
|
adminPasswordHash!
|
|
|
|
);
|
|
|
|
|
|
|
|
console.log(isValidPassword);
|
|
|
|
|
|
|
|
if (isValidPassword) {
|
|
|
|
// aany object returned will be saved in `user` property of the jwtt
|
|
|
|
return { id: 1, name: "svrjsAdmin" };
|
|
|
|
}
|
2024-06-26 21:47:18 +02:00
|
|
|
}
|
2024-06-27 11:12:28 +02:00
|
|
|
// If you return null then an error will be displayed that the user to check their details.
|
|
|
|
return null;
|
2024-06-26 21:47:18 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
callbacks: {
|
|
|
|
async jwt({ token, user }) {
|
|
|
|
// Add user info to token
|
|
|
|
if (user) {
|
|
|
|
token.id = user.id;
|
|
|
|
token.name = user.name;
|
|
|
|
}
|
|
|
|
return token;
|
|
|
|
},
|
|
|
|
async session({ session, token }) {
|
|
|
|
// Add token info to session
|
2024-06-27 11:12:28 +02:00
|
|
|
// session.user.id = token.id;
|
|
|
|
// session.user.name = token.name;
|
2024-06-26 21:47:18 +02:00
|
|
|
return session;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
pages: {
|
|
|
|
signIn: "/login",
|
|
|
|
},
|
|
|
|
session: {
|
|
|
|
strategy: "jwt",
|
|
|
|
},
|
|
|
|
secret: process.env.NEXTAUTH_SECRET,
|
|
|
|
};
|