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
Merged
Show file tree
Hide file tree
Changes from 5 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
21 changes: 10 additions & 11 deletions source/lib/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ type Credentials = {
password: string
}

async function loadCredentials(): Promise<SharedWebCredentials> {
async function loadCredentials(): Promise<null | SharedWebCredentials> {
let credentials = await getInternetCredentials(SIS_LOGIN_KEY)
if (credentials === false) {
throw new NoCredentialsError()
}
return credentials
return credentials ? credentials : null
}

export async function storeCredentials(
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 = null | SharedWebCredentials
type DefaultError = null | unknown
type QueryT<Select> = ReturnType<typeof useCredentials<Select>>

Expand All @@ -104,10 +103,10 @@ export function useCredentials<TData = QueryFnData, TError = DefaultError>(
}

export function useUsername(): QueryT<
false | Pick<SharedWebCredentials, 'username'>
null | Pick<SharedWebCredentials, 'username'>
> {
return useCredentials({
select: (data) => (data ? {username: data.username} : false),
select: (data) => (data ? {username: data.username} : null),
drewvolz marked this conversation as resolved.
Show resolved Hide resolved
})
}

Expand Down
7 changes: 4 additions & 3 deletions source/views/sis/balances.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import {BalancesShapeType, useBalances} from '../../lib/financials'
import * as c from '@frogpond/colors'
import {sto} from '../../lib/colors'
import {useNavigation} from '@react-navigation/native'
import {NoCredentialsError, useCredentials} from '../../lib/login'
import {NoCredentialsError, useUsername} from '../../lib/login'

const DISCLAIMER = 'This data may be outdated or otherwise inaccurate.'

export const BalancesView = (): JSX.Element => {
let navigation = useNavigation()
let credentials = useCredentials()
let {data: usernameData} = useUsername()
drewvolz marked this conversation as resolved.
Show resolved Hide resolved
let username = usernameData ? usernameData.username : ''
drewvolz marked this conversation as resolved.
Show resolved Hide resolved

let {
data = {} as BalancesShapeType,
Expand All @@ -28,7 +29,7 @@ export const BalancesView = (): JSX.Element => {
isInitialLoading,
refetch,
isRefetching,
} = useBalances(credentials.data?.username)
} = useBalances(username)

let openSettings = () => navigation.navigate('Settings')
let refresh = <RefreshControl onRefresh={refetch} refreshing={isRefetching} />
Expand Down