This commit is contained in:
crusader 2018-03-13 18:23:14 +09:00
parent dfd124f62c
commit 7bf2087484

View File

@ -8,10 +8,10 @@ import {
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 { CookieService } from 'ngx-cookie-service';
import * as AuthStore from 'packages/member/store/auth';
import { AuthSelector } from 'packages/member/store';
@ -19,7 +19,8 @@ import { AuthSelector } from 'packages/member/store';
@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild {
constructor(
private store: Store<AuthStore.State>
private store: Store<AuthStore.State>,
private cookieService: CookieService,
) { }
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
@ -27,7 +28,11 @@ export class AuthGuard implements CanActivate, CanActivateChild {
.select(AuthSelector.select('signined'))
.map(signined => {
if (!signined) {
if (this.cookieService.check('authToken')) {
this.store.dispatch(new AuthStore.SigninCookie({authToken: this.cookieService.get('authToken'), returnURL: state.url}));
} else {
this.store.dispatch(new AuthStore.SigninRedirect(state.url));
}
return false;
}
@ -47,10 +52,6 @@ export class AuthGuard implements CanActivate, CanActivateChild {
return true;
})
.catch(() => {
this.store.dispatch(new AuthStore.SigninRedirect(state.url));
return of(false);
})
.take(1);
}
}