7

我正在尝试使用 Auth0 进行社交登录,但我不断收到未定义引用的异常。

这是身份验证服务

import { Injectable }      from '@angular/core';
import { tokenNotExpired } from 'angular2-jwt';

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

@Injectable()
export class AuthService {
// Configure Auth0

lock = new Auth0Lock('I have set the ID correctly here', 'and the domain as well', {});

constructor() {
// Add callback for lock `authenticated` event
  this.lock.on("authenticated", (authResult) => {
  localStorage.setItem('id_token', authResult.idToken);
  });
}

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

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

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

我注入了服务并配置了提供程序。Auth0Lock一切都正确连接,但即使定义它也找不到。每次它到达lock = new Auth0Lock('ID', 'DOMAIN', {});它的炸弹。

4

3 回答 3

6

我替换declare var Auth0Lock: any;const Auth0Lock = require('auth0-lock').default;并解决了问题。

于 2016-12-06T10:27:50.233 回答
3

接受的答案很好。我确实收到了一个找不到名称“需要”的错误。我没有使用'declare const require',而是像这样导入:

import Auth0Lock from 'auth0-lock';

于 2017-05-05T11:28:51.310 回答
-2

我需要添加到index.html

<script src="https://cdn.auth0.com/js/lock/10.8/lock.min.js"></script>

通过https://github.com/auth0/lock/issues/588

于 2017-05-01T03:23:31.803 回答