Set up @Auth

This commit is contained in:
Jorge Vargas 2024-08-19 00:48:34 -06:00
parent 87c08df02b
commit 7bf946deb2
3 changed files with 410 additions and 5 deletions

33
auth.config.ts Normal file
View file

@ -0,0 +1,33 @@
import { CredentialsSignin } from '@auth/core/errors'
import { defineConfig } from 'auth-astro'
import Credentials from "next-auth/providers/credentials"
import bcrypt from 'bcrypt'
import db from '@/sequelize'
class InvalidLoginError extends CredentialsSignin {
code = "Invalid username/email or password"
}
export default defineConfig({
providers: [
// @ts-ignore @auth/core cringe
Credentials({
credentials: {
username: { label: "Username" },
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
if (!credentials?.username || !credentials.password) throw new InvalidLoginError()
const user = await db.models.user.findByPk(credentials.username)
if (!user) throw new InvalidLoginError()
const valid = await bcrypt.compare(credentials.password, user.password)
if (!valid) throw new InvalidLoginError()
return { id: user.username, username: user.username }
},
}),
],
})