-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathauth.controller.js
108 lines (88 loc) · 4.11 KB
/
auth.controller.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
import bcrypt from "bcrypt";
import admin from "firebase-admin";
import { getAuth } from "firebase-admin/auth";
import serviceAccountKey from "../firebase-adminsdk.json" assert { type: "json" };
import User from "../Models/user.model.js";
import { formatDataToSend, generateUsername, emailRegex, passwordRegex } from "../utils/helpers.js";
export const signup = async (req, res) => {
let { fullname, email, password } = req.body;
if (fullname.length < 3) return res.status(400).json({ error: "Full name should be at least 3 letters long" });
if (!emailRegex.test(email)) return res.status(400).json({ error: "Invalid email" });
if (!passwordRegex.test(password)) return res.status(400).json({
error: "Password should be at least 6 characters long and contain at least one uppercase letter, one lowercase letter, and one number"
});
try {
const hashed_password = await bcrypt.hash(password, 10);
const username = await generateUsername(email);
const user = new User({
personal_info: { fullname, email, password: hashed_password, username }
});
const savedUser = await user.save();
return res.status(201).json(formatDataToSend(savedUser));
} catch (err) {
if (err.code === 11000) return res.status(409).json({ error: "User with this email already exists" });
return res.status(500).json({ error: "Internal Server Error" });
}
};
export const login = async (req, res) => {
let { email, password } = req.body;
try {
const user = await User.findOne({ "personal_info.email": email });
if (!user) return res.status(404).json({ error: "Email not found" });
if (user.google_auth) return res.status(403).json({ "error": "This email was signed up with google. Please log in with google to access the account." });
const isMatch = await bcrypt.compare(password, user.personal_info.password);
if (!isMatch) return res.status(401).json({ error: "Incorrect password" });
return res.status(200).json(formatDataToSend(user));
} catch (err) {
return res.status(500).json({ error: "Internal Server Error" });
}
};
// Google Authorization using Firebase
admin.initializeApp({
credential: admin.credential.cert(serviceAccountKey)
})
export const googleAuth = async (req, res) => {
let { access_token } = req.body;
try {
getAuth()
.verifyIdToken(access_token)
.then(async (decodedUser) => {
let { email, name } = decodedUser;
let user = await User.findOne({ "personal_info.email": email }).select("personal_info.fullname personal_info.username personal_info.profile_img google_auth").then((u) => {
return u || null;
})
.catch(err => {
return res.status(500).json({ "error": err.message });
})
if (user) {
// login
if (!user.google_auth) {
return res.status(403).json({ "error": "This email was signed up without google. Please log in with password to access the account." });
}
} else {
//signup
let username = await generateUsername(email);
user = new User({
personal_info: {
fullname: name,
email,
username
},
google_auth: true
})
await user.save().then((u) => {
user = u;
})
.catch(err => {
return res.status(500).json({ "error": err.message });
})
}
return res.status(200).json(formatDataToSend(user));
})
.catch(err => {
return res.status(500).json({ "error": err.message });
})
} catch (err) {
return res.status(500).json({ "error": err.message });
}
}