ing
This commit is contained in:
parent
14a4acb784
commit
2b323cd382
|
@ -6,55 +6,60 @@ import {
|
|||
RouterStateSnapshot,
|
||||
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 * as AuthStore from '@overflow/member/store/auth';
|
||||
import { AuthSelector } from '@overflow/member/store';
|
||||
import { AuthContainerSelector } from '@overflow/shared/auth/store';
|
||||
import * as MemberEntityStore from '@overflow/member/store/entity/member';
|
||||
import * as SigninContaifnerStore from '../store/container/signin';
|
||||
|
||||
@Injectable()
|
||||
export class AuthGuard implements CanActivate, CanActivateChild {
|
||||
constructor(
|
||||
private store: Store<AuthStore.State>,
|
||||
private store: Store<any>,
|
||||
private router: Router,
|
||||
private cookieService: CookieService,
|
||||
) { }
|
||||
|
||||
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
|
||||
return this.store
|
||||
.select(AuthSelector.select('signined'))
|
||||
.map(signined => {
|
||||
return this.store.pipe(
|
||||
select(AuthContainerSelector.selectSignined),
|
||||
map(signined => {
|
||||
if (!signined) {
|
||||
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 {
|
||||
// this.store.dispatch(new AuthStore.SigninRedirect(state.url));
|
||||
this.router.navigateByUrl(state.url);
|
||||
this.store.dispatch(new SigninContaifnerStore.SigninRedirect({returnURL: state.url}));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
})
|
||||
.take(1);
|
||||
}),
|
||||
take(1)
|
||||
);
|
||||
}
|
||||
|
||||
canActivateChild(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
|
||||
return this.store
|
||||
.select(AuthSelector.select('signined'))
|
||||
.map(signined => {
|
||||
return this.store.pipe(
|
||||
select(AuthContainerSelector.selectSignined),
|
||||
map(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 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 { Effects } from './signin-init.effect';
|
||||
import { Effects } from './signin.effect';
|
||||
|
||||
describe('SigninInit.Effects', () => {
|
||||
describe('signin-container.Effects', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
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 * as SigninInitStore from './signin-init';
|
||||
import * as SigninContainerStore from './container/signin';
|
||||
|
||||
export interface State {
|
||||
}
|
||||
|
@ -9,5 +9,5 @@ export const REDUCERS: ActionReducerMap<State> = {
|
|||
};
|
||||
|
||||
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