Angular & Firebase - 可以创建一个身份验证保护来检查用户是否在登录屏幕上吗?



我在用什么

  • 火力基地

我想要实现的目标

  • 根据条件执行路由器重定向的身份验证防护

我必须去哪里

  • 我对许多组件都有一个身份验证保护,如果用户有一个书签(例如......(来url并在未登录时导航到它,他们将被推回登录页面。

问题

  • 是否可以修改我的身份验证保护,以便如果用户已登录,但以某种方式设法导航到"www.mydomain.com/login",他们被重定向到指定的路由?

我的路线卫士

我把注释掉的代码留在那里尝试了一些逻辑。这里的问题是,如果我使用注释掉的代码,如果用户没有登录,它最终会重定向回您已经使用的登录页面。

import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot, CanActivate, Router } from '@angular/router';
import { AuthService } from './auth.service';
//testing
import { Observable } from 'rxjs/Observable';
import { AngularFireAuth } from 'angularfire2/auth';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/take';
import 'rxjs/add/operator/do';
@Injectable()
export class AuthGuardService implements CanActivate {
constructor(
private authService: AuthService,
private router: Router,
private auth: AngularFireAuth
) { }

canActivate(): Observable<boolean> {
return this.auth.authState
.take(1)
.map(authState => !!authState)
.do(auth => !auth ? this.router.navigate(['/login']) : true);

}
// canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
//   return this.checkLoggedIn(state.url);
// }
// checkLoggedIn(url: string) {
//   if (this.authService.isLoggedIn()) {
//     console.log("yup, i'm logged in!");
//     return true;
//   } else {
//     this.router.navigate(['/login']);
//     console.log("nop, i'm not logged in!");
//     return false;
//   }
// }
}

路由上的身份验证防护示例

理想情况下,我希望在我的身份验证保护中有一些逻辑,这意味着我可以在登录路由上执行以下相同的操作

{
path: 'folders',
component: FolderListComponent,
resolve: { folderListData: FolderListResolver },
canActivate: [AuthGuardService]
},

更新多亏了@Vega,才更近了一点。现在会发生什么:

  • 登录页面>登录>重定向到正确的页面=太好了!
  • 在未登录时导航到登录以外的任何页面=重定向回登录页面=太好了!
  • 登录时导航到登录屏幕 = 卡住。它现在认为用户未登录 = 这需要修复

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
let newUrl = state.url;
if (newUrl === "/login") {
if (this.authService.isLoggedIn()) {
console.log('Login Page / Signed In');
return false;
}
else {
console.log('Login Page / Not Signed In');
return true;
}
}
else {
if (this.authService.isLoggedIn()) {
console.log('Other Page / Signed In');
return true
} else {
this.router.navigate(['/login']);
console.log('Other Page / Not Signed In');
return false;
}
}
}

更新:这可能不是更优雅的方式,但它有效,并且代码更少:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
let newUrl = state.url;
if (newUrl === "/login") {
if (this.authService.loggedIn()) {
//this.router.navigate([oldUrl]);
//logged and routing to /login => go back. this probably need to be improved!
this.location.back();
return false
}
else {
//not logged => go /login
return true
}
}
// here is the 'traditional' part of the guard
else {
if (this.authService.loggedIn()) {
return true
} else {
this.router.navigate(['/login']);
return false;
}
}
}

旧答案:

在主要组件中:

ngOnInit() {
if (isPlatformBrowser) {
this.routerSubscription = this.router.events
.filter(event => event instanceof NavigationEnd)
.subscribe(() => {
let url = window.location.toString();
if (url.indexOf("/login") && loggedIn)
//do whatever is necessary
}
else{
this.router.navigate(['login']);
}
});
}
}
ngOnDestroy() {
this.routerSubscription.unsubscribe();
}

或类似以下内容:

{ path: 'login', component: LoginComponent, canActivate: [AnOtherGuard] },

in AnOtherGuard

....
canActivate() {
if (this.authService.loggedIn()) {
this.location.back(); //or whatever is necessary
return false;
}
else {
return true;
}
}

最新更新