Skip to content

Commit

Permalink
Merge pull request #91 from Naresh-chandanbatve/improve-signup
Browse files Browse the repository at this point in the history
Improved signup as required
  • Loading branch information
Siddhant-Patil0203 authored Oct 2, 2023
2 parents 1a520cb + 5148eb3 commit d36ab7d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
1 change: 1 addition & 0 deletions server/src/api/controllers/deleteUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const deleteUser = async (req, res) => {
"@hotmail.com",
"@aol.com",
"@outlook.com",
"@gcoen.ac.in",
];


Expand Down
2 changes: 2 additions & 0 deletions server/src/api/controllers/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const updateProfile = async (req, res) => {
"@hotmail.com",
"@aol.com",
"@outlook.com",
"@gcoen.ac.in",
];

var result = {}
Expand Down Expand Up @@ -151,6 +152,7 @@ export const updateProfile = async (req, res) => {


res.json({
success: true,
msg: "user "+newResult.role+" updated successfully",
user: newResult
})
Expand Down
43 changes: 31 additions & 12 deletions server/src/api/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const user = async (req, res) => {
* Route: /user/signup
* Desc: user sign up
*/
export const signup = async (req, res) => {
export const signup = async (req, res, next) => {
const { email, password, confirmPassword} = req.body


Expand All @@ -37,11 +37,12 @@ export const signup = async (req, res) => {
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[@$%#^&*])(?=.*[0-9]).{8,}$/;

const emailDomains = [
"@gmail.com",
"@yahoo.com",
"@hotmail.com",
"@aol.com",
"@outlook.com",
"@gmail.com",
"@yahoo.com",
"@hotmail.com",
"@aol.com",
"@outlook.com",
"@gcoen.ac.in",
];


Expand Down Expand Up @@ -102,15 +103,33 @@ export const signup = async (req, res) => {
});

if(result){
res.json({
success: true,
msg: "User Added Successfully !"})

const oldUser = await userModel.findOne({email})

const SECRET = process.env.USER_SECRET

const token = generateToken(oldUser, SECRET);

req.session.user = {
token: token,
user: oldUser
}

res.status(200).json({
success: true,
result: oldUser,
token,
// csrfToken: req.csrfToken,
msg: "User added and logged in successfully"
});
}
}
else{
res.json({
success: false,
msg: "user already exist"})
res.status(403).json({
success: false,
msg: "user already exist"
})

}
}
catch(err){
Expand Down
2 changes: 1 addition & 1 deletion server/src/api/middlewares/generateToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import jwt from "jsonwebtoken";
import * as dotenv from "dotenv";
dotenv.config();

const generateToken = (result, SECRET, tokenValidity = "1h") => {
const generateToken = (result, SECRET, tokenValidity = "7d") => {
return jwt.sign({ email: result.email, id: result._id }, SECRET, {
expiresIn: tokenValidity,
});
Expand Down
1 change: 1 addition & 0 deletions server/src/api/models/userModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import sanitizerPlugin from 'mongoose-sanitizer'
const userModel = mongoose.Schema({
id: { type: String },
name: { type: String },
collegeEmail: { type: String },
email: { type: String, required: true },
password: { type: String, required: true },
role: { type: String, default: 'faculty'},
Expand Down
2 changes: 1 addition & 1 deletion server/src/api/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {user, signup, signin} from "../controllers/user.js";


router.get("/", user)
router.post("/signup", signup)
router.post("/signup", session, signup)
// router.post("/signin", session, csrfProtect, signin)
router.post("/signin", session, signin)

Expand Down

0 comments on commit d36ab7d

Please sign in to comment.