Skip to content

Commit 95b2b4a

Browse files
[PLT-28] Google Authentication (#1099)
* created /google-auth route * integrate /google-auth route with frontend * hide secret credentials
1 parent fcbc852 commit 95b2b4a

File tree

10 files changed

+2620
-17
lines changed

10 files changed

+2620
-17
lines changed

.gitignore

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ __pycache__\
1515
*.ini
1616
.ipynb_checkpoints/
1717

18-
# Secret files
19-
kaggle.json
2018

2119

2220
################################################################# WEB DEVELOPMENT #################################################################
@@ -137,3 +135,8 @@ typings
137135
# typescript
138136
*.tsbuildinfo
139137
next-env.d.ts
138+
139+
140+
################################################################# Secret files #################################################################
141+
kaggle.json
142+
firebase-adminsdk.json

backend/Controllers/auth.controller.js

+64
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import bcrypt from "bcrypt";
2+
3+
import admin from "firebase-admin";
4+
import { getAuth } from "firebase-admin/auth";
5+
import serviceAccountKey from "../firebase-adminsdk.json" assert { type: "json" };
6+
27
import User from "../Models/user.model.js";
38
import { formatDataToSend, generateUsername, emailRegex, passwordRegex } from "../utils/helpers.js";
49

@@ -34,6 +39,8 @@ export const login = async (req, res) => {
3439
const user = await User.findOne({ "personal_info.email": email });
3540
if (!user) return res.status(404).json({ error: "Email not found" });
3641

42+
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." });
43+
3744
const isMatch = await bcrypt.compare(password, user.personal_info.password);
3845
if (!isMatch) return res.status(401).json({ error: "Incorrect password" });
3946

@@ -42,3 +49,60 @@ export const login = async (req, res) => {
4249
return res.status(500).json({ error: "Internal Server Error" });
4350
}
4451
};
52+
53+
// Google Authorization using Firebase
54+
55+
admin.initializeApp({
56+
credential: admin.credential.cert(serviceAccountKey)
57+
})
58+
59+
export const googleAuth = async (req, res) => {
60+
let { access_token } = req.body;
61+
62+
try {
63+
getAuth()
64+
.verifyIdToken(access_token)
65+
.then(async (decodedUser) => {
66+
67+
let { email, name } = decodedUser;
68+
69+
let user = await User.findOne({ "personal_info.email": email }).select("personal_info.fullname personal_info.username personal_info.profile_img google_auth").then((u) => {
70+
return u || null;
71+
})
72+
.catch(err => {
73+
return res.status(500).json({ "error": err.message });
74+
})
75+
76+
if (user) {
77+
// login
78+
if (!user.google_auth) {
79+
return res.status(403).json({ "error": "This email was signed up without google. Please log in with password to access the account." });
80+
}
81+
} else {
82+
//signup
83+
let username = await generateUsername(email);
84+
user = new User({
85+
personal_info: {
86+
fullname: name,
87+
email,
88+
username
89+
},
90+
google_auth: true
91+
})
92+
await user.save().then((u) => {
93+
user = u;
94+
})
95+
.catch(err => {
96+
return res.status(500).json({ "error": err.message });
97+
})
98+
}
99+
100+
return res.status(200).json(formatDataToSend(user));
101+
})
102+
.catch(err => {
103+
return res.status(500).json({ "error": err.message });
104+
})
105+
} catch (err) {
106+
return res.status(500).json({ "error": err.message });
107+
}
108+
}

backend/Routes/api/auth.routes.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import express from "express";
2-
import { signup, login } from "../../Controllers/auth.controller.js";
2+
import { signup, login, googleAuth } from "../../Controllers/auth.controller.js";
33

44
const authRoutes = express.Router();
55

66
authRoutes.post("/signup", signup);
77
authRoutes.post("/login", login);
8+
authRoutes.post("/google-auth", googleAuth);
89

910
export default authRoutes;

0 commit comments

Comments
 (0)