From bb2ce39e6beb9f9ab42d018684d6635756c4a0f5 Mon Sep 17 00:00:00 2001 From: richard-loafle <44828666+richard-loafle@users.noreply.github.com> Date: Tue, 7 Apr 2020 14:23:34 +0900 Subject: [PATCH] bug fixed --- src/app/guards/app-authentication.guard.ts | 60 +++++++++++++- .../resolvers/app-authentication.resolver.ts | 22 ++--- .../components/login.section.component.ts | 18 ++--- .../services/app-authentication.service.ts | 80 +++++++++++++------ src/app/services/app.service.ts | 15 ++-- src/app/store/authentication/effects.ts | 10 +-- tslint.json | 56 +++---------- 7 files changed, 152 insertions(+), 109 deletions(-) diff --git a/src/app/guards/app-authentication.guard.ts b/src/app/guards/app-authentication.guard.ts index a98109a..26f5eb7 100644 --- a/src/app/guards/app-authentication.guard.ts +++ b/src/app/guards/app-authentication.guard.ts @@ -1,4 +1,5 @@ import { Observable } from 'rxjs'; +import { take } from 'rxjs/operators'; import { Injectable } from '@angular/core'; import { @@ -9,6 +10,12 @@ import { Router } from '@angular/router'; +import { Store } from '@ngrx/store'; + +import { PiService } from '@ucap/ng-pi'; + +import { LoginActions } from '@ucap/ng-store-authentication'; + import { AppAuthenticationService } from '@app/services/app-authentication.service'; @Injectable({ @@ -16,7 +23,9 @@ import { AppAuthenticationService } from '@app/services/app-authentication.servi }) export class AppAuthenticationGuard implements CanActivate { constructor( + private piService: PiService, private appAuthenticationService: AppAuthenticationService, + private store: Store, private router: Router ) {} @@ -32,8 +41,55 @@ export class AppAuthenticationGuard implements CanActivate { if (this.appAuthenticationService.loggedIn()) { resolve(true); } else { - this.router.navigateByUrl('/account/login'); - resolve(false); + const userStore = this.appAuthenticationService.useAutoLogin(); + if (!!userStore) { + const loginSession = this.appAuthenticationService.loginSession; + + const onWebLoginFailure = (error: any) => { + userStore.settings.general.autoLogin = false; + this.appAuthenticationService.userStore = userStore; + + this.router.navigateByUrl('/account/login'); + resolve(false); + }; + + this.piService + .login2({ + companyCode: userStore.companyCode, + loginId: userStore.loginId, + loginPw: userStore.loginPw, + deviceType: loginSession.deviceType + }) + .pipe(take(1)) + .subscribe( + (res) => { + if ('success' !== res.status.toLowerCase()) { + onWebLoginFailure(res.status); + return; + } else { + this.store.dispatch( + LoginActions.webLoginSuccess({ + companyCode: userStore.companyCode, + loginId: userStore.loginId, + loginPw: userStore.loginPw, + autoLogin: true, + rememberMe: userStore.rememberMe, + login2Response: res + }) + ); + resolve(true); + return; + } + }, + (error) => { + onWebLoginFailure(error); + }, + () => {} + ); + } else { + this.router.navigateByUrl('/account/login'); + resolve(false); + } } }); } diff --git a/src/app/resolvers/app-authentication.resolver.ts b/src/app/resolvers/app-authentication.resolver.ts index a182a74..8be895e 100644 --- a/src/app/resolvers/app-authentication.resolver.ts +++ b/src/app/resolvers/app-authentication.resolver.ts @@ -14,8 +14,8 @@ import { StatusCode } from '@ucap/api'; import { NativeService } from '@ucap/native'; import { SSOMode } from '@ucap/protocol-authentication'; +import { LogService } from '@ucap/ng-logger'; import { UCAP_NATIVE_SERVICE } from '@ucap/ng-native'; -import { SessionStorageService } from '@ucap/ng-web-storage'; import { PublicApiService } from '@ucap/ng-api-public'; import { ExternalApiService } from '@ucap/ng-api-external'; import { ProtocolService } from '@ucap/ng-protocol'; @@ -28,11 +28,7 @@ import { LoginActions } from '@ucap/ng-store-authentication'; -import { LoginSession } from '@app/models/login-session'; -import { AppKey } from '@app/types/app-key.type'; - -import { AppActions } from '@app/store/actions'; -import { LogService } from '@ucap/ng-logger'; +import { AppAuthenticationService } from '@app/services/app-authentication.service'; @Injectable() export class AppAuthenticationResolver implements Resolve { @@ -43,7 +39,7 @@ export class AppAuthenticationResolver implements Resolve { private innerProtocolService: InnerProtocolService, private authenticationProtocolService: AuthenticationProtocolService, private store: Store, - private sessionStorageService: SessionStorageService, + private appAuthenticationService: AppAuthenticationService, private logService: LogService, @Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService ) {} @@ -54,9 +50,7 @@ export class AppAuthenticationResolver implements Resolve { ): void | Observable | Promise { return new Promise(async (resolve, reject) => { try { - const loginSession = this.sessionStorageService.get( - AppKey.LoginSession - ); + const loginSession = this.appAuthenticationService.loginSession; const networkInfo = await this.nativeService.getNetworkInfo(); const localIp = !!networkInfo && 0 < networkInfo.length && !!networkInfo[0].ip @@ -139,7 +133,7 @@ export class AppAuthenticationResolver implements Resolve { concatMap(() => { return this.innerProtocolService.conn({}).pipe( take(1), - concatMap(connRes => { + concatMap((connRes) => { return this.authenticationProtocolService .login({ loginId: loginSession.loginId, @@ -166,7 +160,7 @@ export class AppAuthenticationResolver implements Resolve { }) ) .subscribe( - loginRes => { + (loginRes) => { this.store.dispatch( LoginActions.loginSuccess({ res: loginRes, @@ -175,12 +169,12 @@ export class AppAuthenticationResolver implements Resolve { ); resolve(); }, - error => { + (error) => { reject(error); } ); }, - error => { + (error) => { reject(error); } ); diff --git a/src/app/sections/account/components/login.section.component.ts b/src/app/sections/account/components/login.section.component.ts index f0a12a0..10da32b 100644 --- a/src/app/sections/account/components/login.section.component.ts +++ b/src/app/sections/account/components/login.section.component.ts @@ -1,4 +1,5 @@ import { Subscription } from 'rxjs'; +import { take, filter } from 'rxjs/operators'; import { Component, OnInit, OnDestroy, Input, ViewChild } from '@angular/core'; @@ -20,7 +21,7 @@ import { LoginActions } from '@ucap/ng-store-authentication'; import { UserStore } from '@app/models/user-store'; import { LoginSession } from '@app/models/login-session'; import { AppKey } from '@app/types/app-key.type'; -import { take, filter } from 'rxjs/operators'; +import { AppAuthenticationService } from '@app/services/app-authentication.service'; @Component({ selector: 'app-sections-account-login', @@ -64,13 +65,12 @@ export class LoginSectionComponent implements OnInit, OnDestroy { private sessionStorageService: SessionStorageService, private i18nService: I18nService, private store: Store, + private appAuthenticationService: AppAuthenticationService, private logService: LogService ) {} ngOnInit(): void { - this.loginSession = this.sessionStorageService.get( - AppKey.LoginSession - ); + this.loginSession = this.appAuthenticationService.loginSession; this.loginTry = this.sessionStorageService.get(AppKey.LoginTry); @@ -84,13 +84,13 @@ export class LoginSectionComponent implements OnInit, OnDestroy { this.companyListSubscription = this.store .pipe(select(CompanySelector.companyList)) - .subscribe(companyList => { + .subscribe((companyList) => { this.companyList = companyList; }); this.loginTrySubscription = this.sessionStorageService.changed$ - .pipe(filter(param => AppKey.LoginTry === param.key)) - .subscribe(param => { + .pipe(filter((param) => AppKey.LoginTry === param.key)) + .subscribe((param) => { this.loginTry = param.value as LoginTry; }); } @@ -125,7 +125,7 @@ export class LoginSectionComponent implements OnInit, OnDestroy { }) .pipe(take(1)) .subscribe( - res => { + (res) => { if ('success' !== res.status.toLowerCase()) { this.onWebLoginFailure(event, res.status); return; @@ -143,7 +143,7 @@ export class LoginSectionComponent implements OnInit, OnDestroy { return; } }, - error => { + (error) => { this.onWebLoginFailure(event, error); }, () => { diff --git a/src/app/services/app-authentication.service.ts b/src/app/services/app-authentication.service.ts index fcde21d..3cb5ff4 100644 --- a/src/app/services/app-authentication.service.ts +++ b/src/app/services/app-authentication.service.ts @@ -29,21 +29,65 @@ export class AppAuthenticationService { @Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService ) {} - loggedIn(): boolean { + get userStore(): UserStore { + const userStore = this.localStorageService.encGet( + AppKey.UserStore, + environment.productConfig.localEncriptionKey + ); + + return userStore; + } + + set userStore(userStore: UserStore) { + const oldUserStore = this.userStore; + this.localStorageService.encSet( + AppKey.UserStore, + { + ...oldUserStore, + ...userStore + }, + environment.productConfig.localEncriptionKey + ); + } + + get loginSession(): LoginSession { const loginSession = this.sessionStorageService.get( AppKey.LoginSession ); + + return loginSession; + } + + set loginSession(loginSession: LoginSession) { + const oldLoginSession = this.loginSession; + this.sessionStorageService.set(AppKey.LoginSession, { + ...oldLoginSession, + ...loginSession + }); + } + + loggedIn(): boolean { + const loginSession = this.loginSession; return !!loginSession && !!loginSession.loginId; } - async login( + useAutoLogin(): UserStore | null { + const userStore = this.userStore; + + return !!userStore && + !!userStore.settings && + !!userStore.settings.general && + userStore.settings.general.autoLogin + ? userStore + : null; + } + + async postWebLogin( loginSession: LoginSession, rememberMe: boolean, autoLogin: boolean ) { - const prevLoginSession = this.sessionStorageService.get( - AppKey.LoginSession - ); + const prevLoginSession = this.loginSession; loginSession = { ...prevLoginSession, @@ -52,16 +96,13 @@ export class AppAuthenticationService { }; const encLoginPw = PasswordUtil.encrypt(loginSession.loginPw); - this.sessionStorageService.set(AppKey.LoginSession, { + this.loginSession = { ...loginSession, initPw: loginSession.loginId === loginSession.loginPw, loginPw: encLoginPw - }); + }; - let userStore = this.localStorageService.encGet( - AppKey.UserStore, - environment.productConfig.localEncriptionKey - ); + let userStore = this.userStore; if (!userStore) { userStore = { @@ -101,11 +142,7 @@ export class AppAuthenticationService { userStore.settings.general.autoLogin = autoLogin; } - this.localStorageService.encSet( - AppKey.UserStore, - userStore, - environment.productConfig.localEncriptionKey - ); + this.userStore = userStore; this.sessionStorageService.remove(AppKey.LogoutSession); } @@ -117,10 +154,7 @@ export class AppAuthenticationService { this.sessionStorageService.remove(AppKey.UrlInfoResponse); this.sessionStorageService.remove(AppKey.AuthResponse); - let userStore = this.localStorageService.encGet( - AppKey.UserStore, - environment.productConfig.localEncriptionKey - ); + let userStore = this.userStore; if (!!userStore) { userStore = { @@ -134,11 +168,7 @@ export class AppAuthenticationService { } }; - this.localStorageService.encSet( - AppKey.UserStore, - userStore, - environment.productConfig.localEncriptionKey - ); + this.userStore = userStore; } } } diff --git a/src/app/services/app.service.ts b/src/app/services/app.service.ts index d3a3f75..47c084b 100644 --- a/src/app/services/app.service.ts +++ b/src/app/services/app.service.ts @@ -11,21 +11,20 @@ import { PingProtocolService } from '@ucap/ng-protocol-ping'; import { DateService } from '@ucap/ng-ui'; import { TranslateService } from '@ucap/ng-ui-organization'; -import { LoginSession } from '@app/models/login-session'; -import { AppKey } from '@app/types/app-key.type'; - import { environment } from '@environments'; +import { AppAuthenticationService } from './app-authentication.service'; + @Injectable() export class AppService { readonly companyGroupCode = environment.companyConfig.companyGroupCode; constructor( - private sessionStorageService: SessionStorageService, private i18nService: I18nService, private translateService: TranslateService, private dateService: DateService, private pingProtocolService: PingProtocolService, + private appAuthenticationService: AppAuthenticationService, private logService: LogService, @Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService ) {} @@ -33,9 +32,7 @@ export class AppService { initialize(): Promise { const initSession = new Promise(async (resolve, reject) => { try { - let loginSession = this.sessionStorageService.get( - AppKey.LoginSession - ); + let loginSession = this.appAuthenticationService.loginSession; loginSession = loginSession || {}; @@ -67,12 +64,12 @@ export class AppService { break; } - this.sessionStorageService.set(AppKey.LoginSession, { + this.appAuthenticationService.loginSession = { ...loginSession, deviceType, desktopType, companyGroupCode: this.companyGroupCode - }); + }; this.dateService.setDefaultTimezone('Asia/Seoul'); this.dateService.use('Asia/Seoul'); diff --git a/src/app/store/authentication/effects.ts b/src/app/store/authentication/effects.ts index 312667d..3adffff 100644 --- a/src/app/store/authentication/effects.ts +++ b/src/app/store/authentication/effects.ts @@ -31,9 +31,9 @@ export class Effects { () => this.actions$.pipe( ofType(LoginActions.webLoginSuccess), - tap(params => { + tap((params) => { this.nativeService.checkForUpdates(params.login2Response.version); - this.appAuthenticationService.login( + this.appAuthenticationService.postWebLogin( { companyCode: params.companyCode, loginId: params.loginId, @@ -52,7 +52,7 @@ export class Effects { () => this.actions$.pipe( ofType(LoginActions.webLoginFailure), - tap(params => { + tap((params) => { let loginTry = this.sessionStorageService.get( AppKey.LoginTry ); @@ -91,14 +91,14 @@ export class Effects { interval(1000) .pipe(takeUntil(waitTimer$)) .subscribe( - v => { + (v) => { loginTry.remainTimeForNextTry = this.intervalForRetry - v; this.sessionStorageService.set( AppKey.LoginTry, loginTry ); }, - error => {}, + (error) => {}, () => { loginTry = { failCount: 0, diff --git a/tslint.json b/tslint.json index c8d70f1..91baf45 100644 --- a/tslint.json +++ b/tslint.json @@ -9,28 +9,12 @@ "component-class-suffix": true, "contextual-lifecycle": true, "directive-class-suffix": true, - "directive-selector": [ - true, - "attribute", - "app", - "camelCase" - ], - "component-selector": [ - true, - "element", - "app", - "kebab-case" - ], - "import-blacklist": [ - true, - "rxjs/Rx" - ], + "directive-selector": [true, "attribute", "app", "camelCase"], + "component-selector": [true, "element", "app", "kebab-case"], + "import-blacklist": [true, "rxjs/Rx"], "interface-name": false, "max-classes-per-file": false, - "max-line-length": [ - true, - 140 - ], + "max-line-length": [true, 140], "member-access": false, "member-ordering": [ true, @@ -44,33 +28,17 @@ } ], "no-consecutive-blank-lines": false, - "no-console": [ - true, - "debug", - "info", - "time", - "timeEnd", - "trace" - ], + "no-console": [true, "debug", "info", "time", "timeEnd", "trace"], "no-empty": false, - "no-inferrable-types": [ - true, - "ignore-params" - ], - "no-non-null-assertion": true, + "no-inferrable-types": [true, "ignore-params"], + "no-non-null-assertion": false, "no-redundant-jsdoc": true, "no-switch-case-fall-through": true, "no-var-requires": false, - "object-literal-key-quotes": [ - true, - "as-needed" - ], + "object-literal-key-quotes": [true, "as-needed"], "object-literal-sort-keys": false, "ordered-imports": false, - "quotemark": [ - true, - "single" - ], + "quotemark": [true, "single"], "trailing-comma": false, "no-conflicting-lifecycle": true, "no-host-metadata-property": true, @@ -85,7 +53,5 @@ "use-lifecycle-interface": true, "use-pipe-transform-interface": true }, - "rulesDirectory": [ - "codelyzer" - ] -} \ No newline at end of file + "rulesDirectory": ["codelyzer"] +}