Skip to content
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

fix(ui): clear access token on unauthorized response #2284

Merged
merged 2 commits into from
May 29, 2024
Merged
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
9 changes: 4 additions & 5 deletions ee/tabby-ui/lib/tabby/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const logoutAllSessionsMutation = graphql(/* GraphQL */ `
const AuthProvider: React.FunctionComponent<AuthProviderProps> = ({
children
}) => {
const initialized = React.useRef(false)
const [initialized, setInitialized] = React.useState(false)
const [authToken] = useLocalStorage<AuthData | undefined>(
AUTH_TOKEN_KEY,
undefined
Expand All @@ -127,21 +127,20 @@ const AuthProvider: React.FunctionComponent<AuthProviderProps> = ({
})

React.useEffect(() => {
initialized.current = true
if (authToken?.accessToken && authToken?.refreshToken) {
dispatch({ type: AuthActionType.SignIn, data: authToken })
} else {
dispatch({ type: AuthActionType.SignOut })
}
setInitialized(true)
}, [])

React.useEffect(() => {
if (!initialized.current) return

if (!initialized) return
// After being mounted, listen for changes in the access token
if (authToken?.accessToken && authToken?.refreshToken) {
dispatch({ type: AuthActionType.Refresh, data: authToken })
} else {
} else if (!authToken?.accessToken && !authToken?.refreshToken) {
dispatch({ type: AuthActionType.SignOut })
}
}, [authToken])
Expand Down
2 changes: 2 additions & 0 deletions ee/tabby-ui/lib/tabby/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export default async function authEnhancedFetch(
)

if (response.status === 401) {
tokenManager.clearAccessToken()

return tokenManager.refreshToken(doRefreshToken).then(res => {
return requestWithAuth(url, options)
})
Expand Down
8 changes: 7 additions & 1 deletion ee/tabby-ui/lib/tabby/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,14 @@ const client = new Client({
})
},
didAuthError(error, _operation) {
return error.graphQLErrors.some(
const isUnauthorized = error.graphQLErrors.some(
e => e?.extensions?.code === 'UNAUTHORIZED'
)
if (isUnauthorized) {
tokenManager.clearAccessToken()
}

return isUnauthorized
},
willAuthError(operation) {
// Sync tokens on every operation
Expand Down Expand Up @@ -231,6 +236,7 @@ const client = new Client({
return true
}
} else {
tokenManager.clearAccessToken()
return true
}
},
Expand Down
16 changes: 15 additions & 1 deletion ee/tabby-ui/lib/tabby/token-management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ const isTokenRecentlyIssued = (iat: number | undefined): boolean => {
}

class TokenManager {
clearAccessToken() {
let authToken = getAuthToken()
if (authToken) {
// remove accessToken only, keep the refreshToken
saveAuthToken({
...authToken,
accessToken: ''
})
}
}

async refreshToken(doRefreshToken: () => Promise<AuthData | undefined>) {
try {
if (typeof navigator?.locks === 'undefined') {
Expand All @@ -59,7 +70,8 @@ class TokenManager {

await navigator.locks.request(AUTH_LOCK_KEY, async () => {
const authToken = getAuthToken()
const accessToken = getAuthToken()?.accessToken
const accessToken = authToken?.accessToken
const refreshToken = authToken?.refreshToken

let newAuthToken: AuthData | undefined

Expand All @@ -70,6 +82,8 @@ class TokenManager {
} else {
newAuthToken = await doRefreshToken()
}
} else if (refreshToken) {
newAuthToken = await doRefreshToken()
}

if (newAuthToken) {
Expand Down