Add base admin interface

This commit is contained in:
Bailey Pumfleet committed 2021-03-24 15:03:04 +00:00
1 parent d769c3943c
commit 388ff394c7
25 files changed
+905 -24

No files matched your search

+38
View File
@@ -0,0 +1,38 @@
import NextAuth from 'next-auth';
import Providers from 'next-auth/providers';
import prisma from '../../../lib/prisma';
import {verifyPassword} from "../../../lib/auth";
export default NextAuth({
session: {
jwt: true
},
providers: [
Providers.Credentials({
name: 'Calendso',
credentials: {
email: { label: "Email Address", type: "email", placeholder: "[email protected]" },
password: { label: "Password", type: "password", placeholder: "Your super secure password" }
},
async authorize(credentials) {
const user = await prisma.user.findFirst({
where: {
email: credentials.email
}
});
if (!user) {
throw new Error('No user found');
}
const isValid = await verifyPassword(credentials.password, user.password);
if (!isValid) {
throw new Error('Incorrect password');
}
return {id: user.id, username: user.username, email: user.email, name: user.name};
}
})
],
});
+47
View File
@@ -0,0 +1,47 @@
import prisma from '../../../lib/prisma';
import { hashPassword } from "../../../lib/auth";
export default async function handler(req, res) {
if (req.method !== 'POST') {
return;
}
const data = req.body;
const { username, email, password } = data;
if (!email || !email.includes('@') || !password || password.trim().length < 7) {
res.status(422).json({message: 'Invalid input - password should be at least 7 characters long.'});
return;
}
const existingUser = await prisma.user.findFirst({
where: {
OR: [
{
username: username
},
{
email: email
}
]
}
});
if (existingUser) {
res.status(422).json({message: 'A user exists with that username or email address'});
return;
}
const hashedPassword = await hashPassword(password);
const user = await prisma.user.create({
data: {
username: username,
email: email,
password: hashedPassword
}
});
res.status(201).json({message: 'Created user'});
}