如何对所有页面实施Firebase身份验证?

时间:2020-03-01 22:07:06

标签: javascript html firebase firebase-authentication

我正在为我的网站使用Firebase身份验证。当用户想要创建一个新帐户,登录或注销时,他可以在登录页面上进行操作。但是,当我调用该函数以使当前用户进入另一个页面(索引)时,它为null->表示该用户尚未登录。当我返回登录页面时,该用户将正常登录。如何确保用户已登录网站上的所有页面?谢谢!

firebase.auth().onAuthChanged(function(user) {
    if(user) {
        console.log("logged in");
    } else {
        console.log("logged out");
    }
})

两个文件都链接到此javascript。

1 个答案:

答案 0 :(得分:1)

您必须使用功能with edges as ( select distinct t1.group_id as group_id_1, t2.group_id as group_id_2 from t t1 join t t2 on t1.value = t2.value ), cte as ( select e.group_id_1, e.group_id_2, convert(varchar(max), concat(',', group_id_1, ',', group_id_2)) as visited, 1 as lev from edges e union all select cte.group_id_1, e.group_id_2, concat(visited, e.group_id_2, ','), lev + 1 from cte join edges e on e.group_id_1 = cte.group_id_2 where cte.visited not like concat('%,', e.group_id_2, ',%') and lev < 5 ) select group_id_1, dense_rank() over (order by min(group_id_2)) as master_group from cte group by group_id_1;

添加一个保护文件

guard.ts

CanActivate

auth.service.ts

import { Injectable } from '@angular/core';  
    import {  CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';  
    import { Observable } from 'rxjs';  
    import { AuthService } from 'src/app/services/auth.service';

    @Injectable({   providedIn: 'root' }) export class AdminGuard implements CanActivate {   constructor(private authService: AuthService, private router: Router) {}

      canActivate(
        next: ActivatedRouteSnapshot,
        state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean | UrlTree {
          const currentUser = this.authService.isAuthenticated();
          if (currentUser) {
          return true;
        } else {
          return this.router.parseUrl('/sign-in');
        }

      } }