54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
|
import { NextAuthOptions } from "next-auth";
|
||
|
import CredentialsProvider from "next-auth/providers/credentials";
|
||
|
|
||
|
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;
|
||
|
const adminPassword = process.env.ADMIN_PASSWORD;
|
||
|
|
||
|
if (
|
||
|
credentials.username === adminUsername &&
|
||
|
credentials.password === adminPassword
|
||
|
) {
|
||
|
// Any object returned will be saved in `user` property of the JWT
|
||
|
return { id: 1, name: "svrjsAdmin" };
|
||
|
} else {
|
||
|
// If you return null then an error will be displayed advising the user to check their details.
|
||
|
return null;
|
||
|
}
|
||
|
},
|
||
|
}),
|
||
|
],
|
||
|
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
|
||
|
// session.user.id = token.id;
|
||
|
// session.user.name = token.name;
|
||
|
return session;
|
||
|
},
|
||
|
},
|
||
|
pages: {
|
||
|
signIn: "/login",
|
||
|
},
|
||
|
session: {
|
||
|
strategy: "jwt",
|
||
|
},
|
||
|
secret: process.env.NEXTAUTH_SECRET,
|
||
|
};
|