Skip to content

Commit dd28dc3

Browse files
committed
changed
1 parent ea13422 commit dd28dc3

9 files changed

Lines changed: 101 additions & 58 deletions

File tree

backend/models/User.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@ const mongoose = require("mongoose");
22
const bcrypt = require("bcryptjs");
33

44
const UserSchema = new mongoose.Schema({
5-
username: {
6-
type: String,
7-
required: true,
8-
unique: true,
9-
},
10-
email: {
11-
type: String,
12-
required: true,
13-
unique: true,
14-
},
15-
password: {
16-
type: String,
17-
required: true,
18-
},
5+
username: {
6+
type: String,
7+
required: true,
8+
},
9+
email: {
10+
type: String,
11+
required: true,
12+
unique: true,
13+
},
14+
password: {
15+
type: String,
16+
},
17+
googleId: {
18+
type: String,
19+
unique: true,
20+
sparse: true,
21+
},
1922
});
2023

24+
2125
UserSchema.pre('save', async function (next) {
2226

2327
if (!this.isModified('password'))

backend/routes/auth.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,28 @@ router.post("/login", passport.authenticate('local'), (req, res) => {
2727
res.status(200).json( { message: 'Login successful', user: req.user } );
2828
});
2929

30+
router.post("/google-login", async (req, res) => {
31+
try {
32+
const { email, name, googleId } = req.body;
33+
34+
let user = await User.findOne({ email });
35+
36+
if (!user) {
37+
user = new User({
38+
username: name,
39+
email,
40+
googleId,
41+
});
42+
await user.save();
43+
}
44+
45+
return res.json({ message: "Login successful", user });
46+
} catch (error) {
47+
console.error("Google login error:", error);
48+
return res.status(500).json({ message: "Server error" });
49+
}
50+
});
51+
3052
// Logout route
3153
router.get("/logout", (req, res) => {
3254

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"axios": "^1.7.7",
2222
"firebase": "^12.1.0",
2323
"framer-motion": "^12.23.12",
24+
"i": "^0.3.7",
2425
"lucide-react": "^0.525.0",
2526
"octokit": "^4.0.2",
2627
"postcss": "^8.4.47",

src/App.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { useEffect } from "react";
2+
import { getRedirectResult, GoogleAuthProvider } from "firebase/auth";
3+
import { auth } from "./hooks/firebase";
4+
import axios from "axios";
15
import Navbar from "./components/Navbar";
26
import Footer from "./components/Footer";
37
import ScrollProgressBar from "./components/ScrollProgressBar";
@@ -6,6 +10,33 @@ import Router from "./Routes/Router";
610
import ThemeWrapper from "./context/ThemeContext";
711

812
function App() {
13+
useEffect(() => {
14+
const handleGoogleRedirect = async () => {
15+
try {
16+
const result = await getRedirectResult(auth);
17+
if (result && result.user) {
18+
// Get user info from Firebase
19+
const email = result.user.email;
20+
const name = result.user.displayName;
21+
const googleId = result.user.uid;
22+
23+
// Send user info to backend
24+
await axios.post("/api/auth/google-login", {
25+
email,
26+
name,
27+
googleId,
28+
});
29+
30+
// You can now set user state, navigate, etc.
31+
}
32+
} catch (error) {
33+
console.error("Google redirect error:", error);
34+
}
35+
};
36+
37+
handleGoogleRedirect();
38+
}, []);
39+
940
return (
1041
<ThemeWrapper>
1142
<div className="relative flex flex-col min-h-screen">

src/components/GoogleSignIn.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import React, { useState } from 'react';
2-
import { signInWithGoogle } from '../hooks/firebase';
1+
import { signInWithGoogle } from "../hooks/firebase";
2+
import React, { useState } from "react";
33

44
const GoogleSignIn = () => {
55
const [isLoading, setIsLoading] = useState(false);
66

77
const handleSignIn = async () => {
88
setIsLoading(true);
99
try {
10-
await signInWithGoogle();
10+
await signInWithGoogle(); // No result returned for redirect flow
11+
// After redirect, handle user in App.tsx using getRedirectResult
1112
} catch (error) {
1213
console.error("Error signing in with Google", error);
1314
} finally {
@@ -17,14 +18,11 @@ const GoogleSignIn = () => {
1718

1819
return (
1920
<button
20-
type="button"
21-
disabled={isLoading}
2221
onClick={handleSignIn}
23-
className="w-full bg-gradient-to-r from-purple-600 via-pink-600 to-indigo-600 text-white py-4 px-6 rounded-2xl font-semibold focus:ring-4 focus:ring-purple-500/50 transition-all duration-300 disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center gap-2"
22+
disabled={isLoading}
23+
className="w-full bg-white text-black font-semibold py-4 rounded-2xl hover:bg-gray-100"
2424
>
25-
{/* Optional: Add a Google icon */}
26-
<img src="https://www.svgrepo.com/show/475656/google-color.svg" alt="Google" className="w-6 h-6" />
27-
{isLoading ? "Signing in with Google..." : "Sign in with Google"}
25+
{isLoading ? "Signing in..." : "Sign in with Google"}
2826
</button>
2927
);
3028
};

src/firebase.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/hooks/firebase.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { initializeApp } from 'firebase/app';
22
import { getAuth, GoogleAuthProvider, signInWithRedirect, setPersistence, browserLocalPersistence } from 'firebase/auth';
33

44
const firebaseConfig = {
5-
apiKey: "AIzaSyDT2h_X2bSMxjsiHilQQx9ryPwC3654lt0",
6-
authDomain: "github-tracker-f39ac.firebaseapp.com",
7-
projectId: "github-tracker-f39ac",
8-
storageBucket: "github-tracker-f39ac.firebasestorage.app",
9-
messagingSenderId: "562897055831",
10-
appId: "1:562897055831:web:8f63882abd2f9e13b8f312"
5+
apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
6+
authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
7+
projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
8+
storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET,
9+
messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID,
10+
appId: import.meta.env.VITE_FIREBASE_APP_ID
1111
};
1212

1313
const app = initializeApp(firebaseConfig);

src/pages/Login/Login.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import axios from "axios";
33
import { useNavigate, Link } from "react-router-dom";
44
import { ThemeContext } from "../../context/ThemeContext";
55
import type { ThemeContextType } from "../../context/ThemeContext";
6+
import GoogleSignIn from "../../components/GoogleSignIn";
7+
8+
69

710
const backendUrl = import.meta.env.VITE_BACKEND_URL;
811

@@ -126,8 +129,14 @@ const Login: React.FC = () => {
126129
>
127130
{isLoading ? "Signing in..." : "Sign In"}
128131
</button>
132+
{/* Google Sign-In Button */}
133+
<div className="mt-4">
134+
<GoogleSignIn />
135+
</div>
136+
129137
</form>
130138

139+
131140
{/* Message */}
132141
{message && (
133142
<div className={`mt-6 p-4 rounded-2xl text-center text-sm font-medium ${

src/pages/Signup/Signup.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React, { useState, ChangeEvent, FormEvent } from "react";
22
import axios from "axios";
33
import { useNavigate ,Link } from "react-router-dom";
44
import { User, Mail, Lock } from "lucide-react";
5+
import GoogleSignIn from "../../components/GoogleSignIn";
6+
57
const backendUrl = import.meta.env.VITE_BACKEND_URL;
68
interface SignUpFormData {
79
username: string;
@@ -141,7 +143,12 @@ const navigate = useNavigate();
141143
}`}>
142144
{message}
143145
</div>
146+
144147
)}
148+
<div className="mt-6">
149+
<GoogleSignIn />
150+
</div>
151+
145152

146153
<div className="text-center mt-8">
147154
<p className="text-purple-200">

0 commit comments

Comments
 (0)