diff --git a/projects/ucap-webmessenger-app/src/app/pages/account/components/login.page.component.ts b/projects/ucap-webmessenger-app/src/app/pages/account/components/login.page.component.ts index 2c15bd2d..24aafddd 100644 --- a/projects/ucap-webmessenger-app/src/app/pages/account/components/login.page.component.ts +++ b/projects/ucap-webmessenger-app/src/app/pages/account/components/login.page.component.ts @@ -45,7 +45,7 @@ export class LoginPageComponent implements OnInit { notValid: () => void; }) { this.store.dispatch( - AuthenticationStore.login({ + AuthenticationStore.webLogin({ loginInfo: { companyCode: value.companyCode, loginId: value.loginId, diff --git a/projects/ucap-webmessenger-app/src/app/resolvers/messenger.resolver.ts b/projects/ucap-webmessenger-app/src/app/resolvers/messenger.resolver.ts index 81879ae1..f3b5cd4c 100644 --- a/projects/ucap-webmessenger-app/src/app/resolvers/messenger.resolver.ts +++ b/projects/ucap-webmessenger-app/src/app/resolvers/messenger.resolver.ts @@ -4,8 +4,8 @@ import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; -import { Observable } from 'rxjs'; -import { take, map } from 'rxjs/operators'; +import { Observable, of } from 'rxjs'; +import { take, map, catchError } from 'rxjs/operators'; import { Store, select } from '@ngrx/store'; @@ -22,6 +22,7 @@ import { SSOMode } from '@ucap-webmessenger/protocol-authentication'; import { LocaleCode } from '@ucap-webmessenger/core'; +import * as AuthenticationStore from '@app/store/account/authentication'; @Injectable() export class AppMessengerResolver implements Resolve { @@ -83,7 +84,16 @@ export class AppMessengerResolver implements Resolve { .pipe( take(1), map(loginRes => { - console.log('loginRes', loginRes); + this.store.dispatch( + AuthenticationStore.loginSuccess({ + loginInfo: loginRes + }) + ); + }), + catchError(err => { + return of( + AuthenticationStore.loginFailure({ error: err }) + ); }) ) .subscribe(); diff --git a/projects/ucap-webmessenger-app/src/app/store/account/authentication/actions.ts b/projects/ucap-webmessenger-app/src/app/store/account/authentication/actions.ts index 28b5bcc0..1e10669f 100644 --- a/projects/ucap-webmessenger-app/src/app/store/account/authentication/actions.ts +++ b/projects/ucap-webmessenger-app/src/app/store/account/authentication/actions.ts @@ -1,19 +1,20 @@ import { createAction, props } from '@ngrx/store'; import { Login2Response } from '@ucap-webmessenger/pi'; +import { LoginResponse } from '@ucap-webmessenger/protocol-authentication'; import { LoginInfo } from '../../../types'; -export const login = createAction( - '[Account::Authentication] Login', +export const webLogin = createAction( + '[Account::Authentication] Web Login', props<{ loginInfo: LoginInfo; rememberMe: boolean; }>() ); -export const loginSuccess = createAction( - '[Account::Authentication] Login Success', +export const webLoginSuccess = createAction( + '[Account::Authentication] Web Login Success', props<{ loginInfo: LoginInfo; rememberMe: boolean; @@ -21,6 +22,18 @@ export const loginSuccess = createAction( }>() ); +export const webLoginFailure = createAction( + '[Account::Authentication] Web Login Failure', + props<{ error: any }>() +); + +export const loginSuccess = createAction( + '[Account::Authentication] Login Success', + props<{ + loginInfo: LoginResponse; + }>() +); + export const loginFailure = createAction( '[Account::Authentication] Login Failure', props<{ error: any }>() diff --git a/projects/ucap-webmessenger-app/src/app/store/account/authentication/effects.ts b/projects/ucap-webmessenger-app/src/app/store/account/authentication/effects.ts index b3d81cea..818aa464 100644 --- a/projects/ucap-webmessenger-app/src/app/store/account/authentication/effects.ts +++ b/projects/ucap-webmessenger-app/src/app/store/account/authentication/effects.ts @@ -20,22 +20,24 @@ import { } from '@ucap-webmessenger/ui'; import { - login, loginSuccess, loginFailure, loginRedirect, logout, logoutConfirmation, - logoutConfirmationDismiss + logoutConfirmationDismiss, + webLogin, + webLoginSuccess, + webLoginFailure } from './actions'; import { LoginInfo } from '../../../types'; import { AppAuthenticationService } from '../../../services/authentication.service'; @Injectable() export class Effects { - login$ = createEffect(() => + webLogin$ = createEffect(() => this.actions$.pipe( - ofType(login), + ofType(webLogin), map(action => action), exhaustMap((params: { loginInfo: LoginInfo; rememberMe: boolean }) => this.piService @@ -47,25 +49,25 @@ export class Effects { .pipe( map((res: Login2Response) => { if (res.status === ResponseStatus.Fail) { - return loginFailure({ error: 'Failed' }); + return webLoginFailure({ error: 'Failed' }); } else { - return loginSuccess({ + return webLoginSuccess({ loginInfo: params.loginInfo, rememberMe: params.rememberMe, login2Response: res }); } }), - catchError(error => of(loginFailure({ error }))) + catchError(error => of(webLoginFailure({ error }))) ) ) ) ); - loginSuccess$ = createEffect( + webLoginSuccess$ = createEffect( () => this.actions$.pipe( - ofType(loginSuccess), + ofType(webLoginSuccess), tap(params => { this.nativeService .checkForUpdates() diff --git a/projects/ucap-webmessenger-app/src/app/store/account/authentication/reducers.ts b/projects/ucap-webmessenger-app/src/app/store/account/authentication/reducers.ts index 28e58c6f..c67608e3 100644 --- a/projects/ucap-webmessenger-app/src/app/store/account/authentication/reducers.ts +++ b/projects/ucap-webmessenger-app/src/app/store/account/authentication/reducers.ts @@ -1,6 +1,13 @@ -import { Action, combineReducers } from '@ngrx/store'; -import { State } from './state'; +import { Action, combineReducers, createReducer, on } from '@ngrx/store'; +import { State, initialState } from './state'; +import { loginSuccess } from './actions'; -export function reducers(state: State | undefined, action: Action) { - return combineReducers({})(state, action); -} +export const reducer = createReducer( + initialState, + on(loginSuccess, (state, action) => { + return { + ...state, + loginInfo: action.loginInfo + }; + }) +); diff --git a/projects/ucap-webmessenger-app/src/app/store/account/authentication/state.ts b/projects/ucap-webmessenger-app/src/app/store/account/authentication/state.ts index aacfe4f7..579cad2a 100644 --- a/projects/ucap-webmessenger-app/src/app/store/account/authentication/state.ts +++ b/projects/ucap-webmessenger-app/src/app/store/account/authentication/state.ts @@ -1,10 +1,20 @@ -import { Selector } from '@ngrx/store'; +import { Selector, createSelector } from '@ngrx/store'; +import { LoginResponse } from '@ucap-webmessenger/protocol-authentication'; // tslint:disable-next-line: no-empty-interface -export interface State {} +export interface State { + loginInfo: LoginResponse | null; +} -export const initialState: State = {}; +export const initialState: State = { + loginInfo: null +}; export function selectors(selector: Selector) { - return {}; + return { + loginInfo: createSelector( + selector, + (state: State) => state.loginInfo + ) + }; } diff --git a/projects/ucap-webmessenger-app/src/app/store/account/index.ts b/projects/ucap-webmessenger-app/src/app/store/account/index.ts index 5cdac23a..13adc103 100644 --- a/projects/ucap-webmessenger-app/src/app/store/account/index.ts +++ b/projects/ucap-webmessenger-app/src/app/store/account/index.ts @@ -11,7 +11,7 @@ export const effects: Type[] = [AuthenticationStore.Effects]; export function reducers(state: State | undefined, action: Action) { return combineReducers({ - authentication: AuthenticationStore.reducers + authentication: AuthenticationStore.reducer })(state, action); }