Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLT-28] Google Authentication #1099

Merged
merged 3 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ __pycache__\
*.ini
.ipynb_checkpoints/

# Secret files
kaggle.json


################################################################# WEB DEVELOPMENT #################################################################
Expand Down Expand Up @@ -137,3 +135,8 @@ typings
# typescript
*.tsbuildinfo
next-env.d.ts


################################################################# Secret files #################################################################
kaggle.json
firebase-adminsdk.json
64 changes: 64 additions & 0 deletions backend/Controllers/auth.controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
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";

Expand Down Expand Up @@ -34,6 +39,8 @@ export const login = async (req, res) => {
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" });

Expand All @@ -42,3 +49,60 @@ export const login = async (req, res) => {
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 });
}
}
3 changes: 2 additions & 1 deletion backend/Routes/api/auth.routes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import express from "express";
import { signup, login } from "../../Controllers/auth.controller.js";
import { signup, login, googleAuth } from "../../Controllers/auth.controller.js";

const authRoutes = express.Router();

authRoutes.post("/signup", signup);
authRoutes.post("/login", login);
authRoutes.post("/google-auth", googleAuth);

export default authRoutes;
Loading