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 credentials loading after logging out #6938

Merged
merged 7 commits into from
Feb 21, 2023
15 changes: 7 additions & 8 deletions source/lib/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ type Credentials = {
password: string
}

async function loadCredentials(): Promise<SharedWebCredentials> {
async function loadCredentials(): Promise<false | SharedWebCredentials> {
let credentials = await getInternetCredentials(SIS_LOGIN_KEY)
if (credentials === false) {
throw new NoCredentialsError()
}
return credentials
}
drewvolz marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -62,10 +59,11 @@ export function resetCredentials(): Promise<void> {
export async function performLogin(
credentials: Credentials | null = null,
): Promise<Credentials> {
const {username, password} = credentials ?? (await loadCredentials())
if (!username || !password) {
const saved = credentials ?? (await loadCredentials())
if (!saved) {
throw new NoCredentialsError()
}
const {username, password} = saved

let formData = new FormData()
formData.append('username', username)
Expand All @@ -78,15 +76,16 @@ export async function performLogin(
})

let responseUrl = new URL(loginResponse.url)
let responseMessage = responseUrl.searchParams.get('message')
// URLSearchParams.get requires a polyfill in react native
let responseMessage = responseUrl.href.includes('error=')
drewvolz marked this conversation as resolved.
Show resolved Hide resolved
if (responseMessage) {
throw new LoginFailedError(`Login failed: ${responseMessage}`)
}

return {username, password}
}

type QueryFnData = SharedWebCredentials
type QueryFnData = false | SharedWebCredentials
drewvolz marked this conversation as resolved.
Show resolved Hide resolved
type DefaultError = null | unknown
type QueryT<Select> = ReturnType<typeof useCredentials<Select>>

Expand Down