Use kv datastore for auth (#272)

This commit is contained in:
Jeremy 2024-03-19 04:37:30 +03:00 committed by GitHub
parent d741351e26
commit b6cab643ef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 143 additions and 173 deletions

View file

@ -1,14 +1,26 @@
'use server'
import { signIn } from '@/auth'
import { AuthResult } from '@/lib/types'
import { User } from '@/lib/types'
import { AuthError } from 'next-auth'
import { z } from 'zod'
import { kv } from '@vercel/kv'
import { ResultCode } from '@/lib/utils'
export async function getUser(email: string) {
const user = await kv.hgetall<User>(`user:${email}`)
return user
}
interface Result {
type: string
resultCode: ResultCode
}
export async function authenticate(
_prevState: AuthResult | undefined,
_prevState: Result | undefined,
formData: FormData
) {
): Promise<Result | undefined> {
try {
const email = formData.get('email')
const password = formData.get('password')
@ -27,24 +39,33 @@ export async function authenticate(
await signIn('credentials', {
email,
password,
redirectTo: '/'
redirect: false
})
return {
type: 'success',
resultCode: ResultCode.UserLoggedIn
}
} else {
return { type: 'error', message: 'Invalid credentials!' }
return {
type: 'error',
resultCode: ResultCode.InvalidCredentials
}
}
} catch (error) {
if (error instanceof AuthError) {
switch (error.type) {
case 'CredentialsSignin':
return { type: 'error', message: 'Invalid credentials!' }
return {
type: 'error',
resultCode: ResultCode.InvalidCredentials
}
default:
return {
type: 'error',
message: 'Something went wrong, please try again!'
resultCode: ResultCode.UnknownError
}
}
}
throw error
}
}