Skip to content

Commit

Permalink
Merge pull request #71 from Monilprajapati/main
Browse files Browse the repository at this point in the history
Done with Google Auth Login and Register
  • Loading branch information
victorchrollo14 authored Apr 8, 2024
2 parents 41db6a7 + 5adbaa5 commit 6bccf5c
Show file tree
Hide file tree
Showing 18 changed files with 11,240 additions and 42 deletions.
10 changes: 10 additions & 0 deletions client/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
VITE_CLIENT_ID=githubclientid
VITE_URL=http://localhost:3001

# Firebase environment variables

VITE_FIREBASE_API_KEY=
VITE_FIREBASE_AUTH_DOMAIN=
VITE_FIREBASE_PROJECT_ID=
VITE_FIREBASE_STORAGE_BUCKET=
VITE_FIREBASE_MESSAGING_SENDER_ID=
VITE_FIREBASE_APP_ID=

4,298 changes: 4,281 additions & 17 deletions client/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"axios": "^1.5.0",
"firebase": "^10.10.0",
"flowbite-react": "^0.6.0",
"react": "^18.2.0",
"react-cookie": "^6.1.1",
Expand Down
5 changes: 4 additions & 1 deletion client/src/components/Google.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React from "react";
import { FcGoogle } from "react-icons/fc";
import { handleGoogleAuth } from "../services/auth";

const GoogleButton = ({ children }) => {
return (
<button className="flex my-4 gap-4 w-5/6 max-w-xs text-lg md:text-xl justify-center items-center text-black outline-none border-2 border-lightSlate px-2 md:w-[400px] h-16 rounded-xl">
<button className="flex my-4 gap-4 w-5/6 max-w-xs text-lg md:text-xl justify-center items-center text-black outline-none border-2 border-lightSlate px-2 md:w-[400px] h-16 rounded-xl"
onClick={handleGoogleAuth}
>
<FcGoogle className="" />
<span>{children}</span>
</button>
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/home/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const Home = () => {
getRandom();
}
}, [allCategories]);

console.log(featured);
return (
<div className="overflow-x-hidden w-full">
<NavBar />
Expand Down
41 changes: 40 additions & 1 deletion client/src/services/auth.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { authWithGoogle } from "./firebase";
const URL = import.meta.env.VITE_URL;

const register = async (userData) => {
Expand Down Expand Up @@ -33,6 +34,44 @@ const login = async (userData, setLoggedIn, setToken) => {
}
};

const handleGoogleAuth = (e) => {
e.preventDefault();

authWithGoogle()
.then((user) => {
let serverRoute = "google-auth";
let formData = {
access_token: user.accessToken,
};
console.log(formData)
fetch(`${URL}/user/register/${serverRoute}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(formData),
})
.then((res) => {
if (res.status === 200) {
return res.json();
} else {
return console.log("Google auth failed");
}
})
.then((data) => {
console.log(data);
localStorage.setItem("token", data.access_token);
window.location.href = "/";
})
.catch((err) => {
return console.log(err);
});
})
.catch((err) => {
return console.log(err);
});
};

const logout = async () => {
try {
localStorage.removeItem("token");
Expand All @@ -42,4 +81,4 @@ const logout = async () => {
}
};

export { register, login, logout };
export { register, login, logout, handleGoogleAuth };
31 changes: 31 additions & 0 deletions client/src/services/firebase.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { initializeApp } from "firebase/app";
import { GoogleAuthProvider,signInWithPopup,getAuth } from "firebase/auth";

const firebaseConfig = {
apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET,
messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID,
appId: import.meta.env.VITE_FIREBASE_APP_ID,
};

const app = initializeApp(firebaseConfig);

const provider = new GoogleAuthProvider();

const auth = getAuth();

const authWithGoogle = async () => {
try {
const result = await signInWithPopup(auth, provider);
const user = result.user;
console.log(user)
return user
} catch (error) {
console.log(error);
return err
}
};

export { authWithGoogle };
16 changes: 15 additions & 1 deletion server/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
MONGODB_URI=mongodb://127.0.0.1:27017/blackmarket
JWT_SECRET=jwt_secret
CLIENT_ID=github_client_id
CLIENT_SECRET=github_secret
CLIENT_SECRET=github_secret

# Firebase environment variables

TYPE=
PROJECT_ID=
PRIVATE_KEY_ID=
PRIVATE_KEY=
FIREBASE_CLIENT_EMAIL=
FIREBASE_CLIENT_ID=
AUTH_URI=
TOKEN_URI=
AUTH_PROVIDER_X509_CERT_URL=
CLIENT_X509_CERT_URL=
UNIVERSE_DOMAIN=
1 change: 0 additions & 1 deletion server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ if (NODE_ENV === "production") {
}

console.log(corsUrls);

export { MONGODB_URI, JWT_PRIVATE_KEY, CLIENT_ID, CLIENT_SECRET, corsUrls };
13 changes: 13 additions & 0 deletions server/firebase-sdk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const serviceAccountKey = {
type: process.env.TYPE,
project_id: process.env.PROJECT_ID,
private_key_id: process.env.PRIVATE_KEY_ID,
private_key: process.env.PRIVATE_KEY,
client_email: process.env.FIREBASE_CLIENT_EMAIL,
client_id: process.env.FIREBASE_CLIENT_ID,
auth_uri: process.env.AUTH_URI,
token_uri: process.env.TOKEN_URI,
auth_provider_x509_cert_url: process.env.AUTH_PROVIDER_X509_CERT_URL,
client_x509_cert_url: process.env.CLIENT_X509_CERT_URL,
universe_domain: process.env.UNIVERSE_DOMAIN,
};
9 changes: 8 additions & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import cors from "cors";
import mongoose from "mongoose";
import bodyParser from "body-parser";
import cookieParser from "cookie-parser";
import admin from "firebase-admin";
import {serviceAccountKey} from "./firebase-sdk.js";
import { MONGODB_URI, corsUrls, JWT_PRIVATE_KEY } from "./config.js";

import { userRouter } from "./src/routes/userRoute.js";
import { productRouter } from "./src/routes/productRoute.js";
import { cartRouter } from "./src/routes/cartRoute.js";
Expand All @@ -19,6 +20,12 @@ const corsOptions = {
optionSuccessStatus: 200,
};

// firebase admin
admin.initializeApp({
credential: admin.credential.cert(serviceAccountKey),
});


app.use(cors(corsOptions));
app.use(express.json());

Expand Down
Loading

0 comments on commit 6bccf5c

Please sign in to comment.