Add file model

This commit is contained in:
Liam
2022-11-13 22:45:01 +00:00
parent 66d4d68bb4
commit 772fe1574c
12 changed files with 314 additions and 23 deletions

View File

@ -1,19 +1,47 @@
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) {
console.log(credentials);
const user = { id: "1", name: "J Smith", email: "admin@example.com" };
await connectMongo();
const { username, password } = credentials;
const user = await getUser(username);
if (user == null) {
return null;
}
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;