added tons of features

This commit is contained in:
2025-02-05 08:01:14 +02:00
parent 4ae0d8c545
commit 8138da6b1d
195 changed files with 12619 additions and 415 deletions

21
actions/auth/common.ts Normal file
View File

@@ -0,0 +1,21 @@
'use server'
import {getUserWithAccount} from '@prisma/client/sql'
import {getAccountByUserId} from '@/data/accout'
import {getUserById} from '@/data/user'
import {db} from '@/lib/db/prisma/client'
export const exisingUser = async (id: string | number) => {
return await getUserById(id)
}
export const exisingUserAccount = async (userId: string | number) => {
return await getAccountByUserId(userId)
}
export const getUserAccountByUserId = async (userId: string | number) => {
const user = await db.$queryRawTyped(getUserWithAccount(userId))
return user.length > 0 ? user[0] : null
}

View File

@@ -0,0 +1,16 @@
'use server'
import {AuthError} from 'next-auth'
import {signIn} from '@/auth'
export async function googleAuthenticate() {
try {
await signIn('google')
} catch (error) {
if (error instanceof AuthError) {
return 'google log in failed'
}
throw error
}
}

87
actions/auth/login.ts Normal file
View File

@@ -0,0 +1,87 @@
'use server'
import {User} from '@prisma/client'
import bcrypt from 'bcryptjs'
import {AuthError} from 'next-auth'
import {getTranslations} from 'next-intl/server'
import * as z from 'zod'
import {signIn} from '@/auth'
import {db} from '@/lib/db/prisma/client'
import {LoginSchema} from '@/lib/schemas'
export const login = async (data: z.infer<typeof LoginSchema>) => {
const t = await getTranslations('Auth')
// Validate the input data
const validatedData = LoginSchema.parse(data)
// If the data is invalid, return an error
if (!validatedData) {
return {error: t('Invalid input data')}
}
// Destructure the validated data
const {email, password} = validatedData
const userExists = await db.user.findFirst({
where: {email}
})
if (!userExists || !userExists.password || !userExists.email) {
return {error: t('Error.user-not-found')}
}
try {
await signIn('credentials', {
email: userExists.email as string,
password: password as string,
redirectTo: '/'
})
} catch (error) {
if (error instanceof AuthError) {
switch (error.type) {
case 'CredentialsSignin':
return {error: 'Invalid credentials'}
default:
return {error: error.type}
}
}
throw error
}
return {success: 'User successfully logged in!'}
}
export const authorizeCallback = async (
credentials: Partial<Record<'email' | 'password', unknown>>
): Promise<User | null> => {
const validatedData = z
.object({
email: z.string().email(),
password: z.string().min(6)
})
.safeParse(credentials)
if (!validatedData.success) return null
const {email, password} = validatedData.data
const user = await db.user.findFirst({
where: {email}
})
if (!user || !user.password || !user.email) return null
try {
if (await bcrypt.compare(password, user.password)) {
return user
} else {
console.log('Invalid credentials', user.email)
return null
}
} catch (err) {
console.log('Verifying password error', err)
}
return null
}

79
actions/auth/register.ts Normal file
View File

@@ -0,0 +1,79 @@
'use server'
import bcrypt from 'bcryptjs'
import * as z from 'zod'
import {db} from '@/lib/db/prisma/client'
import {RegisterSchema} from '@/lib/schemas'
import {hashPassword} from '@/lib/utils'
// import { generateVerificationToken } from "@/lib/token";
// import { sendVerificationEmail } from "@/lib/mail";
export const register = async (data: z.infer<typeof RegisterSchema>) => {
try {
// Validate the input data
const validatedData = RegisterSchema.parse(data)
// If the data is invalid, return an error
if (!validatedData) {
return {error: 'Invalid input data'}
}
// Destructure the validated data
const {email, name, password, passwordConfirmation} = validatedData
// Check if passwords match
if (password !== passwordConfirmation) {
return {error: 'Passwords do not match'}
}
// Hash the password
const hashedPassword = await hashPassword(password)
// Check to see if user already exists
const userExists = await db.user.findFirst({
where: {
email
}
})
// If the user exists, return an error
if (userExists) {
return {error: 'Email already is in use. Please try another one.'}
}
const lowerCaseEmail = email.toLowerCase()
// Create the user
const user = await db.user.create({
data: {
email: lowerCaseEmail,
name,
password: hashedPassword
}
})
// Generate Verification Token
// const verificationToken = await generateVerificationToken(email);
// await sendVerificationEmail(lowerCaseEmail, verificationToken.token);
return {success: 'Email Verification was sent'}
} catch (error) {
// Handle the error, specifically check for a 503 error
console.error('Database error:', error)
if ((error as {code: string}).code === 'ETIMEDOUT') {
return {
error: 'Unable to connect to the database. Please try again later.'
}
} else if ((error as {code: string}).code === '503') {
return {
error: 'Service temporarily unavailable. Please try again later.'
}
} else {
return {error: 'An unexpected error occurred. Please try again later.'}
}
}
}