Skip to content

Commit

Permalink
add user info to state
Browse files Browse the repository at this point in the history
- redirect to login endpoint if necessary
  • Loading branch information
cgawron committed Mar 13, 2024
1 parent c55a8d9 commit 2cc7948
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/chat/context/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,33 @@ export const ChatProvider = ({ children }) => {
localStorage.setItem("SESSIONS", JSON.stringify(stateToSave));
}, [latestState.current]);

// get user
useEffect(() => {
fetch("https://login.ki.fh-swf.de/openai/api/dashboard")
.catch(err => {
console.log("getting user: ", err);
window.location.href = "https://login.ki.fh-swf.de/openai/api/login";
})
.then(res => {
console.log("getting user: ", res.status);
if (res.status === 401) {
window.location.href = "https://login.ki.fh-swf.de/openai/api/login";
}
return res.json()
})
.then(data => {
const user = {
name: data.name,
preferred_userame: data.preferred_username,
email: data.email,
sub: data.sub
}
localStorage.setItem("USER", JSON.stringify(user));
dispatch({ type: "SET_STATE", payload: { user } });
})

}, [latestState.current.user])

return (<>
<ChatContext.Provider value={{ ...state, ...actionList }}>
<MessagesContext.Provider value={dispatch}>
Expand Down
1 change: 1 addition & 0 deletions src/chat/context/initState.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const initState = {
apps: true,
},
typeingMessage: {},
user: null,
version: "0.1.0",
cotent: "",
};

0 comments on commit 2cc7948

Please sign in to comment.