This commit is contained in:
geek 2018-05-28 21:37:47 +09:00
parent cc45f35959
commit eb374a388b
9 changed files with 119 additions and 68 deletions

View File

@ -1,6 +1,4 @@
import { Component, OnInit, Input } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Store, select } from '@ngrx/store';
import { Observable } from 'rxjs';

View File

@ -13,8 +13,8 @@ export class MemberTotpService {
}
public createTotp(member: Member): Observable<MemberTotp[]> {
return this.rpcService.call<MemberTotp[]>('MemberTotpService.createTotp', member);
public createTotp(member: Member): Observable<any> {
return this.rpcService.call<any>('MemberTotpService.createTotp', member);
}
public regist(member: Member, secretCode: string, code: string): Observable<boolean> {

View File

@ -0,0 +1,3 @@
export * from './member-totp.reducer';
export * from './member-totp.state';

View File

@ -0,0 +1,44 @@
import {
Actions,
ActionType,
} from '../../entity/member-totp/member-totp.action';
import {
State,
initialState,
} from './member-totp.state';
export function reducer(state = initialState, action: Actions): State {
switch (action.type) {
case ActionType.CreateTotp: {
return {
secretKey: null,
sourceURI: null,
pending: true,
error: null,
};
}
case ActionType.CreateTotpSuccess: {
return {
secretKey: action.payload.key,
sourceURI: action.payload.uri,
pending: false,
error: null,
};
}
case ActionType.CreateTotpFailure: {
return {
secretKey: null,
sourceURI: null,
pending: false,
error: action.payload,
};
}
default: {
return state;
}
}
}

View File

@ -0,0 +1,25 @@
import { Selector, createSelector } from '@ngrx/store';
import { RESTClientError } from '@loafer/ng-rest';
export interface State {
secretKey: string;
sourceURI: string;
pending: boolean;
error: RESTClientError;
}
export const initialState: State = {
secretKey: null,
sourceURI: null,
pending: false,
error: null,
};
export function getSelectors(selector: Selector<any, State>) {
return {
selectSecretKey: createSelector(selector, (state: State) => state.secretKey),
selectSourceURI: createSelector(selector, (state: State) => state.sourceURI),
selectPending: createSelector(selector, (state: State) => state.pending),
selectError: createSelector(selector, (state: State) => state.error),
};
}

View File

@ -46,8 +46,6 @@ export class Regist implements Action {
export class RegistSuccess implements Action {
readonly type = ActionType.RegistSuccess;
constructor(public payload: void) {}
}
export class RegistFailure implements Action {
@ -67,7 +65,7 @@ export class CheckCodeForMember implements Action {
export class CheckCodeForMemberSuccess implements Action {
readonly type = ActionType.CheckCodeForMemberSuccess;
constructor(public payload: void) {}
constructor(public payload: boolean) {}
}
export class CheckCodeForMemberFailure implements Action {

View File

@ -1,20 +1,9 @@
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 { catchError, exhaustMap, map } from 'rxjs/operators';
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 { RESTClientError } from '@loafer/ng-rest';
import { MemberTotpService } from '../../../service/member-totp.service';
import {
@ -32,6 +21,7 @@ import {
ActionType,
} from './member-totp.action';
import {Member} from '@overflow/commons-typescript/model/member';
@Injectable()
export class Effects {
@ -39,53 +29,53 @@ export class Effects {
constructor(
private actions$: Actions,
private memberTotpService: MemberTotpService,
private router: Router
) { }
@Effect()
createTotp$: Observable<Action> = this.actions$
.ofType(ActionType.CreateTotp)
.map((action: CreateTotp) => action.payload)
.switchMap(payload => {
// this._returnURL = payload.returnURL;
return this.memberTotpService.createTotp(payload);
})
.map((result: any) => {
const key = result['key'];
const uri = result['uri'];
return new CreateTotpSuccess({ key: key, uri: uri });
})
.catch((error: RESTClientError) => {
return of(new CreateTotpFailure(error));
});
createTotp$ = this.actions$.pipe(
ofType(ActionType.CreateTotp),
map(( action: CreateTotp ) => action.payload),
exhaustMap(( member: Member ) =>
this.memberTotpService
.createTotp(member)
.pipe(
map((result: any) => {
return new CreateTotpSuccess({ key: result.secretKey, uri: result.sourceURI});
}),
catchError(error => of(new CreateTotpFailure(error)))
)
)
);
@Effect()
regist: Observable<Action> = this.actions$
.ofType(ActionType.Regist)
.map((action: Regist) => action.payload)
.switchMap((payload) => {
// this._returnURL = payload.returnURL;
return this.memberTotpService.regist(payload.member, payload.secretCode, payload.code);
})
.map((result: any) => {
return new RegistSuccess(result);
})
.catch((error: RESTClientError) => {
return of(new RegistFailure(error));
});
regist$ = this.actions$.pipe(
ofType(ActionType.Regist),
map(( action: Regist ) => action.payload),
exhaustMap(( totpInfo: { member: Member, secretCode: string, code: string } ) =>
this.memberTotpService
.regist(totpInfo.member, totpInfo.secretCode, totpInfo.code)
.pipe(
map(() => {
return new RegistSuccess();
}),
catchError(error => of(new RegistFailure(error)))
)
)
);
@Effect()
checkCodeForMember: Observable<Action> = this.actions$
.ofType(ActionType.CheckCodeForMember)
.map((action: Regist) => action.payload)
.switchMap((payload) => {
return this.memberTotpService.checkCodeForMember(payload.member, payload.code);
})
.map((result: any) => {
checkCodeForMember$ = this.actions$.pipe(
ofType(ActionType.CheckCodeForMember),
map(( action: CheckCodeForMember ) => action.payload),
exhaustMap(( totpInfo: { member: Member, code: string } ) =>
this.memberTotpService
.checkCodeForMember(totpInfo.member, totpInfo.code)
.pipe(
map((result: boolean) => {
return new CheckCodeForMemberSuccess(result);
})
.catch((error: RESTClientError) => {
return of(new CheckCodeForMemberFailure(error));
});
}),
catchError(error => of(new CheckCodeForMemberFailure(error)))
)
)
);
}

View File

@ -1,9 +1,6 @@
import { Action } from '@ngrx/store';
import { RESTClientError } from '@loafer/ng-rest';
import { Member } from '@overflow/commons-typescript/model/member';
import { DomainMember } from '@overflow/commons-typescript/model/domain';
export enum ActionType {
Signin = '[member.member] Signin',

View File

@ -2,15 +2,11 @@ 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';
import { catchError, exhaustMap, map, tap } from 'rxjs/operators';
import { Member } from '@overflow/commons-typescript/model/member';
import { MemberService } from '../../../service/member.service';
import { RESTClientError } from '@loafer/ng-rest';
import {
Signin,