Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angular – 使用Auth0 spa快速入门时出错大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在开发一个Angular 2 Web应用程序,它将使用Auth0进行用户身份验证.

@H_404_8@

我在Auth0网站上关注了quickstart,但是我收到错误无法在新的Authservice(auth.service.ts:9)中读取未定义的属性’WebAuth’,即使声明了Authservice.@H_404_8@

我错过了什么吗?很感谢任何形式的帮助.@H_404_8@

这是我的代码@H_404_8@

//app.component.ts@H_404_8@

@H_404_8@

import { Component } from '@angular/core';
import { Authservice } from '../../auth/auth.service';

@Component({
    SELEctor: 'app',templateUrl: './app.component.html',styleUrls: ['./app.component.css']
})
export class AppComponent {

    constructor(public auth: AuthservicE) {
        auth.handleAuthentication();
    }
}

//auth.service.ts@H_404_8@

@H_404_8@

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import 'rxjs/add/operator/filter';
import auth0 from 'auth0-js';

@Injectable()
export class Authservice {

    auth0 = new auth0.WebAuth({
        clientID: '*****',domain: '*****',responseType: 'token id_token',audience: '******',redirectUri: 'http://localhost:4200/callBACk',scope: 'openid'
    });

    constructor(public router: Router) { }

    public login(): void {
        this.auth0.authorize();
    }

    public handleAuthentication(): void {
        this.auth0.parseHash((err,authResult) => {
            if (authResult && authResult.accessToken && authResult.idToken) {
                window.LOCATIOn.hash = '';
                this.setSession(authResult);
                this.router.navigate(['/home']);
            } else if (err) {
                this.router.navigate(['/home']);
                console.log(err);
            }
        });
    }

    private setSession(authResult): void {
        // Set the time that the access token will expire at
        const expiresAt = JSON.Stringify((authResult.expiresIn * 1000) + new Date().getTime());
        localStorage.setItem('access_token',authResult.accessToken);
        localStorage.setItem('id_token',authResult.idToken);
        localStorage.setItem('expires_at',expiresAt);
    }

    public logout(): void {
        // Remove tokens and expiry time from localStorage
        localStorage.removeItem('access_token');
        localStorage.removeItem('id_token');
        localStorage.removeItem('expires_at');
        // Go BACk to the home route
        this.router.navigate(['/']);
    }

    public isAuthenticated(): Boolean {
        // check whether the current time is past the
        // access token's expiry time
        const expiresAt = JSON.parse(localStorage.getItem('expires_at'));
        if (new Date().getTime() < expiresAt) {
            return true;
        } else {
            return false;
        }
    }

}

//app.html@H_404_8@

@H_404_8@

<nav class="navbar navbar-default">
    <div class="container-fluid">
        <div class="navbar-header" *ngIf="auth" >
            <a class="navbar-brand" href="#">Auth0 - Angular</a>

            <button class="btn btn-priMary btn-margin"
                    routerLink="/">
                Home
            </button>

            <button class="btn btn-priMary btn-margin"
                    *ngIf="!auth.isAuthenticated()"
                    (click)="auth.login()">
                Log In
            </button>

            <button class="btn btn-priMary btn-margin"
                    *ngIf="auth.isAuthenticated()"
                    (click)="auth.logout()">
                Log Out
            </button>

        </div>
    </div>
</nav>

<main class="container">
    <router-outlet></router-outlet>
</main>

谢谢@H_404_8@

解决方法

你试过这样导入它:

@H_404_8@

从’auth0-js’导入*为auth0;@H_404_8@

大佬总结

以上是大佬教程为你收集整理的angular – 使用Auth0 spa快速入门时出错全部内容,希望文章能够帮你解决angular – 使用Auth0 spa快速入门时出错所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。