-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: clean auth hooks * fix: don't call /user if no auth is configured
- Loading branch information
1 parent
cefc8d9
commit 79437c3
Showing
13 changed files
with
182 additions
and
204 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useEffect } from 'react'; | ||
import { IAuthConfig } from 'src/index'; | ||
|
||
import { useApi } from '../api'; | ||
import { useAuthState } from './state'; | ||
|
||
export const useAuthConfig = () => { | ||
const { authConfig, setAuthConfig, cookieAuth } = useAuthState(); | ||
const { data: authConfigData, isLoading } = useApi<IAuthConfig>( | ||
authConfig ? null : '/auth/config' | ||
); | ||
|
||
useEffect(() => { | ||
if (authConfigData) { | ||
setAuthConfig(authConfigData); | ||
} | ||
}, [authConfigData, setAuthConfig]); | ||
|
||
return { authConfig, isLoading, cookieAuth }; | ||
}; |
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,27 @@ | ||
import { useContext } from 'react'; | ||
import { ChainlitContext } from 'src/index'; | ||
|
||
import { useAuthState } from './state'; | ||
import { useTokenManagement } from './tokenManagement'; | ||
|
||
export const useSessionManagement = () => { | ||
const apiClient = useContext(ChainlitContext); | ||
const { setUser, setThreadHistory, cookieAuth } = useAuthState(); | ||
const { removeToken } = useTokenManagement(); | ||
|
||
const logout = async (reload = false): Promise<void> => { | ||
await apiClient.logout(); | ||
setUser(null); | ||
setThreadHistory(undefined); | ||
|
||
if (!cookieAuth) { | ||
removeToken(); | ||
} | ||
|
||
if (reload) { | ||
window.location.reload(); | ||
} | ||
}; | ||
|
||
return { logout }; | ||
}; |
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,25 @@ | ||
import { useRecoilState, useSetRecoilState } from 'recoil'; | ||
import { | ||
accessTokenState, | ||
authState, | ||
threadHistoryState, | ||
userState | ||
} from 'src/state'; | ||
|
||
export const useAuthState = () => { | ||
const [authConfig, setAuthConfig] = useRecoilState(authState); | ||
const [user, setUser] = useRecoilState(userState); | ||
const [accessToken, setAccessToken] = useRecoilState(accessTokenState); | ||
const setThreadHistory = useSetRecoilState(threadHistoryState); | ||
|
||
return { | ||
authConfig, | ||
setAuthConfig, | ||
user, | ||
setUser, | ||
accessToken, | ||
setAccessToken, | ||
setThreadHistory, | ||
cookieAuth: authConfig?.cookieAuth !== false && authConfig?.requireLogin | ||
}; | ||
}; |
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,53 @@ | ||
import { useAuthState } from './state'; | ||
|
||
const tokenKey = 'token'; | ||
|
||
export function getToken(): string | null | undefined { | ||
try { | ||
return localStorage.getItem(tokenKey); | ||
} catch (_) { | ||
return; | ||
} | ||
} | ||
|
||
export function setToken(token: string): void { | ||
try { | ||
return localStorage.setItem(tokenKey, token); | ||
} catch (_) { | ||
return; | ||
} | ||
} | ||
|
||
export function removeToken(): void { | ||
try { | ||
return localStorage.removeItem(tokenKey); | ||
} catch (_) { | ||
return; | ||
} | ||
} | ||
|
||
export function ensureTokenPrefix(token: string): string { | ||
const prefix = 'Bearer '; | ||
if (token.startsWith(prefix)) { | ||
return token; | ||
} else { | ||
return prefix + token; | ||
} | ||
} | ||
|
||
export const useTokenManagement = () => { | ||
const { setAccessToken, cookieAuth } = useAuthState(); | ||
|
||
const handleSetAccessToken = (token: string) => { | ||
if (!cookieAuth) { | ||
localStorage.setItem('accessToken', token); | ||
setAccessToken(token); | ||
} | ||
}; | ||
|
||
const removeToken = () => { | ||
localStorage.removeItem('accessToken'); | ||
}; | ||
|
||
return { handleSetAccessToken, removeToken }; | ||
}; |
File renamed without changes.
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,34 @@ | ||
import { useEffect } from 'react'; | ||
import { IUser } from 'src/types'; | ||
|
||
import { useApi } from '../api'; | ||
import { useAuthConfig } from './config'; | ||
import { useAuthState } from './state'; | ||
import { useTokenManagement } from './tokenManagement'; | ||
|
||
export const useUserManagement = () => { | ||
const { user, setUser, cookieAuth } = useAuthState(); | ||
const { handleSetAccessToken } = useTokenManagement(); | ||
const { isLoading: authConfigLoading } = useAuthConfig(); | ||
|
||
const { data: userData, mutate: setUserFromAPI } = useApi<IUser>( | ||
cookieAuth ? '/user' : null | ||
); | ||
|
||
useEffect(() => { | ||
if (userData) { | ||
setUser(userData); | ||
} | ||
}, [userData, setUser]); | ||
|
||
useEffect(() => { | ||
if (!(user && authConfigLoading && cookieAuth)) { | ||
const token = localStorage.getItem('accessToken'); | ||
if (token) { | ||
handleSetAccessToken(token); | ||
} | ||
} | ||
}, [user, cookieAuth, authConfigLoading]); | ||
|
||
return { user, setUserFromAPI }; | ||
}; |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.