Skip to content

Commit

Permalink
Merge pull request #73 from victorchrollo14/Authfix
Browse files Browse the repository at this point in the history
Fixed the google Auth issue and added Avatar image
  • Loading branch information
Monilprajapati authored Aug 5, 2024
2 parents beb9d4d + 84d237b commit e410cad
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 18 deletions.
28 changes: 21 additions & 7 deletions client/src/components/NavBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import { useUserContext } from "../contexts/userContext";

const NavBar = () => {
const navigate = useNavigate();
const { loggedIn } = useUserContext();
const { loggedIn, user } = useUserContext();
const [toggle, setToggle] = useState(false);
const [search, setSearch] = useState(false);
// console.log(user);
// console.log(user?.avatar);

return (
// <div className="sm:h-[60px] md:h-[70px] xl:h-[89px]">
Expand Down Expand Up @@ -65,12 +67,24 @@ const NavBar = () => {
navigate("/cart");
}}
/>
<FaUserCircle
className="text-[25px] place-items-center cursor-pointer xl:text-[35px]"
onClick={() => {
loggedIn ? navigate("/profile") : navigate("/register");
}}
/>

{user?.avatar ? (
<img
src={user?.avatar}
alt="userImage"
className="w-8 h-8 rounded-full cursor-pointer"
onClick={() => {
navigate("/profile");
}}
/>
) : (
<FaUserCircle
className="text-[25px] place-items-center cursor-pointer xl:text-[35px]"
onClick={() => {
loggedIn ? navigate("/profile") : navigate("/register");
}}
/>
)}
</div>

{/* Mobile view */}
Expand Down
5 changes: 3 additions & 2 deletions client/src/services/auth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ const handleGoogleAuth = (e) => {

authWithGoogle()
.then((user) => {
const { accessToken, photoURL } = user;
let serverRoute = "google-auth";
let formData = {
access_token: user.accessToken,
access_token: accessToken,
avatar: photoURL,
};
console.log(formData)
fetch(`${URL}/user/register/${serverRoute}`, {
Expand All @@ -59,7 +61,6 @@ const handleGoogleAuth = (e) => {
}
})
.then((data) => {
console.log(data);
localStorage.setItem("token", data.access_token);
window.location.href = "/";
})
Expand Down
1 change: 0 additions & 1 deletion client/src/services/firebase.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const authWithGoogle = async () => {
try {
const result = await signInWithPopup(auth, provider);
const user = result.user;
console.log(user)
return user
} catch (error) {
console.log(error);
Expand Down
2 changes: 1 addition & 1 deletion server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const JWT_PRIVATE_KEY = process.env.JWT_PRIVATE_KEY;
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;

console.log(MONGODB_URI, JWT_PRIVATE_KEY, CLIENT_ID, CLIENT_SECRET);
console.log("URI", MONGODB_URI);

if (NODE_ENV === "production") {
const CLIENT_URL1 = process.env.CLIENT_URL1;
Expand Down
3 changes: 1 addition & 2 deletions server/firebase-sdk.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const { private_key } = JSON.parse(process.env.PRIVATE_KEY);

export const serviceAccountKey = {
type: process.env.TYPE,
project_id: process.env.PROJECT_ID,
private_key_id: process.env.PRIVATE_KEY_ID,
private_key: private_key,
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,
Expand Down
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"test": "jest",
"dev": "NODE_ENV=development node index.js",
"dev": "NODE_ENV=development node --watch index.js",
"prod": "NODE_ENV=production node index.js"
},
"author": "",
Expand Down
7 changes: 6 additions & 1 deletion server/src/controller/googleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import User from "../models/userModel.js";
import formatData from "../utils/formatData.js";

export const googleAuth = async (req, res) => {
let { access_token } = req.body;
let { access_token, avatar } = req.body;

getAuth()
.verifyIdToken(access_token)
Expand Down Expand Up @@ -38,6 +38,11 @@ export const googleAuth = async (req, res) => {
user = new User({
fullname: name,
email: email,
profile: {
avatar: {
url: avatar,
},
},
roles: "user",
google_auth: true,
});
Expand Down
3 changes: 2 additions & 1 deletion server/src/controller/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ const getMe = async (req, res) => {
const user = await User.findById(id);
res
.status(200)
.json({ _id: user._id, email: user.email, fullname: user.fullname });
.json({ _id: user._id, email: user.email, fullname: user.fullname, avatar: user.profile.avatar.url,
});
} catch (err) {
res.status(500).json({ error: err });
}
Expand Down
3 changes: 2 additions & 1 deletion server/src/utils/formatData.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const formatData = (user) => {
return {
access_token,
email: user.email,
fullname: user.username,
fullname: user.fullname,
avatar: user.profile.avatar.url,
};
};

Expand Down
2 changes: 1 addition & 1 deletion server/src/utils/insertData.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const insertProducts = async () => {

const connect = async () => {
try {
await mongoose.connect("mongodb://localhost:27017/blackmarket");
await mongoose.connect(MONGODB_URI);
console.log("connected to mongodb");

// inserting data into db.
Expand Down

0 comments on commit e410cad

Please sign in to comment.