Skip to content

Commit 5903df0

Browse files
authored
Merge pull request #79 from UCLComputerScience/fix
fix: make the base url based on environmental variables
2 parents fdb6913 + 7253a58 commit 5903df0

2 files changed

Lines changed: 74 additions & 81 deletions

File tree

src/actions/email/sendInvite.ts

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
'use server';
1+
'use server'
22

3-
import sgMail from '@sendgrid/mail';
3+
import sgMail from '@sendgrid/mail'
44

55
import { prisma } from '@/prisma/client'
66

7-
87
export interface User {
98
firstName: string
109
lastName: string
@@ -15,17 +14,17 @@ const getPatientName = async (id: string): Promise<User | null> => {
1514
where: { id },
1615
select: {
1716
firstName: true,
18-
lastName: true,
17+
lastName: true
1918
}
2019
})
2120
}
2221

23-
sgMail.setApiKey(process.env.SENDGRID_API_KEY!);
22+
sgMail.setApiKey(process.env.SENDGRID_API_KEY!)
2423

2524
// Send invite email after login (with uid)
2625
export async function sendInviteEmail(name: string, email: string, patientId: string) {
27-
const patient = await getPatientName(patientId);
28-
const patientName = patient ? `${patient.firstName} ${patient.lastName}` : '';
26+
const patient = await getPatientName(patientId)
27+
const patientName = patient ? `${patient.firstName} ${patient.lastName}` : ''
2928

3029
try {
3130
const msg = {
@@ -36,45 +35,45 @@ export async function sendInviteEmail(name: string, email: string, patientId: st
3635
<p>Dear ${name},</p>
3736
<p>Your patient ${patientName} would like to share their symptoms data with you on our platform. </p>
3837
<p>Register your account to view their spidergrams and track their data.</p>
39-
<p>Here is a link to our website: https://team3docker.uksouth.cloudapp.azure.com/register</p>
38+
<p>Here is a link to our website: ${process.env.NEXT_PUBLIC_APP_URL}/register</p>
4039
<p>Kind regards,</p>
4140
<p>The Spider team</p>
42-
`,
43-
};
41+
`
42+
}
43+
44+
await sgMail.send(msg)
4445

45-
await sgMail.send(msg);
46-
47-
return { success: true };
46+
return { success: true }
4847
} catch (error: any) {
49-
console.error('[Invite Email Error]', error);
50-
51-
return { success: false, error: error.message };
48+
console.error('[Invite Email Error]', error)
49+
50+
return { success: false, error: error.message }
5251
}
5352
}
5453

5554
// Send invite email before login (without uid)
5655
export async function sendInviteEmailDuringRegistration(clinicianName: string, email: string) {
5756
try {
58-
const msg = {
59-
to: email,
60-
from: process.env.SENDGRID_SENDER_EMAIL!,
61-
subject: 'You\'ve been invited to join our platform!',
62-
html: `
57+
const msg = {
58+
to: email,
59+
from: process.env.SENDGRID_SENDER_EMAIL!,
60+
subject: "You've been invited to join our platform!",
61+
html: `
6362
<p>Dear ${clinicianName},</p>
6463
<p>A new patient would like to share their symptoms data with you on our platform.</p>
6564
<p>Register your account to view their spidergrams and track their data.</p>
6665
<p>Here is a link to our website: https://team3.uksouth.cloudapp.azure.com</p>
6766
<p>Kind regards,</p>
6867
<p>The Spider team</p>
69-
`,
70-
};
71-
72-
await sgMail.send(msg);
73-
74-
return { success: true };
68+
`
69+
}
70+
71+
await sgMail.send(msg)
72+
73+
return { success: true }
7574
} catch (error: any) {
76-
console.error('[Invite Email Error]', error);
77-
78-
return { success: false, error: error.message };
75+
console.error('[Invite Email Error]', error)
76+
77+
return { success: false, error: error.message }
7978
}
8079
}

src/actions/email/sendReset.ts

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,112 @@
1-
'use server';
1+
'use server'
22

3-
import crypto from 'crypto';
3+
import crypto from 'crypto'
44

5-
import sgMail from '@sendgrid/mail';
5+
import sgMail from '@sendgrid/mail'
66

7+
import bcrypt from 'bcryptjs'
78

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'
1510

11+
sgMail.setApiKey(process.env.SENDGRID_API_KEY!)
1612

1713
export async function sendPasswordReset(email: string) {
1814
try {
19-
console.log('Checking for user with email:', email);
15+
console.log('Checking for user with email:', email)
2016

21-
const user = await prisma.user.findUnique({ where: { email } });
17+
const user = await prisma.user.findUnique({ where: { email } })
2218

2319
if (!user) {
24-
console.warn('No user found with that email.');
20+
console.warn('No user found with that email.')
2521

26-
return { success: false, error: 'No user found with that email.' };
22+
return { success: false, error: 'No user found with that email.' }
2723
}
2824

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')
3127

3228
try {
3329
await prisma.user.update({
3430
where: { email },
3531
data: {
3632
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+
})
4136
} 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.' }
4540
}
4641

47-
const resetLink = `https://team3docker.uksouth.cloudapp.azure.com//reset-password/${token}`;
42+
const resetLink = `${process.env.NEXT_PUBLIC_APP_URL}//reset-password/${token}`
4843

4944
const msg = {
5045
to: email,
5146
from: process.env.SENDGRID_SENDER_EMAIL!,
5247
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+
}
5550

56-
await sgMail.send(msg);
57-
58-
return { success: true };
51+
await sgMail.send(msg)
52+
53+
return { success: true }
5954
} 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 }
6358
}
6459
}
6560

6661
export async function checkValidityToken(token: string) {
6762
// Hash the token
68-
const hashedToken = crypto.createHash('sha256').update(token).digest('hex');
63+
const hashedToken = crypto.createHash('sha256').update(token).digest('hex')
6964

7065
const user = await prisma.user.findFirst({
7166
where: { passwordResetToken: hashedToken },
7267
select: { passwordResetExpires: true }
73-
});
74-
68+
})
7569

7670
// Check if the user exists and if the token has expired
7771
if (!user || !user.passwordResetExpires) {
78-
throw new Error('Invalid or expired token');
72+
throw new Error('Invalid or expired token')
7973
}
8074

8175
// 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+
8579
if (currentTime > expirationTime) {
86-
throw new Error('Token has expired');
80+
throw new Error('Token has expired')
8781
}
8882

8983
// If token is still valid, return true or user object
90-
return true;
84+
return true
9185
}
9286

9387
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')
9589

9690
const user = await prisma.user.findFirst({
9791
where: {
98-
passwordResetToken: hashedToken,
99-
},
100-
});
92+
passwordResetToken: hashedToken
93+
}
94+
})
10195

10296
if (!user) {
103-
return { success: false, error: 'Invalid or expired token.' };
97+
return { success: false, error: 'Invalid or expired token.' }
10498
}
10599

106-
const hashedPassword = await bcrypt.hash(newPassword, 10);
100+
const hashedPassword = await bcrypt.hash(newPassword, 10)
107101

108102
await prisma.user.update({
109103
where: { id: user.id },
110104
data: {
111105
hashedPassword,
112106
passwordResetToken: null,
113-
passwordResetExpires: null,
114-
},
115-
});
107+
passwordResetExpires: null
108+
}
109+
})
116110

117-
return { success: true };
111+
return { success: true }
118112
}

0 commit comments

Comments
 (0)