ing
This commit is contained in:
parent
14a4acb784
commit
2b323cd382
|
@ -6,55 +6,60 @@ import {
|
||||||
RouterStateSnapshot,
|
RouterStateSnapshot,
|
||||||
Router,
|
Router,
|
||||||
} from '@angular/router';
|
} from '@angular/router';
|
||||||
import { Store } from '@ngrx/store';
|
|
||||||
|
|
||||||
import { Observable } from 'rxjs/Observable';
|
|
||||||
import 'rxjs/add/operator/take';
|
|
||||||
import 'rxjs/add/operator/map';
|
|
||||||
|
|
||||||
|
import { Store, select } from '@ngrx/store';
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
import { map, take } from 'rxjs/operators';
|
||||||
import { CookieService } from 'ngx-cookie-service';
|
import { CookieService } from 'ngx-cookie-service';
|
||||||
|
|
||||||
import * as AuthStore from '@overflow/member/store/auth';
|
import { AuthContainerSelector } from '@overflow/shared/auth/store';
|
||||||
import { AuthSelector } from '@overflow/member/store';
|
import * as MemberEntityStore from '@overflow/member/store/entity/member';
|
||||||
|
import * as SigninContaifnerStore from '../store/container/signin';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthGuard implements CanActivate, CanActivateChild {
|
export class AuthGuard implements CanActivate, CanActivateChild {
|
||||||
constructor(
|
constructor(
|
||||||
private store: Store<AuthStore.State>,
|
private store: Store<any>,
|
||||||
private router: Router,
|
private router: Router,
|
||||||
private cookieService: CookieService,
|
private cookieService: CookieService,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
|
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
|
||||||
return this.store
|
return this.store.pipe(
|
||||||
.select(AuthSelector.select('signined'))
|
select(AuthContainerSelector.selectSignined),
|
||||||
.map(signined => {
|
map(signined => {
|
||||||
if (!signined) {
|
if (!signined) {
|
||||||
if (this.cookieService.check('authToken')) {
|
if (this.cookieService.check('authToken')) {
|
||||||
this.store.dispatch(new AuthStore.SigninCookie({authToken: this.cookieService.get('authToken'), returnURL: state.url}));
|
this.store.dispatch(new MemberEntityStore.SigninCookie({authToken: this.cookieService.get('authToken'), returnURL: state.url}));
|
||||||
} else {
|
} else {
|
||||||
// this.store.dispatch(new AuthStore.SigninRedirect(state.url));
|
this.store.dispatch(new SigninContaifnerStore.SigninRedirect({returnURL: state.url}));
|
||||||
this.router.navigateByUrl(state.url);
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
})
|
}),
|
||||||
.take(1);
|
take(1)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
canActivateChild(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
|
canActivateChild(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
|
||||||
return this.store
|
return this.store.pipe(
|
||||||
.select(AuthSelector.select('signined'))
|
select(AuthContainerSelector.selectSignined),
|
||||||
.map(signined => {
|
map(signined => {
|
||||||
if (!signined) {
|
if (!signined) {
|
||||||
this.store.dispatch(new AuthStore.SigninRedirect(state.url));
|
if (this.cookieService.check('authToken')) {
|
||||||
|
this.store.dispatch(new MemberEntityStore.SigninCookie({authToken: this.cookieService.get('authToken'), returnURL: state.url}));
|
||||||
|
} else {
|
||||||
|
this.store.dispatch(new SigninContaifnerStore.SigninRedirect({returnURL: state.url}));
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
})
|
}),
|
||||||
.take(1);
|
take(1)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
2
src/app/commons/store/container/signin/index.ts
Normal file
2
src/app/commons/store/container/signin/index.ts
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
export * from './signin.action';
|
||||||
|
export * from './signin.effect';
|
15
src/app/commons/store/container/signin/signin.action.ts
Normal file
15
src/app/commons/store/container/signin/signin.action.ts
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import { Action } from '@ngrx/store';
|
||||||
|
|
||||||
|
export enum ActionType {
|
||||||
|
SigninRedirect = '[app.signin] SigninRedirect',
|
||||||
|
}
|
||||||
|
|
||||||
|
export class SigninRedirect implements Action {
|
||||||
|
readonly type = ActionType.SigninRedirect;
|
||||||
|
|
||||||
|
constructor(public payload: {returnURL: string}) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Actions =
|
||||||
|
| SigninRedirect
|
||||||
|
;
|
|
@ -1,8 +1,8 @@
|
||||||
import { TestBed, inject } from '@angular/core/testing';
|
import { TestBed, inject } from '@angular/core/testing';
|
||||||
|
|
||||||
import { Effects } from './signin-init.effect';
|
import { Effects } from './signin.effect';
|
||||||
|
|
||||||
describe('SigninInit.Effects', () => {
|
describe('signin-container.Effects', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
providers: [Effects]
|
providers: [Effects]
|
72
src/app/commons/store/container/signin/signin.effect.ts
Normal file
72
src/app/commons/store/container/signin/signin.effect.ts
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { Router } from '@angular/router';
|
||||||
|
|
||||||
|
import { Effect, Actions, ofType } from '@ngrx/effects';
|
||||||
|
|
||||||
|
import { map, tap } from 'rxjs/operators';
|
||||||
|
|
||||||
|
import { CookieService } from 'ngx-cookie-service';
|
||||||
|
import { RPCService } from '@loafer/ng-rpc';
|
||||||
|
|
||||||
|
import {
|
||||||
|
SigninSuccess,
|
||||||
|
SigninCookieSuccess,
|
||||||
|
ActionType as AuthActionType,
|
||||||
|
} from '@overflow/shared/auth/store/container/auth';
|
||||||
|
|
||||||
|
import {
|
||||||
|
SigninRedirect,
|
||||||
|
ActionType,
|
||||||
|
} from './signin.action';
|
||||||
|
|
||||||
|
import { DomainMember } from '@overflow/commons-typescript/model/domain';
|
||||||
|
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class Effects {
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private actions$: Actions,
|
||||||
|
private rpcService: RPCService,
|
||||||
|
private cookieService: CookieService,
|
||||||
|
private router: Router
|
||||||
|
) { }
|
||||||
|
|
||||||
|
@Effect({ dispatch: false })
|
||||||
|
signinSuccess$ = this.actions$.pipe(
|
||||||
|
ofType(AuthActionType.SigninSuccess),
|
||||||
|
map((action: SigninSuccess) => action.payload),
|
||||||
|
tap((info: {authToken: string, domainMember: DomainMember, returnURL: string}) => {
|
||||||
|
const expires = new Date();
|
||||||
|
expires.setDate(expires.getDate() + 1);
|
||||||
|
this.cookieService.set('authToken', info.authToken, expires, '/');
|
||||||
|
const queryString = `authToken=${info.authToken}`;
|
||||||
|
this.rpcService.connect(queryString);
|
||||||
|
|
||||||
|
this.router.navigateByUrl(info.returnURL);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
@Effect({ dispatch: false })
|
||||||
|
signinCookieSuccess$ = this.actions$.pipe(
|
||||||
|
ofType(AuthActionType.SigninCookieSuccess),
|
||||||
|
map((action: SigninCookieSuccess) => action.payload),
|
||||||
|
tap((info: {domainMember: DomainMember, returnURL: string}) => {
|
||||||
|
const authToken = this.cookieService.get('authToken');
|
||||||
|
const queryString = `authToken=${authToken}`;
|
||||||
|
this.rpcService.connect(queryString);
|
||||||
|
|
||||||
|
this.router.navigateByUrl(info.returnURL);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
@Effect({ dispatch: false })
|
||||||
|
signinRedirect$ = this.actions$.pipe(
|
||||||
|
ofType(ActionType.SigninRedirect),
|
||||||
|
map((action: SigninRedirect) => action.payload),
|
||||||
|
tap((info: {returnURL: string}) => {
|
||||||
|
this.router.navigate(['/auth/signin'], {queryParams: {returnURL: info.returnURL}});
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
import { ActionReducerMap } from '@ngrx/store';
|
import { ActionReducerMap } from '@ngrx/store';
|
||||||
|
|
||||||
import * as SigninInitStore from './signin-init';
|
import * as SigninContainerStore from './container/signin';
|
||||||
|
|
||||||
export interface State {
|
export interface State {
|
||||||
}
|
}
|
||||||
|
@ -9,5 +9,5 @@ export const REDUCERS: ActionReducerMap<State> = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const EFFECTS = [
|
export const EFFECTS = [
|
||||||
SigninInitStore.Effects,
|
SigninContainerStore.Effects,
|
||||||
];
|
];
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
export * from './signin-init.effect';
|
|
|
@ -1,68 +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 { CookieService } from 'ngx-cookie-service';
|
|
||||||
|
|
||||||
import { RPCService } from '@loafer/ng-rpc';
|
|
||||||
|
|
||||||
import {
|
|
||||||
SigninSuccess,
|
|
||||||
SigninCookieSuccess,
|
|
||||||
ActionType,
|
|
||||||
} from '@overflow/member/store/auth';
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class Effects {
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
private actions$: Actions,
|
|
||||||
private rpcService: RPCService,
|
|
||||||
private cookieService: CookieService,
|
|
||||||
) { }
|
|
||||||
|
|
||||||
@Effect({ dispatch: false })
|
|
||||||
signinSuccess$ = this.actions$
|
|
||||||
.ofType(ActionType.SigninSuccess)
|
|
||||||
.map((action: SigninSuccess) => action.payload)
|
|
||||||
.do(
|
|
||||||
(result) => {
|
|
||||||
const authToken = result.authToken;
|
|
||||||
// console.log(`authToken: ${authToken}`);
|
|
||||||
|
|
||||||
const expires = new Date();
|
|
||||||
expires.setDate(expires.getDate() + 1);
|
|
||||||
this.cookieService.set('authToken', authToken, expires, '/');
|
|
||||||
|
|
||||||
const queryString = `authToken=${authToken}`;
|
|
||||||
|
|
||||||
this.rpcService.connect(queryString);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
@Effect({ dispatch: false })
|
|
||||||
signinCookieSuccess$ = this.actions$
|
|
||||||
.ofType(ActionType.SigninCookieSuccess)
|
|
||||||
.map((action: SigninCookieSuccess) => action.payload)
|
|
||||||
.do(
|
|
||||||
(result) => {
|
|
||||||
const authToken = this.cookieService.get('authToken');
|
|
||||||
// console.log(`authToken: ${authToken}`);
|
|
||||||
const queryString = `authToken=${authToken}`;
|
|
||||||
|
|
||||||
this.rpcService.connect(queryString);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user