-
Notifications
You must be signed in to change notification settings - Fork 982
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,54 @@ | ||
import { useContext, useEffect } from 'react'; | ||
import { useEffect } from 'react'; | ||
import { useRecoilState } from 'recoil'; | ||
import { ChainlitContext } from 'src/context'; | ||
import { userState } from 'src/state'; | ||
|
||
import { IUser, useApi } from '..'; | ||
import { useAuthConfig } from './config'; | ||
import { getToken, useTokenManagement } from './token'; | ||
|
||
export const useUser = () => { | ||
const apiClient = useContext(ChainlitContext); | ||
console.log('useUser'); | ||
|
||
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 successful | ||
try { | ||
const apiUser = await apiClient.getUser(); | ||
if (apiUser) { | ||
setUser(apiUser); | ||
const { data: userData, mutate: mutateUserData } = useApi<IUser>('/user', { | ||
revalidateOnMount: false | ||
}); | ||
|
||
// Attempt to get user when cookieAuth are available. | ||
useEffect(() => { | ||
if (!user) { | ||
if (cookieAuth) { | ||
console.log('cookieAuth', user, cookieAuth); | ||
mutateUserData(); | ||
return; | ||
} | ||
} catch (_) { | ||
return; | ||
} | ||
}; | ||
|
||
const initUser = () => { | ||
// Already logged in | ||
if (user) return; | ||
// Not using cookie auth, callback to header tokens | ||
console.log('tokenAuth', user, cookieAuth); | ||
const token = getToken(); | ||
if (token) handleSetAccessToken(token); | ||
} | ||
}, [user, cookieAuth]); | ||
|
||
// Legacy fallback | ||
if (!cookieAuth) return setUserFromLocalStore(); | ||
// When user data is available, set the user object. | ||
useEffect(() => { | ||
console.log('userData effect'); | ||
|
||
// Request user from API | ||
setUserFromAPI(); | ||
}; | ||
if (userData) { | ||
console.log('setUser', userData); | ||
setUser(userData); | ||
} | ||
}, [userData]); | ||
|
||
// Attempt to initialize user on app start. | ||
useEffect(initUser, [cookieAuth]); | ||
useEffect(() => { | ||
console.log('useUser effect'); | ||
}); | ||
|
||
return { | ||
user, | ||
setUserFromAPI | ||
setUserFromAPI: mutateUserData | ||
}; | ||
}; |
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