Skip to content

Commit

Permalink
Separate user not logged in and unknown state
Browse files Browse the repository at this point in the history
  • Loading branch information
gf-rog committed Nov 23, 2023
1 parent 84e68d5 commit d71d7da
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions frontend/src/components/LoginBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ function LoginBox() {
};

useEffect(() => {
if (userId === null) return;
if (userId === undefined) return;

if (userId === "") {
if (userId === null) {
setLoginMsg("Bad credentials");
} else {
if (window.history.state && window.history.state.idx > 0) {
Expand Down
22 changes: 13 additions & 9 deletions frontend/src/helpers/UserProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { fetchData } from "../services/fetchData";
import User from "../models/user.model";

export interface UserContextValue {
userId: string | null;
user: User | null;
setUser: React.Dispatch<React.SetStateAction<User | null>>;
userId: string | null | undefined;
user: User | null | undefined;
setUser: React.Dispatch<React.SetStateAction<User | null | undefined>>;
login: (mail: string, password: string) => Promise<void>;
logout: () => Promise<boolean>;
updateUser: () => Promise<boolean>;
Expand All @@ -32,8 +32,12 @@ function useUser() {
}

function UserProvider({ children }: { children: React.ReactNode }) {
const [userId, setUserId] = useState<string | null>(null);
const [user, setUser] = useState<User | null>(null);
// userId:
// undefined -> user state is loading
// null -> user not logged in
// string -> user logged in, userID correct
const [userId, setUserId] = useState<string | null | undefined>(undefined);
const [user, setUser] = useState<User | null | undefined>(null);
const [token, setToken] = useState<object | null>(null);
const firstRefresh = useRef(true);

Expand Down Expand Up @@ -69,7 +73,7 @@ function UserProvider({ children }: { children: React.ReactNode }) {
}
}

setUserId("");
setUserId(null);
};

const login = async (mail: string, password: string) => {
Expand All @@ -86,7 +90,7 @@ function UserProvider({ children }: { children: React.ReactNode }) {
});

if (response.status != "ok") {
setUserId("");
setUserId(null);
return;
}

Expand Down Expand Up @@ -129,6 +133,7 @@ function UserProvider({ children }: { children: React.ReactNode }) {

const response = await fetchData(`/users/${user.id}`, "DELETE");
if (response.status === "ok") {
setUserId(null);
setUser(null);
return true;
}
Expand All @@ -141,14 +146,13 @@ function UserProvider({ children }: { children: React.ReactNode }) {
if (token) {
const newUserId = (token as any).userId;
fetchData(`/users/${newUserId}`, "GET").then((response) => {
setUser(response.user as any);
setUserId(newUserId);
setUser(response.user as any);
});
}
}, [token]);

if (firstRefresh.current) {
console.log("FIRST REFRESH");
firstRefresh.current = false;
getAccessToken();
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function ProfilePage() {
};

useEffect(() => {
if (userId === "") navigate("/login");
if (userId === null) navigate("/login");
}, [userId]);

return (
Expand Down

0 comments on commit d71d7da

Please sign in to comment.