mirror of
https://github.com/harivansh-afk/Saas-Teamspace.git
synced 2026-04-20 13:02:18 +00:00
changed a lot
This commit is contained in:
parent
ef9ccf22d3
commit
28901128ff
20 changed files with 1794 additions and 526 deletions
|
|
@ -6,6 +6,7 @@ import { AuthError } from 'next-auth'
|
|||
import { generateVerificationToken } from '@/lib/tokens'
|
||||
import { getUserByEmail } from '@/data/user'
|
||||
import { sendVerificationEmail } from '@/lib/mail'
|
||||
import bcrypt from 'bcryptjs'
|
||||
|
||||
export const login = async (values: z.infer<typeof LoginSchema>) => {
|
||||
// Validate fields
|
||||
|
|
@ -15,47 +16,41 @@ export const login = async (values: z.infer<typeof LoginSchema>) => {
|
|||
if (!validatedFields.success) {
|
||||
return { error: 'Invalid fields' }
|
||||
}
|
||||
// If fields are valid
|
||||
const { email, password } = validatedFields.data
|
||||
const exisitingUser = await getUserByEmail(email)
|
||||
|
||||
if (!exisitingUser || !exisitingUser.email || !exisitingUser.password) {
|
||||
return { error: 'Email does not exisit' }
|
||||
const { email, password } = validatedFields.data
|
||||
const existingUser = await getUserByEmail(email)
|
||||
|
||||
if (!existingUser || !existingUser.email || !existingUser.password) {
|
||||
return { error: 'Email does not exist' }
|
||||
}
|
||||
|
||||
if (!exisitingUser.emailVerified) {
|
||||
const verificationToken = await generateVerificationToken(
|
||||
exisitingUser.email
|
||||
)
|
||||
|
||||
// Check if email is verified first
|
||||
if (!existingUser.emailVerified) {
|
||||
const verificationToken = await generateVerificationToken(email)
|
||||
await sendVerificationEmail(
|
||||
verificationToken.email,
|
||||
verificationToken.token
|
||||
)
|
||||
return { error: 'Please verify your email to login. Verification email sent!' }
|
||||
}
|
||||
|
||||
return { success: 'Confirmation email sent!' }
|
||||
// Verify password
|
||||
const passwordsMatch = await bcrypt.compare(password, existingUser.password)
|
||||
if (!passwordsMatch) {
|
||||
return { error: 'Invalid credentials' }
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await signIn('credentials', {
|
||||
redirect: false,
|
||||
await signIn('credentials', {
|
||||
email,
|
||||
password
|
||||
password,
|
||||
redirect: false,
|
||||
})
|
||||
|
||||
if (result?.error) {
|
||||
return { error: result.error }
|
||||
}
|
||||
|
||||
return { success: 'Logged In!' }
|
||||
return { success: 'Logged in successfully!' }
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
switch (error.type) {
|
||||
case 'CredentialsSignin':
|
||||
return { error: 'Invalid credentials' }
|
||||
default:
|
||||
return { error: 'Something went wrong' }
|
||||
}
|
||||
return { error: 'Something went wrong' }
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,40 +2,47 @@
|
|||
|
||||
import { db } from '@/lib/db'
|
||||
import { getUserByEmail } from '@/data/user'
|
||||
|
||||
import { getVerificationTokenByToken } from '@/data/verification-token'
|
||||
|
||||
export const newVerification = async (token: string) => {
|
||||
// if no token, display message
|
||||
const exisitingToken = await getVerificationTokenByToken(token)
|
||||
try {
|
||||
const existingToken = await getVerificationTokenByToken(token)
|
||||
|
||||
if (!exisitingToken) {
|
||||
return { error: 'Token does not exisit!' }
|
||||
}
|
||||
// if token has expired, display message
|
||||
const hasExpired = new Date(exisitingToken.expires) < new Date()
|
||||
|
||||
if (hasExpired) {
|
||||
return { error: 'Token has expired!' }
|
||||
}
|
||||
// if user does not exist, display message
|
||||
const existingUser = await getUserByEmail(exisitingToken.email)
|
||||
|
||||
if (!existingUser) {
|
||||
return { error: 'User does not exisit!' }
|
||||
}
|
||||
// update email value when they verify
|
||||
await db.user.update({
|
||||
where: { id: existingUser.id },
|
||||
data: {
|
||||
emailVerified: new Date(),
|
||||
email: exisitingToken.email
|
||||
if (!existingToken) {
|
||||
return { error: 'Verification link is invalid!' }
|
||||
}
|
||||
})
|
||||
// delete token
|
||||
await db.verificationToken.delete({
|
||||
where: { id: exisitingToken.id }
|
||||
})
|
||||
|
||||
return { success: 'Email verified! Login to continue' }
|
||||
const hasExpired = new Date(existingToken.expires) < new Date()
|
||||
if (hasExpired) {
|
||||
return { error: 'Verification link has expired! Please request a new one.' }
|
||||
}
|
||||
|
||||
const existingUser = await getUserByEmail(existingToken.email)
|
||||
if (!existingUser) {
|
||||
return { error: 'Email not found! Please sign up first.' }
|
||||
}
|
||||
|
||||
// If already verified, just return success
|
||||
if (existingUser.emailVerified) {
|
||||
return { success: 'Email already verified! Please login.' }
|
||||
}
|
||||
|
||||
// Update user verification status
|
||||
await db.user.update({
|
||||
where: { id: existingUser.id },
|
||||
data: {
|
||||
emailVerified: new Date(),
|
||||
email: existingToken.email
|
||||
}
|
||||
})
|
||||
|
||||
// Delete the verification token
|
||||
await db.verificationToken.delete({
|
||||
where: { id: existingToken.id }
|
||||
})
|
||||
|
||||
return { success: 'Email verified successfully! You can now login.' }
|
||||
} catch (error) {
|
||||
return { error: 'Something went wrong! Please try again.' }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue