mirror of
https://github.com/modrinth/code.git
synced 2026-07-31 21:26:40 +00:00
only refresh tokens and clear auth cookies on auth fails, not any random error
This commit is contained in:
@@ -30,6 +30,29 @@ const normalizeAuthToken = (value: unknown) => {
|
|||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getErrorStatus = (error: unknown): number | undefined => {
|
||||||
|
if (!error || typeof error !== 'object') {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
const typedError = error as { statusCode?: unknown; status?: unknown }
|
||||||
|
const status = typedError.statusCode ?? typedError.status
|
||||||
|
|
||||||
|
return typeof status === 'number' ? status : undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
// only when labrinth actually gives us an auth error
|
||||||
|
const isAuthFailure = (error: unknown): boolean => {
|
||||||
|
const status = getErrorStatus(error)
|
||||||
|
return status === 401 || status === 403
|
||||||
|
}
|
||||||
|
|
||||||
|
const clearAuthCookie = (auth: AuthState, authCookie: { value: string | null }) => {
|
||||||
|
authCookie.value = null
|
||||||
|
auth.token = ''
|
||||||
|
auth.user = null
|
||||||
|
}
|
||||||
|
|
||||||
const getQueryString = (value: QueryValue) => {
|
const getQueryString = (value: QueryValue) => {
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
return value[0] ?? null
|
return value[0] ?? null
|
||||||
@@ -90,6 +113,7 @@ export const initAuth = async (oldToken: string | null | undefined = null) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const tokenStr = normalizeAuthToken(authCookie.value)
|
const tokenStr = normalizeAuthToken(authCookie.value)
|
||||||
|
let shouldRefresh = false
|
||||||
|
|
||||||
if (authCookie.value != null && tokenStr === '') {
|
if (authCookie.value != null && tokenStr === '') {
|
||||||
authCookie.value = null
|
authCookie.value = null
|
||||||
@@ -111,12 +135,13 @@ export const initAuth = async (oldToken: string | null | undefined = null) => {
|
|||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
)) as Labrinth.Users.v2.User
|
)) as Labrinth.Users.v2.User
|
||||||
} catch {
|
} catch (error) {
|
||||||
/* empty */
|
// only refresh when the token was rejected. not on timeouts or other errors (think this was the cause of random logouts)
|
||||||
|
shouldRefresh = isAuthFailure(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!auth.user && auth.token) {
|
if (!auth.user && auth.token && shouldRefresh) {
|
||||||
try {
|
try {
|
||||||
const session = (await useBaseFetch(
|
const session = (await useBaseFetch(
|
||||||
'session/refresh',
|
'session/refresh',
|
||||||
@@ -132,22 +157,29 @@ export const initAuth = async (oldToken: string | null | undefined = null) => {
|
|||||||
auth.token = normalizeAuthToken(session.session)
|
auth.token = normalizeAuthToken(session.session)
|
||||||
if (auth.token) {
|
if (auth.token) {
|
||||||
authCookie.value = auth.token
|
authCookie.value = auth.token
|
||||||
auth.user = (await useBaseFetch(
|
try {
|
||||||
'user',
|
auth.user = (await useBaseFetch(
|
||||||
{
|
'user',
|
||||||
apiVersion: 3,
|
{
|
||||||
headers: {
|
apiVersion: 3,
|
||||||
Authorization: auth.token,
|
headers: {
|
||||||
|
Authorization: auth.token,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
true,
|
||||||
true,
|
)) as Labrinth.Users.v2.User
|
||||||
)) as Labrinth.Users.v2.User
|
} catch (error) {
|
||||||
|
if (isAuthFailure(error)) {
|
||||||
|
clearAuthCookie(auth, authCookie)
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
authCookie.value = null
|
clearAuthCookie(auth, authCookie)
|
||||||
auth.token = ''
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (isAuthFailure(error)) {
|
||||||
|
clearAuthCookie(auth, authCookie)
|
||||||
}
|
}
|
||||||
} catch {
|
|
||||||
authCookie.value = null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user