mirror of
https://github.com/jorgev259/soc_site-astro.git
synced 2025-06-29 07:57:41 +00:00
Set up @Auth
This commit is contained in:
parent
87c08df02b
commit
7bf946deb2
3 changed files with 410 additions and 5 deletions
33
auth.config.ts
Normal file
33
auth.config.ts
Normal 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 }
|
||||
},
|
||||
}),
|
||||
],
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue