svrjs-nextjs-website/app/api/auth/[...nextauth]/options.ts

64 lines
1.8 KiB
TypeScript
Raw Normal View History

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" }
2024-06-26 21:47:18 +02:00
},
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);
2024-09-08 17:29:45 +02:00
console.log("[password redacted]");
2024-06-27 11:12:28 +02:00
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;
}
2024-06-26 21:47:18 +02:00
},
pages: {
signIn: "/login"
2024-06-26 21:47:18 +02:00
},
session: {
strategy: "jwt"
2024-06-26 21:47:18 +02:00
},
secret: process.env.NEXTAUTH_SECRET
2024-06-26 21:47:18 +02:00
};