From 2b323cd382a461e560be602455810030d872581b Mon Sep 17 00:00:00 2001 From: crusader Date: Mon, 28 May 2018 19:09:38 +0900 Subject: [PATCH] ing --- src/app/commons/guard/auth.guard.ts | 49 +++++++------ .../commons/store/container/signin/index.ts | 2 + .../store/container/signin/signin.action.ts | 15 ++++ .../signin/signin.effect.spec.ts} | 4 +- .../store/container/signin/signin.effect.ts | 72 +++++++++++++++++++ src/app/commons/store/index.ts | 4 +- src/app/commons/store/signin-init/index.ts | 1 - .../store/signin-init/signin-init.effect.ts | 68 ------------------ 8 files changed, 120 insertions(+), 95 deletions(-) create mode 100644 src/app/commons/store/container/signin/index.ts create mode 100644 src/app/commons/store/container/signin/signin.action.ts rename src/app/commons/store/{signin-init/signin-init.effect.spec.ts => container/signin/signin.effect.spec.ts} (75%) create mode 100644 src/app/commons/store/container/signin/signin.effect.ts delete mode 100644 src/app/commons/store/signin-init/index.ts delete mode 100644 src/app/commons/store/signin-init/signin-init.effect.ts diff --git a/src/app/commons/guard/auth.guard.ts b/src/app/commons/guard/auth.guard.ts index efe44a7..e8f1372 100644 --- a/src/app/commons/guard/auth.guard.ts +++ b/src/app/commons/guard/auth.guard.ts @@ -6,55 +6,60 @@ import { RouterStateSnapshot, Router, } from '@angular/router'; -import { Store } from '@ngrx/store'; - -import { Observable } from 'rxjs/Observable'; -import 'rxjs/add/operator/take'; -import 'rxjs/add/operator/map'; +import { Store, select } from '@ngrx/store'; +import { Observable } from 'rxjs'; +import { map, take } from 'rxjs/operators'; import { CookieService } from 'ngx-cookie-service'; -import * as AuthStore from '@overflow/member/store/auth'; -import { AuthSelector } from '@overflow/member/store'; +import { AuthContainerSelector } from '@overflow/shared/auth/store'; +import * as MemberEntityStore from '@overflow/member/store/entity/member'; +import * as SigninContaifnerStore from '../store/container/signin'; @Injectable() export class AuthGuard implements CanActivate, CanActivateChild { constructor( - private store: Store, + private store: Store, private router: Router, private cookieService: CookieService, ) { } canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { - return this.store - .select(AuthSelector.select('signined')) - .map(signined => { + return this.store.pipe( + select(AuthContainerSelector.selectSignined), + map(signined => { if (!signined) { if (this.cookieService.check('authToken')) { - this.store.dispatch(new AuthStore.SigninCookie({authToken: this.cookieService.get('authToken'), returnURL: state.url})); + this.store.dispatch(new MemberEntityStore.SigninCookie({authToken: this.cookieService.get('authToken'), returnURL: state.url})); } else { - // this.store.dispatch(new AuthStore.SigninRedirect(state.url)); - this.router.navigateByUrl(state.url); + this.store.dispatch(new SigninContaifnerStore.SigninRedirect({returnURL: state.url})); } return false; } return true; - }) - .take(1); + }), + take(1) + ); } canActivateChild(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { - return this.store - .select(AuthSelector.select('signined')) - .map(signined => { + return this.store.pipe( + select(AuthContainerSelector.selectSignined), + map(signined => { if (!signined) { - this.store.dispatch(new AuthStore.SigninRedirect(state.url)); + if (this.cookieService.check('authToken')) { + this.store.dispatch(new MemberEntityStore.SigninCookie({authToken: this.cookieService.get('authToken'), returnURL: state.url})); + } else { + this.store.dispatch(new SigninContaifnerStore.SigninRedirect({returnURL: state.url})); + } return false; } return true; - }) - .take(1); + }), + take(1) + ); } + } diff --git a/src/app/commons/store/container/signin/index.ts b/src/app/commons/store/container/signin/index.ts new file mode 100644 index 0000000..3a90828 --- /dev/null +++ b/src/app/commons/store/container/signin/index.ts @@ -0,0 +1,2 @@ +export * from './signin.action'; +export * from './signin.effect'; diff --git a/src/app/commons/store/container/signin/signin.action.ts b/src/app/commons/store/container/signin/signin.action.ts new file mode 100644 index 0000000..77226a5 --- /dev/null +++ b/src/app/commons/store/container/signin/signin.action.ts @@ -0,0 +1,15 @@ +import { Action } from '@ngrx/store'; + +export enum ActionType { + SigninRedirect = '[app.signin] SigninRedirect', +} + +export class SigninRedirect implements Action { + readonly type = ActionType.SigninRedirect; + + constructor(public payload: {returnURL: string}) {} +} + +export type Actions = + | SigninRedirect +; diff --git a/src/app/commons/store/signin-init/signin-init.effect.spec.ts b/src/app/commons/store/container/signin/signin.effect.spec.ts similarity index 75% rename from src/app/commons/store/signin-init/signin-init.effect.spec.ts rename to src/app/commons/store/container/signin/signin.effect.spec.ts index 6214021..2736e83 100644 --- a/src/app/commons/store/signin-init/signin-init.effect.spec.ts +++ b/src/app/commons/store/container/signin/signin.effect.spec.ts @@ -1,8 +1,8 @@ import { TestBed, inject } from '@angular/core/testing'; -import { Effects } from './signin-init.effect'; +import { Effects } from './signin.effect'; -describe('SigninInit.Effects', () => { +describe('signin-container.Effects', () => { beforeEach(() => { TestBed.configureTestingModule({ providers: [Effects] diff --git a/src/app/commons/store/container/signin/signin.effect.ts b/src/app/commons/store/container/signin/signin.effect.ts new file mode 100644 index 0000000..d8b92ad --- /dev/null +++ b/src/app/commons/store/container/signin/signin.effect.ts @@ -0,0 +1,72 @@ +import { Injectable } from '@angular/core'; +import { Router } from '@angular/router'; + +import { Effect, Actions, ofType } from '@ngrx/effects'; + +import { map, tap } from 'rxjs/operators'; + +import { CookieService } from 'ngx-cookie-service'; +import { RPCService } from '@loafer/ng-rpc'; + +import { + SigninSuccess, + SigninCookieSuccess, + ActionType as AuthActionType, +} from '@overflow/shared/auth/store/container/auth'; + +import { + SigninRedirect, + ActionType, +} from './signin.action'; + +import { DomainMember } from '@overflow/commons-typescript/model/domain'; + + +@Injectable() +export class Effects { + + constructor( + private actions$: Actions, + private rpcService: RPCService, + private cookieService: CookieService, + private router: Router + ) { } + + @Effect({ dispatch: false }) + signinSuccess$ = this.actions$.pipe( + ofType(AuthActionType.SigninSuccess), + map((action: SigninSuccess) => action.payload), + tap((info: {authToken: string, domainMember: DomainMember, returnURL: string}) => { + const expires = new Date(); + expires.setDate(expires.getDate() + 1); + this.cookieService.set('authToken', info.authToken, expires, '/'); + const queryString = `authToken=${info.authToken}`; + this.rpcService.connect(queryString); + + this.router.navigateByUrl(info.returnURL); + }) + ); + + @Effect({ dispatch: false }) + signinCookieSuccess$ = this.actions$.pipe( + ofType(AuthActionType.SigninCookieSuccess), + map((action: SigninCookieSuccess) => action.payload), + tap((info: {domainMember: DomainMember, returnURL: string}) => { + const authToken = this.cookieService.get('authToken'); + const queryString = `authToken=${authToken}`; + this.rpcService.connect(queryString); + + this.router.navigateByUrl(info.returnURL); + }) + ); + + @Effect({ dispatch: false }) + signinRedirect$ = this.actions$.pipe( + ofType(ActionType.SigninRedirect), + map((action: SigninRedirect) => action.payload), + tap((info: {returnURL: string}) => { + this.router.navigate(['/auth/signin'], {queryParams: {returnURL: info.returnURL}}); + }) + ); + +} diff --git a/src/app/commons/store/index.ts b/src/app/commons/store/index.ts index 64ea21e..67a90b3 100644 --- a/src/app/commons/store/index.ts +++ b/src/app/commons/store/index.ts @@ -1,6 +1,6 @@ import { ActionReducerMap } from '@ngrx/store'; -import * as SigninInitStore from './signin-init'; +import * as SigninContainerStore from './container/signin'; export interface State { } @@ -9,5 +9,5 @@ export const REDUCERS: ActionReducerMap = { }; export const EFFECTS = [ - SigninInitStore.Effects, + SigninContainerStore.Effects, ]; diff --git a/src/app/commons/store/signin-init/index.ts b/src/app/commons/store/signin-init/index.ts deleted file mode 100644 index 3ac331f..0000000 --- a/src/app/commons/store/signin-init/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './signin-init.effect'; diff --git a/src/app/commons/store/signin-init/signin-init.effect.ts b/src/app/commons/store/signin-init/signin-init.effect.ts deleted file mode 100644 index 368c51d..0000000 --- a/src/app/commons/store/signin-init/signin-init.effect.ts +++ /dev/null @@ -1,68 +0,0 @@ -import { Injectable } from '@angular/core'; -import { Router } from '@angular/router'; - -import { Effect, Actions, ofType } from '@ngrx/effects'; -import { Action } from '@ngrx/store'; - -import { Observable } from 'rxjs/Observable'; -import { of } from 'rxjs/observable/of'; - -import 'rxjs/add/operator/catch'; -import 'rxjs/add/operator/do'; -import 'rxjs/add/operator/exhaustMap'; -import 'rxjs/add/operator/switchMap'; -import 'rxjs/add/operator/map'; -import 'rxjs/add/operator/take'; - -import { CookieService } from 'ngx-cookie-service'; - -import { RPCService } from '@loafer/ng-rpc'; - -import { - SigninSuccess, - SigninCookieSuccess, - ActionType, -} from '@overflow/member/store/auth'; - -@Injectable() -export class Effects { - - constructor( - private actions$: Actions, - private rpcService: RPCService, - private cookieService: CookieService, - ) { } - - @Effect({ dispatch: false }) - signinSuccess$ = this.actions$ - .ofType(ActionType.SigninSuccess) - .map((action: SigninSuccess) => action.payload) - .do( - (result) => { - const authToken = result.authToken; - // console.log(`authToken: ${authToken}`); - - const expires = new Date(); - expires.setDate(expires.getDate() + 1); - this.cookieService.set('authToken', authToken, expires, '/'); - - const queryString = `authToken=${authToken}`; - - this.rpcService.connect(queryString); - } - ); - - @Effect({ dispatch: false }) - signinCookieSuccess$ = this.actions$ - .ofType(ActionType.SigninCookieSuccess) - .map((action: SigninCookieSuccess) => action.payload) - .do( - (result) => { - const authToken = this.cookieService.get('authToken'); - // console.log(`authToken: ${authToken}`); - const queryString = `authToken=${authToken}`; - - this.rpcService.connect(queryString); - } - ); -}