@ -1,47 +1,19 @@
|
||||
import NextAuth from "next-auth";
|
||||
import CredentialsProvider from "next-auth/providers/credentials";
|
||||
import { connectMongo } from "../../../utils/helpers/mongoHelpers";
|
||||
import {
|
||||
generateRandomPassword,
|
||||
isValidPassword,
|
||||
} from "../../../utils/helpers/passwordHelpers";
|
||||
import { createUser, getUser } from "../../../utils/helpers/userHelpers";
|
||||
|
||||
// Create admin account if one doesn't exist yet
|
||||
const pass = generateRandomPassword();
|
||||
createUser("admin", pass).then((returned) => {
|
||||
if (returned === true) {
|
||||
console.log(`Created admin account. Username: admin, Password: ${pass}`);
|
||||
}
|
||||
});
|
||||
|
||||
export const authOptions = {
|
||||
providers: [
|
||||
CredentialsProvider({
|
||||
name: "Credentials",
|
||||
credentials: {
|
||||
username: {
|
||||
label: "Username",
|
||||
type: "text",
|
||||
placeholder: "admin",
|
||||
},
|
||||
username: { label: "Username", type: "text", placeholder: "admin" },
|
||||
password: { label: "Password", type: "password" },
|
||||
},
|
||||
async authorize(credentials, req) {
|
||||
await connectMongo();
|
||||
const { username, password } = credentials;
|
||||
const user = await getUser(username);
|
||||
if (user == null) {
|
||||
return null;
|
||||
}
|
||||
console.log(credentials);
|
||||
const user = { id: "1", name: "J Smith", email: "admin@example.com" };
|
||||
|
||||
if (user) {
|
||||
const validPassword = isValidPassword(password, user.password);
|
||||
if (validPassword === false) {
|
||||
return null;
|
||||
}
|
||||
user.lastLoginDate = new Date();
|
||||
await user.save();
|
||||
return user;
|
||||
} else {
|
||||
return null;
|
||||
|
Reference in New Issue
Block a user