首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法鉴定角5

无法鉴定角5
EN

Stack Overflow用户
提问于 2018-04-12 04:29:31
回答 2查看 81关注 0票数 2

验证后,应用程序应转移到另一个页面。但是,由于某些原因,日志中写到,所有Ok身份验证passed.But都不会发生到另一个页面的转换。有什么问题吗?

auth.guard.ts:

代码语言:javascript
复制
import { Injectable }     from '@angular/core';
import {CanActivate, Router} from "@angular/router";
import {Angular2TokenService} from "angular2-token";

@Injectable()
export class AuthGuard implements CanActivate {

  constructor(private authTokenService:Angular2TokenService,
              private router:Router){}

  canActivate() {
    if (localStorage.getItem('currentUser')) {
        // logged in so return true
        return true;
    }

    // not logged in so redirect to login page
    this.router.navigate(['/sign_in']);
    return false;
}

}

auth.service.ts:

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import {Angular2TokenService} from "angular2-token";
import {Subject} from 'rxjs/Subject';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import {Response} from "@angular/http";

@Injectable()
export class AuthService {

  userSignedIn$:Subject<boolean> = new Subject();

  constructor(private authService:Angular2TokenService) {
    this.userSignedIn$.next(this.authService.userSignedIn());
  }

  logOutUser():Observable<Response>{

    return this.authService.signOut().map(
        res => {
          this.userSignedIn$.next(false);
          return res;
        }
    );
  }

  logInUser(signInData: {email:string, password:string}):Observable<Response>{

    return this.authService.signIn(signInData).map(
        res => {
          this.userSignedIn$.next(true);
          return res
        }
    );

  }

   registerUser(signUpData:  {email:string, password:string, passwordConfirmation:string}):Observable<Response>{
    return this.authService.registerAccount(signUpData).map(
        res => {
          this.userSignedIn$.next(true);
          return res
        }
    );
  }

}

sign-in.component.ts:

代码语言:javascript
复制
import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { AuthService } from "../../services/auth.service";
import {Router} from "@angular/router";

@Component({
  selector: 'app-sign-in',
  templateUrl: './sign-in.component.html',
  styleUrls: ['./sign-in.component.css']
})
export class SignInComponent implements OnInit {

  signInUser = {
    email: '',
    password: ''
  };

  @Output() onFormResult = new EventEmitter<any>();

  constructor(private _router: Router, private authService:AuthService) { }

  ngOnInit() {}

  onSignInSubmit(){

    this.authService.logInUser(this.signInUser).subscribe(

        res => {
          if(res.status == 200){
            // loginForm.resetForm();
            this.onFormResult.emit({signedIn: true, res});
            this._router.navigate(['user_profile']);
          }
        },

        err => {
          console.log('err:', err);
          // loginForm.resetForm();
          this.onFormResult.emit({signedIn: false, err});
        }
    )

  }




}

sign-in.component.html:

代码语言:javascript
复制
<div class="main-content">
        <form (ngSubmit)="onSignInSubmit()" #f="ngForm" >


                <div class="row">

                  <div >
                    <input id="email"
                           type="email"
                           required
                           name='email' 
                           [(ngModel)]="signInUser.email" 
                           class="validate">

                    <label for="email">Email</label>
                  </div>


                  <div >
                    <input id="password" 
                           type="password" 
                           required  
                           name='password' 
                           [(ngModel)]="signInUser.password" 
                           class="validate">

                    <label for="password">Password</label>
                  </div>

                  <div >
                    <button type="submit" 
                    > 
                      Login </button>
                  </div>


                </div>

              </form>

</div>
EN

回答 2

Stack Overflow用户

发布于 2018-04-12 04:37:17

您在user_profile中有错误的路径,请尝试以下操作

代码语言:javascript
复制
 this._router.navigate(['/user_profile']);
票数 0
EN

Stack Overflow用户

发布于 2018-04-12 06:44:35

如果你的守卫在你的'user_profile‘路径上,那么你需要在你的localStorage中设置'currentUser’。否则你的守卫就会重定向回登录页面。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49788015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档