525 lines
16 KiB
TypeScript
Raw Normal View History

2019-11-19 18:43:49 +09:00
import { Injectable, Inject, NgZone } from '@angular/core';
2019-09-18 15:02:21 +09:00
import { Router } from '@angular/router';
2019-11-13 15:28:33 +09:00
import { of, Observable } from 'rxjs';
2019-10-02 14:34:17 +09:00
import { catchError, exhaustMap, map, tap, switchMap } from 'rxjs/operators';
2019-09-18 15:02:21 +09:00
2019-09-27 12:53:21 +09:00
import { Actions, ofType, createEffect } from '@ngrx/effects';
2019-10-04 10:19:55 +09:00
import {
PiService,
Login2Response,
UserTermsActionResponse
2019-10-04 10:19:55 +09:00
} from '@ucap-webmessenger/pi';
2019-09-19 17:03:39 +09:00
import { NativeService, UCAP_NATIVE_SERVICE } from '@ucap-webmessenger/native';
import {
DialogService,
ConfirmDialogComponent,
ConfirmDialogData,
2019-11-29 15:09:47 +09:00
ConfirmDialogResult,
2019-11-29 16:08:24 +09:00
SnackBarService,
AlertDialogComponent,
AlertDialogData,
AlertDialogResult
2019-09-19 17:03:39 +09:00
} from '@ucap-webmessenger/ui';
2019-09-18 15:02:21 +09:00
import {
loginRedirect,
2019-09-19 17:03:39 +09:00
logout,
logoutConfirmation,
2019-09-24 09:23:30 +09:00
logoutConfirmationDismiss,
webLogin,
webLoginSuccess,
2019-10-04 10:19:55 +09:00
webLoginFailure,
postLogin,
privacyAgree,
privacyDisagree,
privacyAgreeFailure,
privacyAgreeSuccess,
increaseLoginFailCount,
2019-11-19 18:43:49 +09:00
initialLoginFailCount,
2019-11-29 14:26:59 +09:00
logoutInitialize,
userPasswordSet,
userPasswordSetSuccess,
userPasswordSetFailure,
infoUser,
infoUserSuccess,
infoUserFailure
2019-09-18 15:02:21 +09:00
} from './actions';
2019-10-04 10:19:55 +09:00
import {
LoginInfo,
KEY_LOGIN_INFO,
EnvironmentsInfo,
KEY_ENVIRONMENTS_INFO,
KEY_URL_INFO,
LogoutInfo,
KEY_LOGOUT_INFO
2019-10-04 10:19:55 +09:00
} from '@app/types';
import { AppAuthenticationService } from '@app/services/authentication.service';
2019-09-27 12:53:21 +09:00
import { NGXLogger } from 'ngx-logger';
2019-10-04 10:19:55 +09:00
import { Store } from '@ngrx/store';
import {
SessionStorageService,
LocalStorageService
} from '@ucap-webmessenger/web-storage';
2019-11-28 18:12:20 +09:00
import {
AuthenticationProtocolService,
LoginResponse
} from '@ucap-webmessenger/protocol-authentication';
import { KEY_LOGIN_RES_INFO } from '@app/types/login-res-info.type';
2019-11-28 13:19:19 +09:00
import { ProtocolService } from '@ucap-webmessenger/protocol';
2019-09-18 15:02:21 +09:00
2019-11-28 14:02:07 +09:00
import { environment } from '../../../../environments/environment';
2020-01-08 12:30:39 +09:00
2019-11-29 14:26:59 +09:00
import {
ServiceProtocolService,
UserPasswordSetResponse
} from '@ucap-webmessenger/protocol-service';
import { DaesangUrlInfoResponse } from '@ucap-webmessenger/api-external';
import { AppUserInfo, KEY_APP_USER_INFO } from '@app/types/app-user-info.type';
import { DaesangCipherService, WebLinkType } from '@ucap-webmessenger/daesang';
2020-01-08 12:30:39 +09:00
import { TranslateService } from '@ngx-translate/core';
import {
InfoProtocolService,
UserResponse
} from '@ucap-webmessenger/protocol-info';
2019-11-28 14:02:07 +09:00
2019-09-18 15:02:21 +09:00
@Injectable()
export class Effects {
webLogin$ = createEffect(
() =>
this.actions$.pipe(
ofType(webLogin),
map(action => action),
exhaustMap(
(params: {
loginInfo: LoginInfo;
rememberMe: boolean;
autoLogin: boolean;
}) =>
this.piService
.login2({
loginId: params.loginInfo.loginId,
loginPw: params.loginInfo.loginPw,
companyCode: params.loginInfo.companyCode
})
.pipe(
map((res: Login2Response) => {
if ('success' !== res.status.toLowerCase()) {
if (!!params.autoLogin) {
// auto login Failure.
this.localStorageService.remove(KEY_APP_USER_INFO);
this.router.navigateByUrl('/account/login');
} else {
this.store.dispatch(increaseLoginFailCount({}));
this.store.dispatch(webLoginFailure({ error: 'Failed' }));
}
} else {
this.store.dispatch(initialLoginFailCount({}));
this.store.dispatch(
webLoginSuccess({
loginInfo: params.loginInfo,
rememberMe: params.rememberMe,
autoLogin: params.autoLogin,
login2Response: res
})
);
}
}),
catchError(error => of(webLoginFailure({ error })))
)
)
),
{ dispatch: false }
2019-09-18 15:02:21 +09:00
);
2019-09-24 09:23:30 +09:00
webLoginSuccess$ = createEffect(
2019-09-19 14:15:43 +09:00
() =>
this.actions$.pipe(
2019-09-24 09:23:30 +09:00
ofType(webLoginSuccess),
2020-01-23 14:15:08 +09:00
tap(params => {
this.nativeService.checkForUpdates(params.login2Response.version);
this.appAuthenticationService.login(
params.loginInfo,
params.rememberMe,
params.autoLogin
);
this.router.navigate(['/messenger']);
})
2019-09-19 14:15:43 +09:00
),
{ dispatch: false }
2019-09-18 15:02:21 +09:00
);
loginRedirect$ = createEffect(
() =>
this.actions$.pipe(
2019-09-19 17:03:39 +09:00
ofType(loginRedirect),
2019-09-18 15:02:21 +09:00
tap(authed => {
2019-11-28 13:19:19 +09:00
// this.ngZone.run(() => {
// location.href = this.router.parseUrl('/account/login').toString();
this.protocolService.disconnect();
2019-11-28 15:48:36 +09:00
this.router.navigate(['/account/login']);
2019-11-28 13:19:19 +09:00
this.appAuthenticationService.logout();
this.store.dispatch(logoutInitialize());
// });
2019-09-18 15:02:21 +09:00
})
),
{ dispatch: false }
);
2019-11-19 18:43:49 +09:00
logout$ = createEffect(
() => {
return this.actions$.pipe(
ofType(logout),
switchMap(action => {
return this.authenticationProtocolService.logout({}).pipe(
map(res => {
this.sessionStorageService.set<LogoutInfo>(KEY_LOGOUT_INFO, {
personLogout: true
} as LogoutInfo);
2019-11-19 18:43:49 +09:00
this.store.dispatch(loginRedirect());
}),
catchError(error => of(error))
);
})
);
},
{ dispatch: false }
2019-09-19 17:03:39 +09:00
);
logoutConfirmation$ = createEffect(() =>
this.actions$.pipe(
ofType(logoutConfirmation),
exhaustMap(async () => {
const loginRes = this.sessionStorageService.get<LoginResponse>(
KEY_LOGIN_RES_INFO
);
if (!!loginRes && loginRes.userSeq) {
const result = await this.dialogService.open<
ConfirmDialogComponent,
ConfirmDialogData,
ConfirmDialogResult
>(ConfirmDialogComponent, {
data: {
2020-01-08 12:30:39 +09:00
title: this.translateService.instant('accounts.logout'),
message: this.translateService.instant('accounts.confirmLogout')
}
});
2019-09-19 17:03:39 +09:00
if (!!result && !!result.choice && result.choice) {
return true;
} else {
return false;
}
} else {
return false;
}
2019-09-19 17:03:39 +09:00
}),
map(result => (result ? logout() : logoutConfirmationDismiss()))
)
);
2019-10-04 10:19:55 +09:00
postLogin$ = createEffect(
() =>
this.actions$.pipe(
ofType(postLogin),
map(action => action.loginRes),
tap(async loginRes => {
const loginInfo = this.sessionStorageService.get<LoginInfo>(
KEY_LOGIN_INFO
);
const environmentsInfo = this.sessionStorageService.get<
EnvironmentsInfo
>(KEY_ENVIRONMENTS_INFO);
2019-11-28 14:02:07 +09:00
if (
environment.productConfig.authentication
.usePrivateInformationAgree &&
!loginRes.privateInformationAgree
) {
2019-10-04 10:19:55 +09:00
const privacyTotalUrl = this.piService.privacyTotalUrl({
companyCode: loginInfo.companyCode,
userSeq: loginRes.userSeq,
token: loginRes.tokenString,
deviceType: environmentsInfo.deviceType,
localeCode: loginInfo.localeCode,
textOnly: 'true'
2019-10-04 10:19:55 +09:00
});
const result = await this.dialogService.open<
ConfirmDialogComponent,
ConfirmDialogData,
ConfirmDialogResult
>(ConfirmDialogComponent, {
width: '100%',
height: '500px',
disableClose: true,
data: {
2020-01-08 12:30:39 +09:00
title: this.translateService.instant(
'accounts.consentOfPrivacy'
),
2019-11-28 13:19:19 +09:00
html: `<iframe id="ifm_privacy" src="${privacyTotalUrl}" style="width: 100%;height: 300px;" />`
}
2019-10-04 10:19:55 +09:00
});
if (result.choice) {
this.store.dispatch(privacyAgree({ loginRes }));
} else {
this.store.dispatch(privacyDisagree());
return;
}
}
if (!!loginInfo.initPw) {
2019-11-28 15:48:36 +09:00
this.store.dispatch(logout());
const passwordInitStep1Url = this.piService.passwordInitStep1Url({
localeCode: loginInfo.localeCode
});
2019-10-04 10:19:55 +09:00
const result = await this.dialogService.open<
ConfirmDialogComponent,
ConfirmDialogData,
ConfirmDialogResult
>(ConfirmDialogComponent, {
width: '100%',
height: '500px',
disableClose: true,
data: {
2020-01-08 12:30:39 +09:00
title: this.translateService.instant('accounts.resetPassword'),
2019-11-28 15:48:36 +09:00
html: `<iframe id="ifm_privacy" src="${passwordInitStep1Url}" style="width: 100%;height: 300px;" />`
}
2019-10-04 10:19:55 +09:00
});
if (result.choice) {
} else {
return;
}
}
2019-11-28 15:48:36 +09:00
if (!loginRes.passwordValid) {
// [Daesang]
const urlInfo: DaesangUrlInfoResponse = this.sessionStorageService.get<
DaesangUrlInfoResponse
>(KEY_URL_INFO);
await this.dialogService.open<
AlertDialogComponent,
AlertDialogData,
AlertDialogResult
>(AlertDialogComponent, {
2019-11-29 14:26:59 +09:00
data: {
2020-01-08 12:30:39 +09:00
title: this.translateService.instant(
'accounts.passwordExpired'
),
html: this.translateService.instant(
'accounts.descriptionOfPasswordExpired'
)
2019-11-29 14:26:59 +09:00
}
2019-11-28 15:48:36 +09:00
});
const passwordChangeUrl = urlInfo.webLink.filter(
weblink => weblink.key === WebLinkType.ChgPassword
);
if (passwordChangeUrl.length > 0) {
const appUserInfo = this.localStorageService.encGet<AppUserInfo>(
KEY_APP_USER_INFO,
environment.customConfig.appKey
);
const loginPw = appUserInfo.loginPw;
const loginId = appUserInfo.loginId;
const url = passwordChangeUrl[0].url
.replace(/(\(%USER_ID%\))/g, loginId)
.replace(
/(\(%USER_PASS%\))/g,
this.daesangCipherService.encryptForWebLink(loginPw)
);
this.nativeService.openDefaultBrowser(url);
2019-11-28 15:48:36 +09:00
}
this.store.dispatch(logout());
// // [GROUP]
// const result = await this.dialogService.open<
// ChangePasswordDialogComponent,
// ChangePasswordDialogData,
// ChangePasswordDialogResult
// >(ChangePasswordDialogComponent, {
// width: '500px',
// height: '500px',
// disableClose: false,
// data: {
// loginId: loginInfo.loginId,
// encryptedLoginPw: loginInfo.loginPw,
// phoneNumber: loginRes.userInfo.hpNumber
// }
// });
// if (!!result && result.choice) {
// this.store.dispatch(
// userPasswordSet({
// req: {
// companyCode: loginInfo.companyCode,
// loginId: loginInfo.loginId,
// oldLoginPw: result.currentLoginPw,
// newLoginPw: result.newLoginPw
// }
// })
// );
// } else {
// this.store.dispatch(logout());
// return;
// }
2019-11-28 15:48:36 +09:00
}
2019-10-04 10:19:55 +09:00
})
),
{ dispatch: false }
);
privacyAgree$ = createEffect(() =>
this.actions$.pipe(
ofType(privacyAgree),
map(action => {
const loginInfo = this.sessionStorageService.get<LoginInfo>(
KEY_LOGIN_INFO
);
const environmentsInfo = this.sessionStorageService.get<
EnvironmentsInfo
>(KEY_ENVIRONMENTS_INFO);
return {
loginInfo,
environmentsInfo,
loginResponse: action.loginRes
2019-10-04 10:19:55 +09:00
};
}),
exhaustMap(params =>
this.piService
.userTermsAction({
userSeq: params.loginResponse.userSeq,
token: params.loginResponse.tokenString,
deviceType: params.environmentsInfo.deviceType
2019-10-04 10:19:55 +09:00
})
.pipe(
map((res: UserTermsActionResponse) => {
if ('00' !== res.responseCode) {
return privacyAgreeFailure({ error: 'Failed' });
} else {
return privacyAgreeSuccess();
}
}),
catchError(error => of(privacyAgreeFailure({ error })))
)
)
)
);
2019-11-29 14:26:59 +09:00
userPasswordSet$ = createEffect(() =>
this.actions$.pipe(
ofType(userPasswordSet),
map(action => action.req),
exhaustMap(req =>
this.serviceProtocolService.userPasswordSet(req).pipe(
map((res: UserPasswordSetResponse) => {
return userPasswordSetSuccess({
res
});
}),
catchError(error => of(userPasswordSetFailure({ error })))
)
)
)
);
2019-11-29 15:09:47 +09:00
userPasswordSetSuccess$ = createEffect(
() => {
return this.actions$.pipe(
ofType(userPasswordSetSuccess),
2019-11-29 16:08:24 +09:00
tap(async action => {
await this.dialogService.open<
AlertDialogComponent,
AlertDialogData,
AlertDialogResult
>(AlertDialogComponent, {
width: '360px',
disableClose: true,
data: {
2020-01-08 12:30:39 +09:00
title: this.translateService.instant('accounts.changePassword'),
message: this.translateService.instant(
'accounts.results.changedPassword'
)
2019-11-29 16:08:24 +09:00
}
2019-11-29 15:09:47 +09:00
});
2019-11-29 16:08:24 +09:00
this.store.dispatch(logout());
2019-11-29 15:09:47 +09:00
})
);
},
{ dispatch: false }
);
userPasswordSetFailure$ = createEffect(
() => {
return this.actions$.pipe(
ofType(userPasswordSetFailure),
tap(action => {
this.snackBarService.open(
2020-01-08 12:30:39 +09:00
this.translateService.instant(
'accounts.errors.failToChangePassword'
),
2019-11-29 15:09:47 +09:00
'',
{
duration: 3000,
verticalPosition: 'bottom'
}
);
})
);
},
{ dispatch: false }
);
infoUser$ = createEffect(() =>
this.actions$.pipe(
ofType(infoUser),
map(action => action.req),
exhaustMap(req =>
this.infoProtocolService.user(req).pipe(
map((res: UserResponse) => {
return infoUserSuccess({
res
});
}),
catchError(error => of(infoUserFailure({ error })))
)
)
)
);
2019-09-18 15:02:21 +09:00
constructor(
private actions$: Actions,
2019-11-19 18:43:49 +09:00
private ngZone: NgZone,
2019-09-19 17:03:39 +09:00
private router: Router,
2019-10-04 10:19:55 +09:00
private store: Store<any>,
private localStorageService: LocalStorageService,
2019-10-04 10:19:55 +09:00
private sessionStorageService: SessionStorageService,
2019-09-18 15:02:21 +09:00
private piService: PiService,
2019-09-19 18:22:13 +09:00
private appAuthenticationService: AppAuthenticationService,
2019-11-28 13:19:19 +09:00
private protocolService: ProtocolService,
2019-11-19 18:43:49 +09:00
private authenticationProtocolService: AuthenticationProtocolService,
private infoProtocolService: InfoProtocolService,
2019-11-29 14:26:59 +09:00
private serviceProtocolService: ServiceProtocolService,
2020-01-08 12:30:39 +09:00
private translateService: TranslateService,
2019-09-19 17:03:39 +09:00
private dialogService: DialogService,
2019-11-29 15:09:47 +09:00
private snackBarService: SnackBarService,
2019-09-27 12:53:21 +09:00
@Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService,
private logger: NGXLogger,
private daesangCipherService: DaesangCipherService
2019-09-18 15:02:21 +09:00
) {}
}