Skip to content

Commit

Permalink
fix(UserProvider): add anonymous user state
Browse files Browse the repository at this point in the history
  • Loading branch information
yohanngab committed Feb 14, 2024
1 parent ee16684 commit 111db01
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions ui/src/common/UserProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React, { useState, useEffect, useContext } from "react";
import React, { useState, useEffect, useCallback, useContext } from "react";
import jwt from "jsonwebtoken";
import { ApiContext } from "./ApiProvider.jsx";

const UserContext = React.createContext([{}, () => {}]);

let initialState = { loading: true, token: null };
const anonymous = { sub: "anonymous", isAnonymous: true, loading: true };

const UserProvider = (props) => {
const [user, setUser] = useState(initialState);
const { httpClient } = useContext(ApiContext);
const [user, setUser] = useState(anonymous);

const verifyUser = async () => {
const verifyUser = useCallback(async () => {
const result = await httpClient._post(`/api/v1/users/refreshToken`);
if (result.success) {
const decodedToken = jwt.decode(result.token);
Expand All @@ -29,11 +29,11 @@ const UserProvider = (props) => {
});
}
setTimeout(verifyUser, 60 * 5 * 1000);
};
}, [setUser]);

useEffect(() => {
verifyUser();
}, []);
}, [verifyUser]);

return <UserContext.Provider value={[user, setUser]}>{props.children}</UserContext.Provider>;
};
Expand Down

0 comments on commit 111db01

Please sign in to comment.