|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import express, { response, urlencoded } from 'express';
- import { DB_NAME, getDatabaseClient } from '../db-utils';
- import bcrypt from 'bcrypt';
-
- const authRoutes = express.Router();
-
- export const SALT_ROUNDS = 12;
-
- authRoutes.get('/users/', async (request, response) => {
- console.log(request);
- response.send("List of users will be displayed");
- });
-
- authRoutes.post('/register-applicant/', async (request, response) => {
- const name = request.body.name;
- const email = request.body.email;
- const password = request.body.password;
- const userType = 'APPLICANT';
- const isVerified = false;
-
- const userCollection = getDatabaseClient().db(DB_NAME).collection('users');
-
- // Check if form is filled
- if (!name || !email || !password) {
- response.status(400);
- response.send("Please field the required fields");
- return;
- }
-
- try {
- const userWithSameMail = await userCollection.find({
- email,
- }).count();
-
- if (userWithSameMail > 0) {
- response.status(400);
- response.send('EmailID already exists');
- return;
- }
-
- } catch(e) {
- console.log(e);
- return;
- }
-
- try {
- bcrypt.hash(password, SALT_ROUNDS, (error, hashedPassword) => {
- if (error) {
- throw error;
- }
-
- userCollection.insertOne({
- name,
- email,
- password: hashedPassword,
- isVerified,
- userType,
- });
- });
-
- response.send("Registeration Complete, Please verify your profile to proceed further");
- } catch (e) {
- console.log(e);
- }
-
- return;
-
- });
-
- export default authRoutes;
|