|
1 | | -'use server'; |
| 1 | +'use server' |
2 | 2 |
|
3 | | -import crypto from 'crypto'; |
| 3 | +import crypto from 'crypto' |
4 | 4 |
|
5 | | -import sgMail from '@sendgrid/mail'; |
| 5 | +import sgMail from '@sendgrid/mail' |
6 | 6 |
|
| 7 | +import bcrypt from 'bcryptjs' |
7 | 8 |
|
8 | | -import bcrypt from 'bcryptjs'; |
9 | | - |
10 | | -import { prisma } from '@/prisma/client'; |
11 | | - |
12 | | - |
13 | | - |
14 | | -sgMail.setApiKey(process.env.SENDGRID_API_KEY!); |
| 9 | +import { prisma } from '@/prisma/client' |
15 | 10 |
|
| 11 | +sgMail.setApiKey(process.env.SENDGRID_API_KEY!) |
16 | 12 |
|
17 | 13 | export async function sendPasswordReset(email: string) { |
18 | 14 | try { |
19 | | - console.log('Checking for user with email:', email); |
| 15 | + console.log('Checking for user with email:', email) |
20 | 16 |
|
21 | | - const user = await prisma.user.findUnique({ where: { email } }); |
| 17 | + const user = await prisma.user.findUnique({ where: { email } }) |
22 | 18 |
|
23 | 19 | if (!user) { |
24 | | - console.warn('No user found with that email.'); |
| 20 | + console.warn('No user found with that email.') |
25 | 21 |
|
26 | | - return { success: false, error: 'No user found with that email.' }; |
| 22 | + return { success: false, error: 'No user found with that email.' } |
27 | 23 | } |
28 | 24 |
|
29 | | - const token = crypto.randomBytes(32).toString('hex'); |
30 | | - const hashedToken = crypto.createHash('sha256').update(token).digest('hex'); |
| 25 | + const token = crypto.randomBytes(32).toString('hex') |
| 26 | + const hashedToken = crypto.createHash('sha256').update(token).digest('hex') |
31 | 27 |
|
32 | 28 | try { |
33 | 29 | await prisma.user.update({ |
34 | 30 | where: { email }, |
35 | 31 | data: { |
36 | 32 | passwordResetToken: hashedToken, |
37 | | - passwordResetExpires: new Date(Date.now() + 1000 * 60 * 15), |
38 | | - }, |
39 | | - }); |
40 | | - |
| 33 | + passwordResetExpires: new Date(Date.now() + 1000 * 60 * 15) |
| 34 | + } |
| 35 | + }) |
41 | 36 | } catch (updateError: any) { |
42 | | - console.error('Prisma update failed:', updateError); |
43 | | - |
44 | | - return { success: false, error: 'Failed to update user in database.' }; |
| 37 | + console.error('Prisma update failed:', updateError) |
| 38 | + |
| 39 | + return { success: false, error: 'Failed to update user in database.' } |
45 | 40 | } |
46 | 41 |
|
47 | | - const resetLink = `https://team3docker.uksouth.cloudapp.azure.com//reset-password/${token}`; |
| 42 | + const resetLink = `${process.env.NEXT_PUBLIC_APP_URL}//reset-password/${token}` |
48 | 43 |
|
49 | 44 | const msg = { |
50 | 45 | to: email, |
51 | 46 | from: process.env.SENDGRID_SENDER_EMAIL!, |
52 | 47 | subject: 'Reset Your Password', |
53 | | - html: `<p>Click <a href="${resetLink}">here</a> to reset your password. This link is valid for 15 minutes.</p>`, |
54 | | - }; |
| 48 | + html: `<p>Click <a href="${resetLink}">here</a> to reset your password. This link is valid for 15 minutes.</p>` |
| 49 | + } |
55 | 50 |
|
56 | | - await sgMail.send(msg); |
57 | | - |
58 | | -return { success: true }; |
| 51 | + await sgMail.send(msg) |
| 52 | + |
| 53 | + return { success: true } |
59 | 54 | } catch (error: any) { |
60 | | - console.error('Error in sendPasswordReset:', error); |
61 | | - |
62 | | -return { success: false, error: error.message }; |
| 55 | + console.error('Error in sendPasswordReset:', error) |
| 56 | + |
| 57 | + return { success: false, error: error.message } |
63 | 58 | } |
64 | 59 | } |
65 | 60 |
|
66 | 61 | export async function checkValidityToken(token: string) { |
67 | 62 | // Hash the token |
68 | | - const hashedToken = crypto.createHash('sha256').update(token).digest('hex'); |
| 63 | + const hashedToken = crypto.createHash('sha256').update(token).digest('hex') |
69 | 64 |
|
70 | 65 | const user = await prisma.user.findFirst({ |
71 | 66 | where: { passwordResetToken: hashedToken }, |
72 | 67 | select: { passwordResetExpires: true } |
73 | | - }); |
74 | | - |
| 68 | + }) |
75 | 69 |
|
76 | 70 | // Check if the user exists and if the token has expired |
77 | 71 | if (!user || !user.passwordResetExpires) { |
78 | | - throw new Error('Invalid or expired token'); |
| 72 | + throw new Error('Invalid or expired token') |
79 | 73 | } |
80 | 74 |
|
81 | 75 | // Compare expiration date with current time |
82 | | - const currentTime = new Date().toISOString(); |
83 | | - const expirationTime = user.passwordResetExpires.toISOString(); |
84 | | - |
| 76 | + const currentTime = new Date().toISOString() |
| 77 | + const expirationTime = user.passwordResetExpires.toISOString() |
| 78 | + |
85 | 79 | if (currentTime > expirationTime) { |
86 | | - throw new Error('Token has expired'); |
| 80 | + throw new Error('Token has expired') |
87 | 81 | } |
88 | 82 |
|
89 | 83 | // If token is still valid, return true or user object |
90 | | - return true; |
| 84 | + return true |
91 | 85 | } |
92 | 86 |
|
93 | 87 | export async function handleResetPassword(token: string, newPassword: string) { |
94 | | - const hashedToken = crypto.createHash('sha256').update(token).digest('hex'); |
| 88 | + const hashedToken = crypto.createHash('sha256').update(token).digest('hex') |
95 | 89 |
|
96 | 90 | const user = await prisma.user.findFirst({ |
97 | 91 | where: { |
98 | | - passwordResetToken: hashedToken, |
99 | | - }, |
100 | | - }); |
| 92 | + passwordResetToken: hashedToken |
| 93 | + } |
| 94 | + }) |
101 | 95 |
|
102 | 96 | if (!user) { |
103 | | - return { success: false, error: 'Invalid or expired token.' }; |
| 97 | + return { success: false, error: 'Invalid or expired token.' } |
104 | 98 | } |
105 | 99 |
|
106 | | - const hashedPassword = await bcrypt.hash(newPassword, 10); |
| 100 | + const hashedPassword = await bcrypt.hash(newPassword, 10) |
107 | 101 |
|
108 | 102 | await prisma.user.update({ |
109 | 103 | where: { id: user.id }, |
110 | 104 | data: { |
111 | 105 | hashedPassword, |
112 | 106 | passwordResetToken: null, |
113 | | - passwordResetExpires: null, |
114 | | - }, |
115 | | - }); |
| 107 | + passwordResetExpires: null |
| 108 | + } |
| 109 | + }) |
116 | 110 |
|
117 | | - return { success: true }; |
| 111 | + return { success: true } |
118 | 112 | } |
0 commit comments