-
Notifications
You must be signed in to change notification settings - Fork 401
Added authentication requirement for cart and wishlist functionality #2631
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,12 +4,35 @@ const AuthContext = createContext(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export const AuthProvider = ({ children }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const [isAdmin, setIsAdmin] = useState(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const [isLoggedIn, setIsLoggedIn] = useState(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const [user, setUser] = useState(null); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+7
to
9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Auth state isn’t persisted or initialized from storage; will desync with existing login flow. Login code elsewhere writes to localStorage, but AuthContext never reads it. After a successful login, Apply this diff to initialize from storage on mount: -const AuthContext = createContext();
+const AuthContext = createContext();
@@
-export const AuthProvider = ({ children }) => {
+export const AuthProvider = ({ children }) => {
const [isAdmin, setIsAdmin] = useState(false);
const [isLoggedIn, setIsLoggedIn] = useState(false);
const [user, setUser] = useState(null);
+ React.useEffect(() => {
+ try {
+ const storedLoggedIn = localStorage.getItem("isLoggedIn") === "true";
+ const storedUser = localStorage.getItem("user");
+ const storedAdminRole = localStorage.getItem("adminRole");
+ if (storedLoggedIn) {
+ setIsLoggedIn(true);
+ setUser(storedUser ? JSON.parse(storedUser) : null);
+ }
+ if (storedAdminRole != null) {
+ setIsAdmin(storedAdminRole === "1" || storedAdminRole === "true");
+ }
+ } catch {
+ /* no-op */
+ }
+ }, []); Also update the import to include -import React, { createContext, useState, useContext } from 'react';
+import React, { createContext, useState, useContext, useEffect } from 'react'; 🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const loginAsAdmin = () => setIsAdmin(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const loginAsUser = () => setIsAdmin(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const loginAsAdmin = () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setIsAdmin(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setIsLoggedIn(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const loginAsUser = (userData = null) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setIsAdmin(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setIsLoggedIn(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setUser(userData); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+10
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Login helpers don’t sync storage; context and consumers using localStorage will diverge. Ensure Apply: const loginAsAdmin = () => {
setIsAdmin(true);
setIsLoggedIn(true);
+ try {
+ localStorage.setItem("isLoggedIn", "true");
+ localStorage.setItem("adminRole", "1");
+ } catch {}
};
@@
const loginAsUser = (userData = null) => {
setIsAdmin(false);
setIsLoggedIn(true);
setUser(userData);
+ try {
+ localStorage.setItem("isLoggedIn", "true");
+ localStorage.setItem("user", JSON.stringify(userData));
+ localStorage.removeItem("adminRole");
+ } catch {}
}; 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const logout = () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setIsAdmin(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setIsLoggedIn(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setUser(null); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+7
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainVerify end-to-end: Is Current login screens set localStorage directly and don’t invoke these helpers, leaving context stale during the session. Run: 🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "Calls to login helpers (outside context file):"
rg -nP 'loginAs(User|Admin)\s*\(' -g '!src/context/AuthContext.jsx' -C2 || true
echo
echo "Direct localStorage isLoggedIn usage:"
rg -nP "localStorage\\.getItem\\(['\"]isLoggedIn['\"]\\)" -n -C2 Length of output: 1975 🏁 Script executed: #!/usr/bin/env bash
set -euo pipefail
echo "Locations with localStorage.setItem:"
rg -nP 'localStorage\.setItem\(' -n -C2 Length of output: 6264 Login helpers are unused and context remains stale The Please address by:
This change is necessary to guarantee that the React Context accurately reflects authentication state across your app. 🤖 Prompt for AI Agents
Comment on lines
+21
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logout doesn’t clear storage. Components that read from localStorage will still think the user is logged in. Apply: const logout = () => {
setIsAdmin(false);
setIsLoggedIn(false);
setUser(null);
+ try {
+ localStorage.removeItem("isLoggedIn");
+ localStorage.removeItem("user");
+ localStorage.removeItem("adminRole");
+ } catch {}
}; 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<AuthContext.Provider value={{ isAdmin, loginAsAdmin, loginAsUser }}> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<AuthContext.Provider value={{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
isAdmin, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
isLoggedIn, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
user, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
loginAsAdmin, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
loginAsUser, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
logout | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}}> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{children} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</AuthContext.Provider> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
“Add to Bag” shows success without adding anything.
This misleads users and tests. Either wire Redux add-to-cart or remove the success message until implemented.
Minimal fix to avoid false positives and align UX with ProductGrid toasts:
📝 Committable suggestion
🤖 Prompt for AI Agents