From a1f5355d6a14d11e20c63cc6540c6f268dd9fb05 Mon Sep 17 00:00:00 2001 From: Saad Jutt Date: Tue, 7 Sep 2021 05:26:42 +0500 Subject: [PATCH] chore: fetch username for Redirected-Login and return --- src/SASjs.ts | 4 +-- src/auth/AuthManager.ts | 70 +++++++++++++++++++++------------------ src/types/Login.ts | 8 +++++ src/types/LoginOptions.ts | 3 -- 4 files changed, 48 insertions(+), 37 deletions(-) create mode 100644 src/types/Login.ts delete mode 100644 src/types/LoginOptions.ts diff --git a/src/SASjs.ts b/src/SASjs.ts index 1458a69..9323fa5 100644 --- a/src/SASjs.ts +++ b/src/SASjs.ts @@ -25,7 +25,7 @@ import { Sas9JobExecutor } from './job-execution' import { ErrorResponse } from './types/errors' -import { LoginOptions } from './types/LoginOptions' +import { LoginOptions, LoginReturn } from './types/Login' const defaultConfig: SASjsConfig = { serverUrl: '', @@ -538,7 +538,7 @@ export default class SASjs { username?: string, password?: string, options: LoginOptions = {} - ) { + ): Promise { if (this.sasjsConfig.loginMechanism === LoginMechanism.Default) { if (!username || !password) { throw new Error( diff --git a/src/auth/AuthManager.ts b/src/auth/AuthManager.ts index 914cdbb..441a455 100644 --- a/src/auth/AuthManager.ts +++ b/src/auth/AuthManager.ts @@ -1,6 +1,6 @@ import { ServerType } from '@sasjs/utils/types' import { RequestClient } from '../request/RequestClient' -import { LoginOptions } from '../types/LoginOptions' +import { LoginOptions, LoginReturn } from '../types/Login' import { serialize } from '../utils' import { openWebPage } from './openWebPage' import { verifyingPopUpLoginSAS9 } from './verifyingPopUpLoginSAS9' @@ -28,7 +28,9 @@ export class AuthManager { * Opens Pop up window to SAS Login screen. * And checks if user has finished login process. */ - public async redirectedLogIn({ onLoggedOut }: LoginOptions) { + public async redirectedLogIn({ + onLoggedOut + }: LoginOptions): Promise { const loginPopup = await openWebPage( this.loginPreventRedirectUrl, 'SASLogon', @@ -40,7 +42,7 @@ export class AuthManager { ) if (!loginPopup) { - return { isLoggedIn: false } + return { isLoggedIn: false, userName: '' } } const { isLoggedIn } = @@ -60,10 +62,14 @@ export class AuthManager { ) } + const { userName } = await this.fetchUserName() + await this.loginCallback() + + return { isLoggedIn: true, userName } } - return { isLoggedIn } + return { isLoggedIn: false, userName: '' } } /** @@ -72,13 +78,7 @@ export class AuthManager { * @param password - a string representing the password. * @returns - a boolean `isLoggedin` and a string `username` */ - public async logIn( - username: string, - password: string - ): Promise<{ - isLoggedIn: boolean - userName: string - }> { + public async logIn(username: string, password: string): Promise { const loginParams = { _service: 'default', username, @@ -119,7 +119,7 @@ export class AuthManager { const res = await this.checkSession() isLoggedIn = res.isLoggedIn - if (isLoggedIn) this.userName = res.userName! + if (isLoggedIn) this.userName = res.userName } else { this.userName = loginParams.username } @@ -175,27 +175,10 @@ export class AuthManager { */ public async checkSession(): Promise<{ isLoggedIn: boolean - userName?: string + userName: string loginForm?: any }> { - //For VIYA we will send request on API endpoint. Which is faster then pinging SASJobExecution. - //For SAS9 we will send request on SASStoredProcess - const url = - this.serverType === 'SASVIYA' - ? `${this.serverUrl}/identities/users/@currentUser` - : `${this.serverUrl}/SASStoredProcess` - - const { result: loginResponse } = await this.requestClient - .get(url, undefined, 'text/plain') - .catch((err: any) => { - return { result: 'authErr' } - }) - - const isLoggedIn = loginResponse !== 'authErr' - const userName = isLoggedIn - ? this.extractUserName(loginResponse) - : undefined - + const { isLoggedIn, userName } = await this.fetchUserName() let loginForm = null if (!isLoggedIn) { @@ -214,11 +197,34 @@ export class AuthManager { return Promise.resolve({ isLoggedIn, - userName: userName?.toLowerCase(), + userName: userName.toLowerCase(), loginForm }) } + private async fetchUserName(): Promise<{ + isLoggedIn: boolean + userName: string + }> { + //For VIYA we will send request on API endpoint. Which is faster then pinging SASJobExecution. + //For SAS9 we will send request on SASStoredProcess + const url = + this.serverType === 'SASVIYA' + ? `${this.serverUrl}/identities/users/@currentUser` + : `${this.serverUrl}/SASStoredProcess` + + const { result: loginResponse } = await this.requestClient + .get(url, undefined, 'text/plain') + .catch((err: any) => { + return { result: 'authErr' } + }) + + const isLoggedIn = loginResponse !== 'authErr' + const userName = isLoggedIn ? this.extractUserName(loginResponse) : '' + + return { isLoggedIn, userName } + } + private extractUserName = (response: any): string => { switch (this.serverType) { case ServerType.SasViya: diff --git a/src/types/Login.ts b/src/types/Login.ts new file mode 100644 index 0000000..60d21cb --- /dev/null +++ b/src/types/Login.ts @@ -0,0 +1,8 @@ +export interface LoginOptions { + onLoggedOut?: () => Promise +} + +export interface LoginReturn { + isLoggedIn: boolean + userName: string +} diff --git a/src/types/LoginOptions.ts b/src/types/LoginOptions.ts deleted file mode 100644 index c8e9329..0000000 --- a/src/types/LoginOptions.ts +++ /dev/null @@ -1,3 +0,0 @@ -export interface LoginOptions { - onLoggedOut?: () => Promise -}