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 { 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 { Store, select } from '@ngrx/store';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';

View File

@ -13,8 +13,8 @@ export class MemberTotpService {
} }
public createTotp(member: Member): Observable<MemberTotp[]> { public createTotp(member: Member): Observable<any> {
return this.rpcService.call<MemberTotp[]>('MemberTotpService.createTotp', member); return this.rpcService.call<any>('MemberTotpService.createTotp', member);
} }
public regist(member: Member, secretCode: string, code: string): Observable<boolean> { 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 { export class RegistSuccess implements Action {
readonly type = ActionType.RegistSuccess; readonly type = ActionType.RegistSuccess;
constructor(public payload: void) {}
} }
export class RegistFailure implements Action { export class RegistFailure implements Action {
@ -67,7 +65,7 @@ export class CheckCodeForMember implements Action {
export class CheckCodeForMemberSuccess implements Action { export class CheckCodeForMemberSuccess implements Action {
readonly type = ActionType.CheckCodeForMemberSuccess; readonly type = ActionType.CheckCodeForMemberSuccess;
constructor(public payload: void) {} constructor(public payload: boolean) {}
} }
export class CheckCodeForMemberFailure implements Action { export class CheckCodeForMemberFailure implements Action {

View File

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

View File

@ -2,15 +2,11 @@ import { Injectable } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { Effect, Actions, ofType } from '@ngrx/effects'; import { Effect, Actions, ofType } from '@ngrx/effects';
import { Action } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs'; import { of } from 'rxjs';
import { catchError, exhaustMap, map, tap } from 'rxjs/operators'; import { catchError, exhaustMap, map, tap } from 'rxjs/operators';
import { Member } from '@overflow/commons-typescript/model/member'; import { Member } from '@overflow/commons-typescript/model/member';
import { MemberService } from '../../../service/member.service'; import { MemberService } from '../../../service/member.service';
import { RESTClientError } from '@loafer/ng-rest';
import { import {
Signin, Signin,