From 5a3099f3ca49511d30ce5621de3474708c5c3faf Mon Sep 17 00:00:00 2001 From: crusader Date: Mon, 12 Mar 2018 18:39:03 +0900 Subject: [PATCH] ing --- src/app/commons/guard/auth.guard.ts | 4 ++-- .../member/component/signin/signin.component.ts | 13 +++++++++++-- src/packages/member/store/auth/auth.action.ts | 3 ++- src/packages/member/store/auth/auth.effect.ts | 16 ++++++++++++---- src/packages/member/store/auth/auth.reducer.ts | 4 ++-- 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/app/commons/guard/auth.guard.ts b/src/app/commons/guard/auth.guard.ts index 6f85e17..4334e97 100644 --- a/src/app/commons/guard/auth.guard.ts +++ b/src/app/commons/guard/auth.guard.ts @@ -25,7 +25,7 @@ export class AuthGuard implements CanActivate, CanActivateChild { .select(AuthSelector.select('signined')) .map(signined => { if (!signined) { - this.store.dispatch(new AuthStore.SigninRedirect()); + this.store.dispatch(new AuthStore.SigninRedirect(state.url)); return false; } @@ -39,7 +39,7 @@ export class AuthGuard implements CanActivate, CanActivateChild { .select(AuthSelector.select('signined')) .map(signined => { if (!signined) { - this.store.dispatch(new AuthStore.SigninRedirect()); + this.store.dispatch(new AuthStore.SigninRedirect(state.url)); return false; } diff --git a/src/packages/member/component/signin/signin.component.ts b/src/packages/member/component/signin/signin.component.ts index 02816fe..23f786c 100644 --- a/src/packages/member/component/signin/signin.component.ts +++ b/src/packages/member/component/signin/signin.component.ts @@ -1,5 +1,5 @@ import { Component, OnInit } from '@angular/core'; -import { Router } from '@angular/router'; +import { ActivatedRoute, Router } from '@angular/router'; import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { Store, select } from '@ngrx/store'; @@ -16,6 +16,7 @@ export class SigninComponent implements OnInit { error$ = this.store.pipe(select(AuthSelector.select('error'))); errorMessage: string | null; + returnURL: string; signinForm: FormGroup; formErrors = { @@ -24,6 +25,7 @@ export class SigninComponent implements OnInit { }; constructor( + private activatedRoute: ActivatedRoute, private router: Router, private store: Store, private formBuilder: FormBuilder, @@ -33,6 +35,8 @@ export class SigninComponent implements OnInit { ngOnInit() { this.initForm(); + this.returnURL = this.activatedRoute.snapshot.queryParams['returnURL'] || '/'; + this.pending$.subscribe((pending: boolean) => { if (pending) { this.signinForm.disable(); @@ -79,7 +83,12 @@ export class SigninComponent implements OnInit { this.router.navigateByUrl('/auth/signup'); } signin() { - this.store.dispatch(new AuthStore.Signin(this.signinForm.value)); + const signinValue = Object.assign({}, this.signinForm.value); + signinValue.options = { + returnURL: this.returnURL, + }; + + this.store.dispatch(new AuthStore.Signin(signinValue)); console.log('signin component signin fnc'); } } diff --git a/src/packages/member/store/auth/auth.action.ts b/src/packages/member/store/auth/auth.action.ts index 63f0508..b8dde70 100644 --- a/src/packages/member/store/auth/auth.action.ts +++ b/src/packages/member/store/auth/auth.action.ts @@ -18,7 +18,7 @@ export enum ActionType { export class Signin implements Action { readonly type = ActionType.Signin; - constructor(public payload: {email: string, password: string}) {} + constructor(public payload: {email: string, password: string, options: any}) {} } export class SigninSuccess implements Action { @@ -35,6 +35,7 @@ export class SigninFailure implements Action { export class SigninRedirect implements Action { readonly type = ActionType.SigninRedirect; + constructor(public payload: string) {} } export class Signout implements Action { diff --git a/src/packages/member/store/auth/auth.effect.ts b/src/packages/member/store/auth/auth.effect.ts index 2ef98c5..cfadff0 100644 --- a/src/packages/member/store/auth/auth.effect.ts +++ b/src/packages/member/store/auth/auth.effect.ts @@ -25,11 +25,13 @@ import { Signin, SigninSuccess, SigninFailure, + SigninRedirect, ActionType, } from './auth.action'; @Injectable() export class Effects { + private _options: any; constructor( private actions$: Actions, @@ -41,7 +43,10 @@ export class Effects { signin$: Observable = this.actions$ .ofType(ActionType.Signin) .map((action: Signin) => action.payload) - .switchMap(payload => this.memberService.signin(payload.email, payload.password)) + .switchMap(payload => { + this._options = payload.options; + return this.memberService.signin(payload.email, payload.password); + }) .map((domainMember: DomainMember) => { return new SigninSuccess(domainMember); }) @@ -52,13 +57,16 @@ export class Effects { @Effect({ dispatch: false }) signinSuccess$ = this.actions$ .ofType(ActionType.SigninSuccess) - .do(() => this.router.navigate(['/'])); + .do(() => { + this.router.navigateByUrl(this._options.returnURL); + }); @Effect({ dispatch: false }) signinRedirect$ = this.actions$ .ofType(ActionType.SigninRedirect, ActionType.Signout) - .do(authed => { - this.router.navigate(['/auth/signin']); + .map((action: SigninRedirect) => action.payload) + .do(returnURL => { + this.router.navigate(['/auth/signin'], {queryParams: {returnURL: returnURL}}); }); } diff --git a/src/packages/member/store/auth/auth.reducer.ts b/src/packages/member/store/auth/auth.reducer.ts index 618522c..318b027 100644 --- a/src/packages/member/store/auth/auth.reducer.ts +++ b/src/packages/member/store/auth/auth.reducer.ts @@ -51,7 +51,7 @@ export function reducer(state = initialState, action: Actions): State { }; } - case ActionType.SigninSuccess: { + case ActionType.SignoutSuccess: { return { ...state, signined: false, @@ -62,7 +62,7 @@ export function reducer(state = initialState, action: Actions): State { }; } - case ActionType.SigninFailure: { + case ActionType.SignoutFailure: { return { ...state, error: action.payload,