Express TS project
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

70 rader
1.7 KiB

  1. import express, { response, urlencoded } from 'express';
  2. import { DB_NAME, getDatabaseClient } from '../db-utils';
  3. import bcrypt from 'bcrypt';
  4. const authRoutes = express.Router();
  5. export const SALT_ROUNDS = 12;
  6. authRoutes.get('/users/', async (request, response) => {
  7. console.log(request);
  8. response.send("List of users will be displayed");
  9. });
  10. authRoutes.post('/register-applicant/', async (request, response) => {
  11. const name = request.body.name;
  12. const email = request.body.email;
  13. const password = request.body.password;
  14. const userType = 'APPLICANT';
  15. const isVerified = false;
  16. const userCollection = getDatabaseClient().db(DB_NAME).collection('users');
  17. // Check if form is filled
  18. if (!name || !email || !password) {
  19. response.status(400);
  20. response.send("Please field the required fields");
  21. return;
  22. }
  23. try {
  24. const userWithSameMail = await userCollection.find({
  25. email,
  26. }).count();
  27. if (userWithSameMail > 0) {
  28. response.status(400);
  29. response.send('EmailID already exists');
  30. return;
  31. }
  32. } catch(e) {
  33. console.log(e);
  34. return;
  35. }
  36. try {
  37. bcrypt.hash(password, SALT_ROUNDS, (error, hashedPassword) => {
  38. if (error) {
  39. throw error;
  40. }
  41. userCollection.insertOne({
  42. name,
  43. email,
  44. password: hashedPassword,
  45. isVerified,
  46. userType,
  47. });
  48. });
  49. response.send("Registeration Complete, Please verify your profile to proceed further");
  50. } catch (e) {
  51. console.log(e);
  52. }
  53. return;
  54. });
  55. export default authRoutes;