-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
134 lines (127 loc) · 4.56 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import mongoose from "mongoose";
import express from "express"
import User from "../Models/User.js";
import bcrypt from "bcryptjs";
import { createError } from "../error.js";
import jwt from "jsonwebtoken";
import Token from '../Models/Token.js'
import sendEmail from "../sendEmail.js";
import crypto from 'crypto'
import dotenv from "dotenv"
import emailTemplate from "../emailTemplate.js";
dotenv.config();
// const http = require('http');
// const querystring = require('querystring');
// const app = express();
// app.use(cookieParser);
let reg=/.ac.in/
export let signup = async (req, res, next) => {
try {
const salt = bcrypt.genSaltSync(10);
const hash = bcrypt.hashSync(req.body.password, salt); //encrypting password
const newUser = new User({ ...req.body, password: hash }); //creating new user
let e=newUser.email;
let p=reg.test(e);
if(p!=true)return next(createError(401, "Use college email id only"))
// console.log("p"+ p);
await newUser.save();
signup = await newUser.save();
const token = await new Token({
userId: signup._id,
token: crypto.randomBytes(32).toString("hex")
}).save();
const url = `${process.env.BASE_URL}users/${newUser._id}/verify/${token.token}`;
await sendEmail(newUser.email, "Verification email", emailTemplate(url, "Verify Your Email Address",newUser.Normalname,"Thank you for signing up for IIITU Snapshot! To get started, please click the button below to verify your email address","Verify Email","If you didn't sign up for IIITU Snapshot, you can safely ignore this email."))
res.status(200).send("An email has been sent to you verify it for further process!");
} catch (err) {
console.log(err)
next(err);
}
};
export var toExport;
export const signin = async (req, res, next) => {
try {
const user = await User.findOne({ name: req.body.name });
if (!user) return next(createError(404, "User not found!"));
const isCorrect = await bcrypt.compare(req.body.password, user.password);
if (!isCorrect) return next(createError(400, "Wrong Credentials!"));
let e=user.email;
let p=reg.test(e);
if(p!=true)return next(createError(401, "Use college email id only"))
if (!user.verified) {
let token = await Token.findOne({ userId: user._id });
if (token === undefined || token === null) {
token = await new Token({
userId: user._id,
token: crypto.randomBytes(32).toString("hex"),
}).save();
}
const url = `${process.env.BASE_URL}users/${user._id}/verify/${token.token}`;
await sendEmail(user.email, "Verification email", emailTemplate(url, "Verify Your Email Address",user.Normalname,"Thank you for signing up for IIITU Snapshot! To get started, please click the button below to verify your email address","Verify Email","If you didn't sign up for IIITU Snapshot, you can safely ignore this email."))
return res
.status(400)
.send({ message: "An Email sent to your account please verify" });
}
var token = jwt.sign({ id: user._id }, process.env.JWT); //assigning a token to user
var { password, ...others } = user._doc; //stopping to send password
res
.cookie("access_token", token, { //sending token as cookie as acess token
secure: true,
sameSite: "none"
});
res
.status(200)
.json(others);
} catch (err) {
next(err);
}
};
export const signout = async (req, res, next) => {
try {
const user = null;
res
.cookie("access_token", "null", {
secure: true,
sameSite: "none"
})
.status(200)
.json("Logout");
} catch (err) {
next(err);
}
};
export const googleAuth = async (req, res, next) => {
try {
const user = await User.findOne({ email: req.body.email });
if (user) {
const token = jwt.sign({ id: user._id }, process.env.JWT);
res
.cookie("access_token", token, {
secure: true,
sameSite: "none"
})
.status(200)
.json(user._doc);
} else {
const newUser = new User({
...req.body,
fromGoogle: true,
});
let e=newUser.email;
let p=reg.test(e);
if(p!=true)return next(createError(401, "Use college email id only"))
const savedUser = await newUser.save();
const token = jwt.sign({ id: savedUser._id }, process.env.JWT);
res
.cookie("access_token", token, {
secure: true,
sameSite: "none"
})
.status(200)
.json(savedUser._doc);
}
} catch (err) {
next(err);
}
};
// module .exports=signin;