71 lines
1.7 KiB
TypeScript
71 lines
1.7 KiB
TypeScript
import { Client } from 'visions-client'
|
|
|
|
export type AuthState =
|
|
| { type: 'unauthed' }
|
|
| { type: 'authed'; sessionId: string }
|
|
|
|
export type State = {
|
|
auth: AuthState
|
|
}
|
|
|
|
export const initialState = (): State => ({
|
|
auth: { type: 'unauthed' },
|
|
})
|
|
|
|
export const sessionId = (state: State): string | undefined => {
|
|
if (state.auth.type === 'authed') {
|
|
return state.auth.sessionId
|
|
} else {
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
export type Action = { type: 'set-auth'; content: AuthState }
|
|
|
|
export const reducer = (state: State, action: Action) => {
|
|
switch (action.type) {
|
|
case 'set-auth': {
|
|
return { ...state, auth: action.content }
|
|
}
|
|
default: {
|
|
return state
|
|
}
|
|
}
|
|
}
|
|
|
|
export class Controller {
|
|
client: Client
|
|
state: State
|
|
|
|
constructor(
|
|
client: Client,
|
|
state: State,
|
|
) {
|
|
this.client = client
|
|
this.state = state
|
|
}
|
|
|
|
// On any request, there are four options.
|
|
// The request succeeds. No problem.
|
|
// The request succeeds, but the user needs to reset their password.
|
|
// The action fails.
|
|
// The HTTP request itself fails.
|
|
async auth(username: string, password: string) {
|
|
let response = await this.client.auth(username, password)
|
|
switch (response.status) {
|
|
case 'ok': {
|
|
/*
|
|
this.dispatch({
|
|
type: 'set-auth',
|
|
content: {
|
|
type: 'authed',
|
|
sessionId: response.content.content,
|
|
},
|
|
})
|
|
*/
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|