所以当我开始将我的节点模块和其他东西更新到最新版本时。即使在更新之前,我的auth0登录也无法正常工作。它工作正常。但是现在我的登录工作甚至不能正常工作,我也不知道发生了什么。当我查看本地存储时,我看到尽管登录是正确的,email_verified返回false。我怀疑更新angular2-jwt是一个问题但不完全确定。 https://toddmotto.com/angular-2-authentication根据本文的登录信息,但不知道发生了哪些变化。
// services/auth.service.ts
import { Injectable, NgZone } from '@angular/core';
import {Router} from '@angular/router';
import { tokenNotExpired } from 'angular2-jwt';
// We want to avoid any 'name not found'
// warnings from TypeScript
declare var Auth0Lock: any;
@Injectable()
export class AuthenticationService {
constructor( private _router: Router, private _zone: NgZone) {}
lock = new Auth0Lock('clientid', 'domain');
login() {
this.lock.show((error: string, profile: Object, id_token: string) => {
if (error) {
console.log(error);
}
// We get a profile object for the user from Auth0
localStorage.setItem('profile', JSON.stringify(profile));
// We also get the user's JWT
localStorage.setItem('id_token', id_token);
});
}
logout() {
// To log out, we just need to remove
// the user's profile and token
localStorage.removeItem('profile');
localStorage.removeItem('id_token');
}
loggedIn() {
return tokenNotExpired();
}
}
navbar.component.html
<nav class="navbar navbar-fixed-top navbar-dark bg-primary">
<div class="container">
<button class="navbar-toggler hidden-sm-up" type="button" data-toggle="collapse" data-target="#exCollapsingNavbar">
☰
</button>
<div class="collapse navbar-toggleable-xs" id="exCollapsingNavbar">
<a class="navbar-brand display-nav" [routerLink]="['/home']"></a>
<div class="nav navbar-nav">
<a class="nav-item nav-link active" [routerLink]="['/home']">
Home <span class="sr-only">(current)</span>
</a>
<a class="nav-item nav-link" [routerLink]="['/about']">About</a>
<a class="nav-item nav-link" (click)="auth.login()" >Login</a>
<a class="nav-item nav-link" (click)="auth.logout()" >Logout</a>
</div>
<div class="nav navbar-nav float-xs-right">
<db-cart-menu ></db-cart-menu>
</div>
</div>
</div>
</nav>
navbar_component.ts
import { Component } from '@angular/core';
import { AuthenticationService } from '../services/authentication.service';
@Component({
selector: 'db-navbar',
templateUrl: 'app/navbar/navbar.component.html',
providers: [ AuthenticationService ]
})
export class NavbarComponent {
constructor(private auth: AuthenticationService) {}
login() {
this.auth.login();
}
logout() {
this.auth.logout();
}
loggedIn () {
this.auth.loggedIn();
}
}