This commit is contained in:
crusader 2018-03-12 18:39:03 +09:00
parent a9eb6863f1
commit 5a3099f3ca
5 changed files with 29 additions and 11 deletions

View File

@ -25,7 +25,7 @@ export class AuthGuard implements CanActivate, CanActivateChild {
.select(AuthSelector.select('signined')) .select(AuthSelector.select('signined'))
.map(signined => { .map(signined => {
if (!signined) { if (!signined) {
this.store.dispatch(new AuthStore.SigninRedirect()); this.store.dispatch(new AuthStore.SigninRedirect(state.url));
return false; return false;
} }
@ -39,7 +39,7 @@ export class AuthGuard implements CanActivate, CanActivateChild {
.select(AuthSelector.select('signined')) .select(AuthSelector.select('signined'))
.map(signined => { .map(signined => {
if (!signined) { if (!signined) {
this.store.dispatch(new AuthStore.SigninRedirect()); this.store.dispatch(new AuthStore.SigninRedirect(state.url));
return false; return false;
} }

View File

@ -1,5 +1,5 @@
import { Component, OnInit } from '@angular/core'; 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 { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Store, select } from '@ngrx/store'; import { Store, select } from '@ngrx/store';
@ -16,6 +16,7 @@ export class SigninComponent implements OnInit {
error$ = this.store.pipe(select(AuthSelector.select('error'))); error$ = this.store.pipe(select(AuthSelector.select('error')));
errorMessage: string | null; errorMessage: string | null;
returnURL: string;
signinForm: FormGroup; signinForm: FormGroup;
formErrors = { formErrors = {
@ -24,6 +25,7 @@ export class SigninComponent implements OnInit {
}; };
constructor( constructor(
private activatedRoute: ActivatedRoute,
private router: Router, private router: Router,
private store: Store<AuthStore.State>, private store: Store<AuthStore.State>,
private formBuilder: FormBuilder, private formBuilder: FormBuilder,
@ -33,6 +35,8 @@ export class SigninComponent implements OnInit {
ngOnInit() { ngOnInit() {
this.initForm(); this.initForm();
this.returnURL = this.activatedRoute.snapshot.queryParams['returnURL'] || '/';
this.pending$.subscribe((pending: boolean) => { this.pending$.subscribe((pending: boolean) => {
if (pending) { if (pending) {
this.signinForm.disable(); this.signinForm.disable();
@ -79,7 +83,12 @@ export class SigninComponent implements OnInit {
this.router.navigateByUrl('/auth/signup'); this.router.navigateByUrl('/auth/signup');
} }
signin() { 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'); console.log('signin component signin fnc');
} }
} }

View File

@ -18,7 +18,7 @@ export enum ActionType {
export class Signin implements Action { export class Signin implements Action {
readonly type = ActionType.Signin; 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 { export class SigninSuccess implements Action {
@ -35,6 +35,7 @@ export class SigninFailure implements Action {
export class SigninRedirect implements Action { export class SigninRedirect implements Action {
readonly type = ActionType.SigninRedirect; readonly type = ActionType.SigninRedirect;
constructor(public payload: string) {}
} }
export class Signout implements Action { export class Signout implements Action {

View File

@ -25,11 +25,13 @@ import {
Signin, Signin,
SigninSuccess, SigninSuccess,
SigninFailure, SigninFailure,
SigninRedirect,
ActionType, ActionType,
} from './auth.action'; } from './auth.action';
@Injectable() @Injectable()
export class Effects { export class Effects {
private _options: any;
constructor( constructor(
private actions$: Actions, private actions$: Actions,
@ -41,7 +43,10 @@ export class Effects {
signin$: Observable<Action> = this.actions$ signin$: Observable<Action> = this.actions$
.ofType(ActionType.Signin) .ofType(ActionType.Signin)
.map((action: Signin) => action.payload) .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) => { .map((domainMember: DomainMember) => {
return new SigninSuccess(domainMember); return new SigninSuccess(domainMember);
}) })
@ -52,13 +57,16 @@ export class Effects {
@Effect({ dispatch: false }) @Effect({ dispatch: false })
signinSuccess$ = this.actions$ signinSuccess$ = this.actions$
.ofType(ActionType.SigninSuccess) .ofType(ActionType.SigninSuccess)
.do(() => this.router.navigate(['/'])); .do(() => {
this.router.navigateByUrl(this._options.returnURL);
});
@Effect({ dispatch: false }) @Effect({ dispatch: false })
signinRedirect$ = this.actions$ signinRedirect$ = this.actions$
.ofType(ActionType.SigninRedirect, ActionType.Signout) .ofType(ActionType.SigninRedirect, ActionType.Signout)
.do(authed => { .map((action: SigninRedirect) => action.payload)
this.router.navigate(['/auth/signin']); .do(returnURL => {
this.router.navigate(['/auth/signin'], {queryParams: {returnURL: returnURL}});
}); });
} }

View File

@ -51,7 +51,7 @@ export function reducer(state = initialState, action: Actions): State {
}; };
} }
case ActionType.SigninSuccess: { case ActionType.SignoutSuccess: {
return { return {
...state, ...state,
signined: false, signined: false,
@ -62,7 +62,7 @@ export function reducer(state = initialState, action: Actions): State {
}; };
} }
case ActionType.SigninFailure: { case ActionType.SignoutFailure: {
return { return {
...state, ...state,
error: action.payload, error: action.payload,