Skip to content

feat: add logInSilent method to AuthContext #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const AuthContext = createContext<IAuthContext>({
token: '',
login: () => null,
logIn: () => null,
logInSilent: () => null,
logOut: () => null,
error: null,
loginInProgress: false,
Expand Down Expand Up @@ -106,6 +107,39 @@ export const AuthProvider = ({ authConfig, children }: IAuthProvider) => {
})
}

function logInSilent(initial: boolean = false) {
setToken('')
setTokenExpire(epochAtSecondsFromNow(FALLBACK_EXPIRE_TIME))
setIdToken(undefined)
setLoginInProgress(true)
if (refreshToken) {
fetchWithRefreshToken({ config, refreshToken })
.then((result: TTokenResponse) => handleTokenResponse(result))
.catch((error: unknown) => {
if (error instanceof FetchError) {
// If the fetch failed with status 400, assume expired refresh token
if (error.status === 400) {
handleExpiredRefreshToken(initial)
return
}
// Unknown error. Set error, and log in if first page load
console.error(error)
setError(error.message)
if (initial) logIn(undefined, undefined, config.loginMethod)
}
// Unknown error. Set error, and log in if first page load
else if (error instanceof Error) {
console.error(error)
setError(error.message)
if (initial) logIn(undefined, undefined, config.loginMethod)
}
})
.finally(() => {
setLoginInProgress(false)
})
}
}

function handleTokenResponse(response: TTokenResponse) {
setToken(response.access_token)
if (response.id_token) {
Expand Down Expand Up @@ -269,6 +303,7 @@ export const AuthProvider = ({ authConfig, children }: IAuthProvider) => {
login: logIn,
logIn,
logOut,
logInSilent,
error,
loginInProgress,
}}
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ export interface IAuthProvider {
}

type TLogInFunction = (state?: string, additionalParameters?: TPrimitiveRecord, method?: TLoginMethod) => void
type TLogInSilentFunction = (initial?: boolean) => void
export interface IAuthContext {
token: string
logIn: TLogInFunction
logOut: (state?: string, logoutHint?: string, additionalParameters?: TPrimitiveRecord) => void
/** @deprecated Use `logIn` instead */
login: TLogInFunction
logInSilent: TLogInSilentFunction
error: string | null
tokenData?: TTokenData
idToken?: string
Expand Down
Loading