member_webapp/@overflow/member/store/entity/member-totp/member-totp.effect.ts
2018-05-28 21:37:47 +09:00

82 lines
2.2 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Effect, Actions, ofType } from '@ngrx/effects';
import { of } from 'rxjs/observable/of';
import { catchError, exhaustMap, map } from 'rxjs/operators';
import { MemberTotpService } from '../../../service/member-totp.service';
import {
CreateTotp,
CreateTotpSuccess,
CreateTotpFailure,
Regist,
RegistSuccess,
RegistFailure,
CheckCodeForMember,
CheckCodeForMemberSuccess,
CheckCodeForMemberFailure,
ActionType,
} from './member-totp.action';
import {Member} from '@overflow/commons-typescript/model/member';
@Injectable()
export class Effects {
constructor(
private actions$: Actions,
private memberTotpService: MemberTotpService,
) { }
@Effect()
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$ = 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$ = 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);
}),
catchError(error => of(new CheckCodeForMemberFailure(error)))
)
)
);
}