|
1 | 1 | import User from '../models/userModel';
|
2 |
| -import Profile from '../models/profileModel'; |
3 | 2 | import generateToken from '../utils/generateToken';
|
4 | 3 | import { Request, Response, NextFunction } from 'express';
|
5 | 4 | import { UserType } from '../types/user';
|
6 |
| -import GraduateInvitation from '../models/graduateInvitationModel'; |
7 | 5 |
|
8 |
| -// ENDPOINT POST api/users/register |
9 |
| -// PURPOSE Register a new user |
10 |
| -// ACCESS Public |
11 |
| -const registerUser = async (req: Request, res: Response, next: NextFunction) => { |
12 |
| - const { email, password } = req.body; |
13 |
| - const { token } = req.query; |
14 |
| - |
15 |
| - try { |
16 |
| - const isValidEmail = email.match(/[\w\d.]+@[a-z]+.[\w]+$/gim); |
17 |
| - if (!isValidEmail) { |
18 |
| - return res.status(400).json('Invalid Email'); |
19 |
| - } |
20 |
| - |
21 |
| - const invitation = await GraduateInvitation.findOne({ |
22 |
| - email, |
23 |
| - token, |
24 |
| - tokenExpiry: { $gt: new Date() }, |
25 |
| - isRegistered: false, |
26 |
| - }); |
27 |
| - |
28 |
| - //TODO Needs better error handling - this can trigger with situaions other than bad or missing token |
29 |
| - if (!invitation) { |
30 |
| - return res.status(400).json({ message: 'Invalid or expired registration token' }); |
31 |
| - } |
32 |
| - const userExists: UserType | null = await User.findOne({ email }); |
33 |
| - if (userExists) { |
34 |
| - return res.status(400).json({ message: 'User already exists!' }); |
35 |
| - } |
36 |
| - const user: UserType = await User.create({ |
37 |
| - firstName: invitation.firstName, |
38 |
| - lastName: invitation.lastName, |
39 |
| - email, |
40 |
| - password, |
41 |
| - }); |
42 |
| - |
43 |
| - if (user) { |
44 |
| - invitation.isRegistered = true; |
45 |
| - await invitation?.save(); |
46 |
| - await Profile.create({ |
47 |
| - user: user._id, |
48 |
| - firstName: invitation.firstName, |
49 |
| - lastName: invitation.lastName, |
50 |
| - cohort: invitation.cohort, |
51 |
| - }); |
52 |
| - res.locals.user = { |
53 |
| - _id: user._id, |
54 |
| - firstName: user.firstName, |
55 |
| - lastName: user.lastName, |
56 |
| - email: user.email, |
57 |
| - }; |
58 |
| - |
59 |
| - res.cookie('token', generateToken(user._id.toString())); |
60 |
| - return res.status(201).json(res.locals.user); |
61 |
| - } |
62 |
| - } catch (error) { |
63 |
| - console.error('Error during user signup:', error); |
64 |
| - return next({ |
65 |
| - log: 'Express error in createUser Middleware', |
66 |
| - status: 503, |
67 |
| - message: { err: 'An error occurred during sign-up' }, |
68 |
| - }); |
69 |
| - } |
70 |
| -}; |
71 | 6 | // ENDPOINT POST api/users/login
|
72 | 7 | // PURPOSE Authenticate User and get token
|
73 | 8 | // ACCESS Public
|
@@ -165,4 +100,4 @@ const deleteUserByEmail = async (req: Request, res: Response, next: NextFunction
|
165 | 100 | }
|
166 | 101 | };
|
167 | 102 |
|
168 |
| -export { registerUser, authUser, getUserById, deleteUserByEmail }; |
| 103 | +export { authUser, getUserById, deleteUserByEmail }; |
0 commit comments