-
Notifications
You must be signed in to change notification settings - Fork 966
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out user setting, cleanup auth API.
- Loading branch information
Showing
6 changed files
with
76 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useContext, useEffect } from 'react'; | ||
import { useRecoilState } from 'recoil'; | ||
import { ChainlitContext } from 'src/context'; | ||
import { userState } from 'src/state'; | ||
|
||
import { useAuthConfig } from './config'; | ||
import { getToken, useTokenManagement } from './token'; | ||
|
||
export const useUser = () => { | ||
const apiClient = useContext(ChainlitContext); | ||
const [user, setUser] = useRecoilState(userState); | ||
const { cookieAuth } = useAuthConfig(); | ||
const { handleSetAccessToken } = useTokenManagement(); | ||
|
||
// Legacy token auth; initialize the token from local storage | ||
const setUserFromLocalStore = () => { | ||
{ | ||
const storedAccessToken = getToken(); | ||
if (storedAccessToken) handleSetAccessToken(storedAccessToken); | ||
} | ||
}; | ||
|
||
// Cookie-based auth; use API to set user. | ||
const setUserFromAPI = async () => { | ||
// Get user from cookie, return true when succesful | ||
try { | ||
const apiUser = await apiClient.getUser(); | ||
if (apiUser) { | ||
setUser(apiUser); | ||
} | ||
} catch (e) { | ||
return; | ||
} | ||
}; | ||
|
||
const initUser = () => { | ||
// Already logged in | ||
if (user) return; | ||
|
||
// Legacy fallback | ||
if (!cookieAuth) return setUserFromLocalStore(); | ||
|
||
// Request user from API | ||
setUserFromAPI(); | ||
}; | ||
|
||
// Attempt to initialise user on app start. | ||
useEffect(initUser, [cookieAuth]); | ||
|
||
return { | ||
user, | ||
setUserFromAPI | ||
}; | ||
}; |