【问题标题】:Template HTML not loading after authentication Auth0身份验证 Auth0 后未加载模板 HTML
【发布时间】:2017-04-26 11:53:18
【问题描述】:

我正在关注https://github.com/auth0-samples/auth0-angularjs2-systemjs-sample/tree/master/01-Login 此处给出的 Auth0 登录示例。

这是我的身份验证服务代码。

import { Injectable } from '@angular/core';
import { tokenNotExpired } from 'angular2-jwt';
import { myConfig } from '../auth0.config';
import { Router } from '@angular/router'


// Avoid name not found warnings
declare var Auth0Lock: any;

@Injectable()
export class Auth {
  // Configure Auth0
  lock = new Auth0Lock(myConfig.ClientID,myConfig.DomainName, {
     auth: {
       redirectUrl: 'http://localhost:4200/server-auth',
       responseType: 'token',
       params: {
         state: 'mologin'
       }
     }
  });

  constructor(private router: Router) {
    // Add callback for lock `authenticated` event
    this.lock.on('authenticated', (authResult) => {
      localStorage.setItem('id_token', authResult.idToken);
      this.router.navigate(['/emails'])
    });

  }

  public login() {
    // Call the show method to display the widget.
    this.lock.show();
  };

  public authenticated() {
    // Check if there's an unexpired JWT
    // It searches for an item in localStorage with key == 'id_token'
    return tokenNotExpired();
  };

  public logout() {
    // Remove token from localStorage
    localStorage.removeItem('id_token');
  };
}

成功路由后,我将导航到我的电子邮件页面。电子邮件组件被加载。它的构造函数和 onInit 方法被成功调用。但是没有显示 html。

这是我的身份验证保护代码。用户不会被路由到登录页面,所以这很好。

import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import { Auth } from '../../services';
import { Router } from '@angular/router';
import { Injectable} from '@angular/core';

@Injectable()
export class AuthenticationGuard implements CanActivate {

    constructor(private authService: Auth, private router: Router) {
    }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        let url = route.url[0];
        if (!this.authService.authenticated()) {
            this.router.navigate(['login']);
        }
        return true;
    }
}

这是成功导航后正在加载的电子邮件组件。

@Component({
    selector: 'email-templates',
    templateUrl: './email-templates.component.html'
})
export class EmailTemplatesComponent implements OnInit {

    public isLoaded: Boolean = false;
    public barData:any;//barChart data
    private emailsArray: Array<any> = [];//table's data

    constructor(private router: Router, private toasterService: ToasterService,
        private elementRef: ElementRef, private emailService: EmailTemplateService,
        public modal: Modal, public overlay: Overlay, public vcRef: ViewContainerRef) {
         overlay.defaultViewContainer = vcRef;
    }

    ngOnInit() {
        this.getAllEmails();

    }

    getAllEmails() {
        this.emailService.getAllEmails().subscribe(res => {
            if(res.success=="true"){
            this.emailsArray = res.data;
           }
        })

    }
}

【问题讨论】:

    标签: angular jwt angular2-routing auth0


    【解决方案1】:

    根据您提供的信息,很难为您提供明确的答案,但很可能会发生以下情况:

    1. 您的服务器端需要身份验证才能提供静态文件,例如 Angular2 HTML 模板。
    2. 您导航到电子邮件页面,由于缺乏适当的身份验证,模板请求失败。

    如果是这种情况,您可以通过浏览器开发者工具查看网络跟踪来确认,您需要允许在不需要身份验证的情况下获取 HTML 模板。

    此外,使用setTimeout 将 ID 令牌保存在 Web 存储中并不是您每天都能看到的,因此您可能需要更详细地解释您的用例。

    【讨论】:

    • setTimeout 我用的只是为了让路由有一点延迟。我已经对其进行了编辑。
    • 身份验证是正确的,因为我在服务中使用 authenticated() 方法来确认每个路由上的身份验证。我不认为服务器端需要对服务文件进行身份验证,就好像在空白电子邮件页面出现并刷新浏览器时进行身份验证后,页面加载正常,所有 Api 命中都成功。
    • 我的问题和这个问题stackoverflow.com/questions/35068366/… 差不多。但它也没有得到回答
    • 认证后刷新,理论上任何需要认证的请求都可以;这就是为什么他们在身份验证之前无法工作的原因会导致我建议模板需要在服务器端进行身份验证。您是否使用浏览器工具来获取有关 HTML 模板的 HTTP 响应的更多信息?你能在问题中包含这些信息吗?
    • 我的电子邮件组件中的 http get 请求返回 success:"true" 响应。我的组件中没有其他对服务器的 http 请求。 angular 2 上的 html 在前端,它没有收到响应。我从服务器得到的唯一响应是我根据需要接收的 json 格式的数据。响应如下 { timestampt:'timestamp', success:'true', data:[returned data] }
    猜你喜欢
    • 2015-04-10
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 2016-04-22
    相关资源
    最近更新 更多