From c5bec5f9eee06fd1c47b7993ff9aab3d10771276 Mon Sep 17 00:00:00 2001 From: Kajetan Kazimierczak Date: Mon, 11 Apr 2022 22:20:22 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Fulfix.=20Login=20funkar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/login.component.tsx | 25 ++++++----- .../hooks/useSettingsStorage.tsx | 6 ++- libs/api-skolplattformen/lib/api.ts | 44 +++++++++---------- .../lib/frejaLoginStatusChecker.ts | 2 +- libs/api/lib/loginStatus.ts | 2 +- 5 files changed, 41 insertions(+), 38 deletions(-) diff --git a/apps/skolplattformen-app/components/login.component.tsx b/apps/skolplattformen-app/components/login.component.tsx index 721ab7f8..c6c9eb7f 100644 --- a/apps/skolplattformen-app/components/login.component.tsx +++ b/apps/skolplattformen-app/components/login.component.tsx @@ -54,7 +54,6 @@ const LoginMethods: Logins = { BANKID_SAME_DEVICE: 0, BANKID_ANOTHER_DEVICE: 2, TEST_USER: 3, - } export const Login = () => { @@ -122,11 +121,11 @@ export const Login = () => { } } - const openFreja = (token:string) => { + const openFreja = (token: string) => { try { - const originAppScheme = encodeURIComponent(schema); + const originAppScheme = encodeURIComponent(schema) const frejaUrl = - Platform.OS === 'ios' + Platform.OS === 'ios' ? `${token}&originAppScheme=${originAppScheme}` : `${token}&originAppScheme=${originAppScheme}` Linking.openURL(frejaUrl) @@ -140,22 +139,24 @@ export const Login = () => { (loginMethodId === 'thisdevice' && !loginBankIdSameDeviceWithoutId) const startLogin = async (text: string) => { - - if(loginMethodId === 'freja'){ + if (loginMethodId === 'freja') { showModal(true) - const status = await api.loginFreja(); + const status = await api.loginFreja() setCancelLoginRequest(() => () => status.cancel()) openFreja(status.token) status.on('STARTED', () => console.log('Freja eID app not yet opened')) - status.on('DELIVERED_TO_MOBILE', () => console.log('Freja eID app is open')) - status.on('CANCELLED', () => { + status.on('DELIVERED_TO_MOBILE', () => + console.log('Freja eID app is open') + ) + status.on('CANCELLED', () => { console.log('User pressed cancel in Freja eID') showModal(false) }) status.on('APPROVED', () => console.log('Freja eID ok')) - } - - else if (loginMethodId === 'thisdevice' || loginMethodId === 'otherdevice') { + } else if ( + loginMethodId === 'thisdevice' || + loginMethodId === 'otherdevice' + ) { showModal(true) let ssn diff --git a/apps/skolplattformen-app/hooks/useSettingsStorage.tsx b/apps/skolplattformen-app/hooks/useSettingsStorage.tsx index 4c72aaf8..01071fb1 100644 --- a/apps/skolplattformen-app/hooks/useSettingsStorage.tsx +++ b/apps/skolplattformen-app/hooks/useSettingsStorage.tsx @@ -7,7 +7,11 @@ export type ChildPersonalNumbers = Record export const settingsState = proxy({ hydrated: false, settings: { - loginMethodId: 'thisdevice' as 'thisdevice' | 'otherdevice' | 'testuser' | 'freja', + loginMethodId: 'thisdevice' as + | 'thisdevice' + | 'otherdevice' + | 'testuser' + | 'freja', usingSystemTheme: true, theme: 'light', cachedPersonalIdentityNumber: '', diff --git a/libs/api-skolplattformen/lib/api.ts b/libs/api-skolplattformen/lib/api.ts index 910cb51d..ec120f56 100644 --- a/libs/api-skolplattformen/lib/api.ts +++ b/libs/api-skolplattformen/lib/api.ts @@ -201,10 +201,7 @@ export class ApiSkolplattformen extends EventEmitter implements Api { this.isLoggedIn = true this.emit('login') }) - // status.on('ERROR', () => { - // this.personalNumber = undefined - // }) - + return status } @@ -244,27 +241,28 @@ export class ApiSkolplattformen extends EventEmitter implements Api { } private async retrieveFrejaSessionCookie(): Promise { - const url = routes.frejaLoginCookie - const session = await this.getSession(url, { - redirect: 'manual', - }) - //const session = this.getRequestInit() - - -console.log(JSON.stringify(session)) - - const response = await this.fetch('freja-login-return-url', url, session) - console.log(response.status) - console.log(response.text()) - - console.log(JSON.stringify(response)) - - const response2 = await this.fetch('freja-login-cookie', url, session) - console.log(response2.status) - console.log(response2.text()) + + try{ + const url = routes.frejaReturnUrl + const session = await this.getSession(url, { + redirect: 'manual', + }) + await this.fetch('freja-login-return-url', url, session) + } catch(error){ + console.log(JSON.stringify(error)) + } + + try{ + const url2 = routes.frejaLoginCookie + const session = await this.getSession(url2, { + redirect: 'manual', + }) + await this.fetch('freja-login-cookie', url2, session) + } catch(error2){ + console.log(JSON.stringify(error2)) + } } - private async retrieveXsrfToken(): Promise { const url = routes.hemPage const session = this.getRequestInit() diff --git a/libs/api-skolplattformen/lib/frejaLoginStatusChecker.ts b/libs/api-skolplattformen/lib/frejaLoginStatusChecker.ts index 9ba18e0f..6d579e3d 100644 --- a/libs/api-skolplattformen/lib/frejaLoginStatusChecker.ts +++ b/libs/api-skolplattformen/lib/frejaLoginStatusChecker.ts @@ -25,7 +25,7 @@ export class FrejaChecker extends EventEmitter implements FrejaLoginStatusChecke this.emit(status); if (!this.cancelled && status !== 'APPROVED' && - // status !== 'ERROR!' && + status !== 'ERROR' && status !== 'CANCELLED' ){ setTimeout(() => this.check(), 1000); diff --git a/libs/api/lib/loginStatus.ts b/libs/api/lib/loginStatus.ts index a07038a0..1fbce3f5 100644 --- a/libs/api/lib/loginStatus.ts +++ b/libs/api/lib/loginStatus.ts @@ -19,7 +19,7 @@ export interface LoginStatusChecker { export interface FrejaLoginStatusChecker { token: string on: ( - event: 'APPROVED' | 'STARTED' | 'DELIVERED_TO_MOBILE' | 'CANCELLED', + event: 'APPROVED' | 'STARTED' | 'UNKNOWN' | 'DELIVERED_TO_MOBILE' | 'CANCELLED' | 'EXPIRED' | 'RP_CANCELED' | 'ERROR', listener: (...args: any[]) => void ) => FrejaLoginStatusChecker cancel: () => Promise