finished reset password & other changes
This commit is contained in:
@@ -43,12 +43,12 @@ export const login = async (values: zInfer<typeof LoginSchema>) => {
|
||||
return { error: 'common.something_went_wrong' }
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: logging must be implemented
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export const SignInProvider = async (provider: 'google' | 'github' | 'facebook') => {
|
||||
export const SignInProvider = async (provider: 'google' | 'github') => {
|
||||
await signIn(provider, {
|
||||
redirectTo: DEFAULT_LOGIN_REDIRECT,
|
||||
})
|
||||
|
||||
66
actions/new-password.ts
Normal file
66
actions/new-password.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
'use server'
|
||||
|
||||
import { NewPasswordSchema } from '@/schemas'
|
||||
import { infer as zInfer } from 'zod'
|
||||
import bcrypt from 'bcryptjs'
|
||||
import { PASSWORD_SALT_LENGTH } from '@/config/validation'
|
||||
|
||||
import { getPasswordResetTokenByToken } from '@/data/password-reset-token'
|
||||
import { getUserByEmail } from '@/data/user'
|
||||
import db from '@/lib/db'
|
||||
|
||||
export const newPassword = async (values: zInfer<typeof NewPasswordSchema>, token?: string | null) => {
|
||||
if (!token) {
|
||||
return { error: 'auth.form.error.missing_token' }
|
||||
}
|
||||
|
||||
const validatedFields = NewPasswordSchema.safeParse(values)
|
||||
|
||||
if (!validatedFields.success) {
|
||||
return { error: 'auth.form.error.invalid_fields' }
|
||||
}
|
||||
|
||||
const existingToken = await getPasswordResetTokenByToken(token)
|
||||
|
||||
if (!existingToken) {
|
||||
return { error: 'auth.form.error.invalid_token' }
|
||||
}
|
||||
|
||||
const hasExpired = new Date(existingToken.expires) < new Date()
|
||||
|
||||
if (hasExpired) {
|
||||
return { error: 'auth.form.error.expired_token' }
|
||||
}
|
||||
|
||||
const existingUser = await getUserByEmail(existingToken.email)
|
||||
|
||||
if (!existingUser) {
|
||||
return { error: 'auth.form.error.invalid_email' }
|
||||
}
|
||||
|
||||
const { password } = validatedFields.data
|
||||
const hashedPassword = await bcrypt.hash(password, PASSWORD_SALT_LENGTH)
|
||||
|
||||
try {
|
||||
await db.user.update({
|
||||
where: { id: existingUser.id },
|
||||
data: { password: hashedPassword },
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return { error: 'db.error.update.user_password' }
|
||||
}
|
||||
|
||||
try {
|
||||
await db.passwordResetToken.delete({
|
||||
where: { id: existingToken.id },
|
||||
})
|
||||
|
||||
return { success: 'db.success.update.password_updated' }
|
||||
} catch (err) {
|
||||
//TODO: Implement logging
|
||||
console.error(err)
|
||||
}
|
||||
|
||||
return { error: 'db.error.common.something_wrong' }
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
'use server'
|
||||
|
||||
import { RegisterSchema } from '@/schemas'
|
||||
import { infer as zInfer } from 'zod'
|
||||
import bcrypt from 'bcryptjs'
|
||||
|
||||
import { RegisterSchema } from '@/schemas'
|
||||
import { PASSWORD_SALT_LENGTH } from '@/config/validation'
|
||||
import db from '@/lib/db'
|
||||
import { getUserByEmail } from '@/data/user'
|
||||
|
||||
24
actions/reset.ts
Normal file
24
actions/reset.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
'use server'
|
||||
|
||||
import { infer as zInfer } from 'zod'
|
||||
import { ResetSchema } from '@/schemas'
|
||||
import { getUserByEmail } from '@/data/user'
|
||||
import { sendPasswordResetEmail } from '@/actions/send-verification-email'
|
||||
|
||||
export const reset = async (values: zInfer<typeof ResetSchema>) => {
|
||||
const validatedFields = ResetSchema.safeParse(values)
|
||||
|
||||
if (!validatedFields.success) {
|
||||
return { error: 'auth.form.error.invalid_fields' }
|
||||
}
|
||||
|
||||
const { email } = validatedFields.data
|
||||
|
||||
const existingUser = await getUserByEmail(email)
|
||||
|
||||
if (!existingUser) {
|
||||
return { error: 'auth.email.success.reset_email_sent' }
|
||||
}
|
||||
|
||||
return await sendPasswordResetEmail(existingUser.email as string, existingUser.name)
|
||||
}
|
||||
@@ -1,18 +1,28 @@
|
||||
'use server'
|
||||
|
||||
import mailer from '@/lib/mailer'
|
||||
import { AUTH_USER_VERIFICATION_URL } from '@/config/routes'
|
||||
import { generateVerificationToken } from '@/lib/tokens'
|
||||
import { AUTH_NEW_PASSWORD_URL, AUTH_USER_VERIFICATION_URL } from '@/config/routes'
|
||||
import { generatePasswordResetToken, generateVerificationToken } from '@/lib/tokens'
|
||||
import { env } from '@/lib/utils'
|
||||
import { __ct } from '@/lib/translate'
|
||||
import { body } from '@/templates/email/send-verification-email'
|
||||
|
||||
const sendVerificationEmail = async (email: string, name?: string | null) => {
|
||||
const sendVerificationEmail = async (
|
||||
email: string,
|
||||
name?: string | null,
|
||||
) => {
|
||||
const verificationToken = await generateVerificationToken(email)
|
||||
const confirmLink: string = [env('SITE_URL'), AUTH_USER_VERIFICATION_URL, verificationToken.token].join('')
|
||||
const confirmLink: string = [env('SITE_URL'), AUTH_USER_VERIFICATION_URL, '/', verificationToken.token].join('')
|
||||
const message = (await body({ confirmLink }))
|
||||
|
||||
const { isOk, code, info, error } = await mailer({
|
||||
to: name ? { name: name?.toString(), address: verificationToken.email } : verificationToken.email,
|
||||
subject: 'Complete email verification for A-Naklejka',
|
||||
html: `<p>Click <a href="${confirmLink}">here</a> to confirm email</p>`,
|
||||
subject: await __ct({
|
||||
key: 'mailer.subject.send_verification_email',
|
||||
params: { site_name: env('SITE_NAME') },
|
||||
}),
|
||||
text: message?.text,
|
||||
html: message?.html,
|
||||
})
|
||||
|
||||
if (isOk) {
|
||||
@@ -22,4 +32,24 @@ const sendVerificationEmail = async (email: string, name?: string | null) => {
|
||||
}
|
||||
}
|
||||
|
||||
export { sendVerificationEmail }
|
||||
const sendPasswordResetEmail = async (
|
||||
email: string,
|
||||
name?: string | null,
|
||||
) => {
|
||||
const resetToken = await generatePasswordResetToken(email)
|
||||
const resetLink: string = [env('SITE_URL'), AUTH_NEW_PASSWORD_URL, '/', resetToken.token].join('')
|
||||
|
||||
const { isOk, code, info, error } = await mailer({
|
||||
to: name ? { name: name?.toString(), address: resetToken.email } : resetToken.email,
|
||||
subject: 'Reset your password at A-Naklejka',
|
||||
html: `<p>Click <a href="${resetLink}">here</a> to reset password</p>`,
|
||||
})
|
||||
|
||||
if (isOk) {
|
||||
return { success: code === 250 ? 'auth.email.success.reset_email_sent' : info?.response }
|
||||
} else {
|
||||
return { error: env('DEBUG') === 'true' ? error?.response : 'auth.email.error.reset_password_sending_error' }
|
||||
}
|
||||
}
|
||||
|
||||
export { sendVerificationEmail, sendPasswordResetEmail }
|
||||
@@ -25,7 +25,7 @@ export const userVerification = async (token: string) => {
|
||||
})
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
return { error: 'Could not update user data! Please, try again by reloading page!' }
|
||||
return { error: 'db.error.update.user_data' }
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user