ing
This commit is contained in:
18
@overflow/shared/auth/auth-store.module.ts
Normal file
18
@overflow/shared/auth/auth-store.module.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { StoreModule } from '@ngrx/store';
|
||||
import { EffectsModule } from '@ngrx/effects';
|
||||
|
||||
import {
|
||||
REDUCERS,
|
||||
EFFECTS,
|
||||
} from './store';
|
||||
|
||||
import { MODULE } from './auth.constant';
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
StoreModule.forFeature(MODULE.name, REDUCERS),
|
||||
EffectsModule.forFeature(EFFECTS),
|
||||
],
|
||||
})
|
||||
export class AuthStoreModule { }
|
||||
@@ -1,9 +1,11 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { AuthStoreModule } from './auth-store.module';
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
CommonModule,
|
||||
AuthStoreModule,
|
||||
],
|
||||
})
|
||||
export class AuthModule { }
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
import { TestBed, inject } from '@angular/core/testing';
|
||||
|
||||
import { Effects } from './auth.effect';
|
||||
|
||||
describe('Auth.Effects', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [Effects]
|
||||
});
|
||||
});
|
||||
|
||||
it('should be created', inject([Effects], (effects: Effects) => {
|
||||
expect(effects).toBeTruthy();
|
||||
}));
|
||||
});
|
||||
@@ -1,107 +0,0 @@
|
||||
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 '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 { DomainMember } from '@overflow/commons-typescript/model/domain';
|
||||
|
||||
import { Member } from '@overflow/commons-typescript/model/member';
|
||||
import { MemberService } from '../../service/member.service';
|
||||
|
||||
import {
|
||||
Signin,
|
||||
SigninSuccess,
|
||||
SigninFailure,
|
||||
SigninRedirect,
|
||||
SigninCookie,
|
||||
SigninCookieSuccess,
|
||||
SigninCookieFailure,
|
||||
|
||||
ActionType,
|
||||
} from './auth.action';
|
||||
|
||||
@Injectable()
|
||||
export class Effects {
|
||||
private _returnURL: any;
|
||||
|
||||
constructor(
|
||||
private actions$: Actions,
|
||||
private memberService: MemberService,
|
||||
private router: Router
|
||||
) { }
|
||||
|
||||
@Effect()
|
||||
signin$: Observable<Action> = this.actions$
|
||||
.ofType(ActionType.Signin)
|
||||
.map((action: Signin) => action.payload)
|
||||
.switchMap(payload => {
|
||||
this._returnURL = payload.returnURL;
|
||||
return this.memberService.signin(payload.email, payload.password);
|
||||
})
|
||||
.map((result: any) => {
|
||||
const authToken = result['authToken'];
|
||||
const domainMember = result['domainMember'];
|
||||
return new SigninSuccess({authToken: authToken, domainMember: domainMember});
|
||||
})
|
||||
.catch((error: RESTClientError) => {
|
||||
return of(new SigninFailure(error));
|
||||
});
|
||||
|
||||
@Effect({ dispatch: false })
|
||||
signinSuccess$ = this.actions$
|
||||
.ofType(ActionType.SigninSuccess)
|
||||
.do(() => {
|
||||
this.router.navigateByUrl(this._returnURL);
|
||||
});
|
||||
|
||||
@Effect({ dispatch: false })
|
||||
signinRedirect$ = this.actions$
|
||||
.ofType(ActionType.SigninRedirect, ActionType.Signout)
|
||||
.map((action: SigninRedirect) => action.payload)
|
||||
.do(returnURL => {
|
||||
this.router.navigate(['/auth/signin'], {queryParams: {returnURL: returnURL}});
|
||||
});
|
||||
|
||||
|
||||
@Effect()
|
||||
signinCookie$: Observable<Action> = this.actions$
|
||||
.ofType(ActionType.SigninCookie)
|
||||
.map((action: SigninCookie) => action.payload)
|
||||
.switchMap((payload) => {
|
||||
this._returnURL = payload.returnURL;
|
||||
return this.memberService.signin_cookie(payload.authToken);
|
||||
})
|
||||
.map((domainMember: DomainMember) => {
|
||||
return new SigninCookieSuccess(domainMember);
|
||||
})
|
||||
.catch((error: RESTClientError) => {
|
||||
return of(new SigninFailure(error));
|
||||
});
|
||||
|
||||
@Effect({ dispatch: false })
|
||||
signinCookieSuccess$ = this.actions$
|
||||
.ofType(ActionType.SigninCookieSuccess)
|
||||
.do(() => {
|
||||
this.router.navigateByUrl(this._returnURL);
|
||||
});
|
||||
|
||||
@Effect({ dispatch: false })
|
||||
signinCookieFailure$ = this.actions$
|
||||
.ofType(ActionType.SigninCookieFailure)
|
||||
.map(() => {
|
||||
return new SigninRedirect(this._returnURL);
|
||||
});
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
import {
|
||||
Actions,
|
||||
ActionType,
|
||||
Signin,
|
||||
} from './auth.action';
|
||||
|
||||
import {
|
||||
State,
|
||||
initialState,
|
||||
} from './auth.state';
|
||||
|
||||
import { Member } from '@overflow/commons-typescript/model/member';
|
||||
import { DomainMember } from '@overflow/commons-typescript/model/domain';
|
||||
|
||||
export function reducer(state = initialState, action: Actions): State {
|
||||
switch (action.type) {
|
||||
case ActionType.Signin: {
|
||||
return {
|
||||
...state,
|
||||
error: null,
|
||||
pending: true,
|
||||
};
|
||||
}
|
||||
|
||||
case ActionType.SigninSuccess: {
|
||||
const domainMember = action.payload.domainMember;
|
||||
return {
|
||||
...state,
|
||||
signined: true,
|
||||
error: null,
|
||||
pending: false,
|
||||
member: domainMember.member,
|
||||
domain: domainMember.domain,
|
||||
};
|
||||
}
|
||||
|
||||
case ActionType.SigninFailure: {
|
||||
return {
|
||||
...state,
|
||||
signined: false,
|
||||
error: action.payload,
|
||||
pending: false,
|
||||
member: null,
|
||||
domain: null,
|
||||
};
|
||||
}
|
||||
|
||||
case ActionType.SigninCookieSuccess: {
|
||||
return {
|
||||
...state,
|
||||
signined: true,
|
||||
error: null,
|
||||
pending: false,
|
||||
member: action.payload.member,
|
||||
domain: action.payload.domain,
|
||||
};
|
||||
}
|
||||
|
||||
case ActionType.SigninCookieFailure: {
|
||||
return {
|
||||
...state,
|
||||
signined: false,
|
||||
error: action.payload,
|
||||
pending: false,
|
||||
member: null,
|
||||
domain: null,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
case ActionType.Signout: {
|
||||
return {
|
||||
...state,
|
||||
error: null,
|
||||
pending: true,
|
||||
};
|
||||
}
|
||||
|
||||
case ActionType.SignoutSuccess: {
|
||||
return {
|
||||
...state,
|
||||
signined: false,
|
||||
error: null,
|
||||
pending: false,
|
||||
member: null,
|
||||
domain: null,
|
||||
};
|
||||
}
|
||||
|
||||
case ActionType.SignoutFailure: {
|
||||
return {
|
||||
...state,
|
||||
error: action.payload,
|
||||
pending: false,
|
||||
};
|
||||
}
|
||||
|
||||
default: {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
import { RESTClientError } from '@loafer/ng-rest';
|
||||
import { Domain } from '@overflow/commons-typescript/model/domain';
|
||||
import { Member } from '@overflow/commons-typescript/model/member';
|
||||
|
||||
export interface State {
|
||||
signined: boolean;
|
||||
error: RESTClientError | null;
|
||||
pending: boolean;
|
||||
member: Member | null;
|
||||
domain: Domain | null;
|
||||
}
|
||||
|
||||
export const initialState: State = {
|
||||
signined: false,
|
||||
error: null,
|
||||
pending: false,
|
||||
member: null,
|
||||
domain: null,
|
||||
};
|
||||
@@ -3,27 +3,17 @@ import { Action } from '@ngrx/store';
|
||||
import { RESTClientError } from '@loafer/ng-rest';
|
||||
|
||||
import { DomainMember } from '@overflow/commons-typescript/model/domain';
|
||||
import { Member } from '@overflow/commons-typescript/model/member';
|
||||
|
||||
export enum ActionType {
|
||||
Signin = '[member.auth] Signin',
|
||||
SigninSuccess = '[member.auth] SigninSuccess',
|
||||
SigninFailure = '[member.auth] SigninFailure',
|
||||
SigninRedirect = '[member.auth] SigninRedirect',
|
||||
SigninSuccess = '[auth.auth] SigninSuccess',
|
||||
SigninFailure = '[auth.auth] SigninFailure',
|
||||
SigninRedirect = '[auth.auth] SigninRedirect',
|
||||
|
||||
SigninCookie = '[member.auth] SigninCookie',
|
||||
SigninCookieSuccess = '[member.auth] SigninCookieSuccess',
|
||||
SigninCookieFailure = '[member.auth] SigninCookieFailure',
|
||||
SigninCookieSuccess = '[auth.auth] SigninCookieSuccess',
|
||||
SigninCookieFailure = '[auth.auth] SigninCookieFailure',
|
||||
|
||||
Signout = '[member.auth] Signout',
|
||||
SignoutSuccess = '[member.auth] SignoutSuccess',
|
||||
SignoutFailure = '[member.auth] SignoutFailure',
|
||||
}
|
||||
|
||||
export class Signin implements Action {
|
||||
readonly type = ActionType.Signin;
|
||||
|
||||
constructor(public payload: {email: string, password: string, returnURL: string}) {}
|
||||
SignoutSuccess = '[auth.auth] SignoutSuccess',
|
||||
SignoutFailure = '[auth.auth] SignoutFailure',
|
||||
}
|
||||
|
||||
export class SigninSuccess implements Action {
|
||||
@@ -43,12 +33,6 @@ export class SigninRedirect implements Action {
|
||||
constructor(public payload: string) {}
|
||||
}
|
||||
|
||||
export class SigninCookie implements Action {
|
||||
readonly type = ActionType.SigninCookie;
|
||||
|
||||
constructor(public payload: {authToken: string, returnURL: string}) {}
|
||||
}
|
||||
|
||||
export class SigninCookieSuccess implements Action {
|
||||
readonly type = ActionType.SigninCookieSuccess;
|
||||
|
||||
@@ -61,10 +45,6 @@ export class SigninCookieFailure implements Action {
|
||||
constructor(public payload: RESTClientError) {}
|
||||
}
|
||||
|
||||
export class Signout implements Action {
|
||||
readonly type = ActionType.Signout;
|
||||
}
|
||||
|
||||
export class SignoutSuccess implements Action {
|
||||
readonly type = ActionType.SignoutSuccess;
|
||||
}
|
||||
@@ -76,14 +56,11 @@ export class SignoutFailure implements Action {
|
||||
}
|
||||
|
||||
export type Actions =
|
||||
| Signin
|
||||
| SigninSuccess
|
||||
| SigninFailure
|
||||
| SigninRedirect
|
||||
| SigninCookie
|
||||
| SigninCookieSuccess
|
||||
| SigninCookieFailure
|
||||
| Signout
|
||||
| SignoutSuccess
|
||||
| SignoutFailure
|
||||
;
|
||||
58
@overflow/shared/auth/store/container/auth/auth.reducer.ts
Normal file
58
@overflow/shared/auth/store/container/auth/auth.reducer.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import {
|
||||
Actions,
|
||||
ActionType,
|
||||
} from './auth.action';
|
||||
|
||||
import {
|
||||
State,
|
||||
initialState,
|
||||
} from './auth.state';
|
||||
|
||||
export function reducer(state: State = initialState, action: Actions): State {
|
||||
switch (action.type) {
|
||||
case ActionType.SigninSuccess: {
|
||||
return {
|
||||
signined: true,
|
||||
domainMember: action.payload.domainMember,
|
||||
};
|
||||
}
|
||||
|
||||
case ActionType.SigninFailure: {
|
||||
return {
|
||||
signined: false,
|
||||
domainMember: null,
|
||||
};
|
||||
}
|
||||
|
||||
case ActionType.SigninCookieSuccess: {
|
||||
return {
|
||||
signined: true,
|
||||
domainMember: action.payload,
|
||||
};
|
||||
}
|
||||
|
||||
case ActionType.SigninCookieFailure: {
|
||||
return {
|
||||
signined: false,
|
||||
domainMember: null,
|
||||
};
|
||||
}
|
||||
|
||||
case ActionType.SignoutSuccess: {
|
||||
return {
|
||||
signined: false,
|
||||
domainMember: null,
|
||||
};
|
||||
}
|
||||
|
||||
case ActionType.SignoutFailure: {
|
||||
return {
|
||||
...state,
|
||||
};
|
||||
}
|
||||
|
||||
default: {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
}
|
||||
19
@overflow/shared/auth/store/container/auth/auth.state.ts
Normal file
19
@overflow/shared/auth/store/container/auth/auth.state.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Selector, createSelector } from '@ngrx/store';
|
||||
import { DomainMember } from '@overflow/commons-typescript/model/domain';
|
||||
|
||||
export interface State {
|
||||
signined: boolean;
|
||||
domainMember: DomainMember | null;
|
||||
}
|
||||
|
||||
export const initialState: State = {
|
||||
signined: false,
|
||||
domainMember: null,
|
||||
};
|
||||
|
||||
export function getSelectors<S>(selector: Selector<any, State>) {
|
||||
return {
|
||||
selectSignined: createSelector(selector, (state: State) => state.signined),
|
||||
selectDomainMember: createSelector(selector, (state: State) => state.domainMember),
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
export * from './auth.action';
|
||||
export * from './auth.effect';
|
||||
export * from './auth.reducer';
|
||||
export * from './auth.state';
|
||||
26
@overflow/shared/auth/store/index.ts
Normal file
26
@overflow/shared/auth/store/index.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import {
|
||||
createSelector,
|
||||
createFeatureSelector,
|
||||
} from '@ngrx/store';
|
||||
|
||||
import { MODULE } from '../auth.constant';
|
||||
|
||||
import * as AuthContainerStore from './container/auth';
|
||||
|
||||
export interface State {
|
||||
auth_container: AuthContainerStore.State;
|
||||
}
|
||||
|
||||
export const REDUCERS = {
|
||||
auth_container: AuthContainerStore.reducer,
|
||||
};
|
||||
|
||||
export const EFFECTS = [
|
||||
];
|
||||
|
||||
export const selectState = createFeatureSelector<State>(MODULE.name);
|
||||
|
||||
export const AuthContainerSelector = AuthContainerStore.getSelectors(createSelector(
|
||||
selectState,
|
||||
(state: State) => state.auth_container
|
||||
));
|
||||
@@ -1,3 +0,0 @@
|
||||
export const MODULE = {
|
||||
name: 'Target'
|
||||
};
|
||||
@@ -1,9 +0,0 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
CommonModule,
|
||||
],
|
||||
})
|
||||
export class SharedModule { }
|
||||
Reference in New Issue
Block a user