57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import {
|
|
CanActivate,
|
|
CanActivateChild,
|
|
ActivatedRouteSnapshot,
|
|
RouterStateSnapshot
|
|
} from '@angular/router';
|
|
import { Store } from '@ngrx/store';
|
|
|
|
import { Observable } from 'rxjs/Observable';
|
|
import 'rxjs/add/operator/catch';
|
|
import 'rxjs/add/operator/take';
|
|
import 'rxjs/add/operator/map';
|
|
import { of } from 'rxjs/observable/of';
|
|
|
|
import * as AuthStore from 'packages/member/store/auth';
|
|
import { AuthSelector } from 'packages/member/store';
|
|
|
|
@Injectable()
|
|
export class AuthGuard implements CanActivate, CanActivateChild {
|
|
constructor(
|
|
private store: Store<AuthStore.State>
|
|
) { }
|
|
|
|
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
|
|
return this.store
|
|
.select(AuthSelector.select('signined'))
|
|
.map(signined => {
|
|
if (!signined) {
|
|
this.store.dispatch(new AuthStore.SigninRedirect(state.url));
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
})
|
|
.take(1);
|
|
}
|
|
|
|
canActivateChild(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
|
|
return this.store
|
|
.select(AuthSelector.select('signined'))
|
|
.map(signined => {
|
|
if (!signined) {
|
|
this.store.dispatch(new AuthStore.SigninRedirect(state.url));
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
})
|
|
.catch(() => {
|
|
this.store.dispatch(new AuthStore.SigninRedirect(state.url));
|
|
return of(false);
|
|
})
|
|
.take(1);
|
|
}
|
|
}
|