简体   繁体   English

如果未使用AngularFire 2登录,则重定向到登录页面

[英]Redirect to login page, if not logged in with AngularFire 2

Whats the best way to redirect the user to the login page, if he's not authenticated with Angular2 / AngularFire2? 如果他没有使用Angular2 / AngularFire2进行身份验证,那么将用户重定向到登录页面的最佳方法是什么?

For example; 例如; I want to protect the /dashboard page from not authenticated users. 我想保护/仪表板页面免受未经过身份验证的用户的影响。 The user should instantly get redirected to the /login page, 用户应立即重定向到/ login页面,

I'm using 我正在使用

  • angular2 version 2.0.0-rc.1 angular2版本2.0.0-rc.1
  • angular2/router version 2.0.0-rc.1 angular2 /路由器版本2.0.0-rc.1
  • firebase version 3.0.5 firebase版本3.0.5
  • angularfire2 version 2.0.0-beta.1 angularfire2版本2.0.0-beta.1

You can protect urls using Angular 2's UI router guard conditions. 您可以使用Angular 2的UI路由器防护条件保护URL。 https://angular.io/docs/ts/latest/guide/router.html#!#can-activate-guard https://angular.io/docs/ts/latest/guide/router.html#!#can-activate-guard

An example using firebase 使用firebase的一个例子

Guard Component 警卫组件

Note it would probably be better to replace referencing AF here, with an authentication service. 请注意,在此处使用身份验证服务替换引用AF可能会更好。

import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { AngularFire } from 'angularfire2';

@Injectable()
export class CanActivateViaAuthGuard implements CanActivate {
    user;
    constructor(private af: AngularFire, private router: Router) {
        this.af.auth.subscribe(user => {
            if (user) {
                this.user = user;
            } else {
                this.user = undefined;
            }
        });
    }

    canActivate() {
        if (this.user) {
            return true;
        }
        this.router.navigate(['/login']);
        return false;
   }
}

Application Routes 申请路线

import { CanActivateViaAuthGuard} from './CanActivateViaAuthGuard';

const routes: Routes = [
    {path: '/Login', component: LoginComponent},
    {path: '/dashboard', component: DashboardComponent, canActivate: [CanActivateViaAuthGuard]}
];

export const routing = RouterModule.forRoot(routes);

Finally the login code 最后登录代码

onSubmit() {
    this.af.auth.login({
      email: this.email,
      password: this.password,
    }).then(() => {
      this.submitted = true;
      this.router.navigate(['/dashboard', this.dashboard]);
    });
  }

You can use the Auth method here. 您可以在此处使用Auth方法。

if ($scope.auth === null) {
  $state.go('login');
}

Inject your $firebaseAuth and assign it to the $scope.auth then let the if check of its true or false 注入$ firebaseAuth并将其分配给$ scope.auth然后让if检查其true或false

Found the solution. 找到了解决方案。

Thanks to todo-angular-2 from r-park 感谢来自r-park的todo-angular-2

import { ReflectiveInjector } from '@angular/core';
import { Router } from '@angular/router-deprecated';
import { AuthService } from './auth-service';


let appInjector: ReflectiveInjector;

/**
 * This is a workaround until `CanActivate` supports DI
 * @see https://github.com/angular/angular/issues/4112
 */

export class AuthRouteHelper {
  static dependencies(): {auth: AuthService, router: Router} {
    const injector: ReflectiveInjector = AuthRouteHelper.injector();
    const auth: AuthService = injector.get(AuthService);
    const router: Router = injector.get(Router);
    return {auth, router};
  }

  static injector(injector?: ReflectiveInjector): ReflectiveInjector {
    if (injector) appInjector = injector;
    return appInjector;
  }

  static requireAuth(): boolean {
    const { auth, router } = AuthRouteHelper.dependencies();
    if (!auth.authenticated) router.navigate(['/SignIn']);
    return auth.authenticated;
  }

  static requireUnauth(): boolean {
    const { auth, router } = AuthRouteHelper.dependencies();
    if (auth.authenticated) router.navigate(['/Tasks']);
    return !auth.authenticated;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 登录页面重定向 - 如果未登录,则使 HTML 无法访问 - Login Page Redirect - Make HTML inaccessible if not logged in 登录页面不会重定向到路由到登录页面 - Login page won't redirect to route to logged-in page 如果未处于登录状态并且登录后重定向到原始页面,如何重定向到登录页面? - How do I redirect to login page if not in logged in state and after login redirect to the original page? 使用php将登录用户从登录页面重定向到用户仪表板 - Redirect a logged in user from login page to user dashboard with php 如果未登录,则将用户从 JQuery 自动完成重定向到登录页面 - Redirect user to login page from JQuery autocomplete if not logged in Bigcommerce-如果未登录,如何将用户从首页重定向到登录页面 - Bigcommerce - How to redirect users to login page from homepage if not logged in 如果用户尚未登录Angular JS,则重定向到登录页面 - Redirect to a login page if the user is not yet logged in Angular JS 如果注销,Bigcommerce将重定向到登录名 - Bigcommerce redirect to login if logged out AngularFire2 - 如何在页面刷新后保持登录状态 - AngularFire2 - How to maintain logged in state after page refresh 登录后如何重定向到我想要的页面,登录成功后如何显示用户名 - How to redirect to the page that I want after logged in and display username after successful login
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM