【问题标题】:How am i supposed to use a Auth0 with a RESTful api?我应该如何使用带有 RESTful api 的 Auth0?
【发布时间】:2017-03-26 06:46:09
【问题描述】:

我正在考虑使用 Auth0 在我的 nodejs API 上登录我的用户。

我正在使用 MySQL 数据库为他们登录,我还想使用 Facebook,以便他们可以注册和登录。

我对回调的概念有疑问,因为我的 API 不应该通过浏览器访问。只有 webapp 或移动应用程序才能访问它。我如何必须在我的移动应用程序上实现对我的登录/登录表单输入的处理才能使用我应该使用 Auth0 的 API?

感谢您的回答。

【问题讨论】:

    标签: mysql node.js rest express auth0


    【解决方案1】:

    Auth0 带有一个免费帐户的数据库。当您将登录注册小部件添加到您的应用程序并且用户注册时,它会将它们添加到您的 auth0 帐户中的数据库中。

    可以看到进程信息here

    我所做的是使用 auth0 小部件对用户进行身份验证。这允许 auth0 处理加密和安全性。然后,当用户登录时,我在响应中请求配置文件。通常,这至少可以为我提供基本信息,例如电子邮件地址。我使用电子邮件地址作为唯一键创建自己的数据库,这允许我在用户登录时向用户提供正确的数据。

    这是我的 auth0 服务示例,它使用小部件并在响应中请求用户的个人资料,然后将其存储到本地存储中。

    import { Injectable }                      from '@angular/core';
    import { tokenNotExpired, JwtHelper }      from 'angular2-jwt';
    import { Router }                          from '@angular/router';
    import { myConfig }                        from './auth.config';
    
    declare var Auth0Lock: any;
    
    var options = {
        theme: {
        logo: '/img/logo.png',
        primaryColor: '#779476'
        },
        languageDictionary: {
        emailInputPlaceholder: "email@example.com",
        title: "Login or SignUp"
      }, 
     };
    
    @Injectable()
    export class Auth {
      lock = new Auth0Lock(myConfig.clientID, myConfig.domain, options, {});
      userProfile: Object;
      constructor(private router: Router) {
        this.userProfile = JSON.parse(localStorage.getItem('profile'));
        this.lock.on('authenticated', (authResult: any) => {
          localStorage.setItem('access_token', authResult.idToken);
          this.lock.getProfile(authResult.idToken, (error: any, profile: any) => {
            if (error) {
              console.log(error);
              return;
            }
            localStorage.setItem('profile', JSON.stringify(profile));
            this.userProfile = profile;
            this.router.navigateByUrl('/overview');
          });
          this.lock.hide();
        });
      }
    
      public login() {
        this.lock.show();
      }
    
      private get accessToken(): string {
            return localStorage.getItem('access_token');
        }
    
      public authenticated(): boolean {
        try {
            var jwtHelper: JwtHelper = new JwtHelper();
            var token = this.accessToken;
            if (jwtHelper.isTokenExpired(token))
                return false;
            return true;
        }
        catch (err) {
            return false;
        }
      }
    
      public logout() {
        localStorage.removeItem('profile');
        localStorage.removeItem('access_token');
        this.userProfile = undefined;
        this.router.navigateByUrl('/home');
      };
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-15
      • 2016-04-16
      • 2015-04-24
      • 1970-01-01
      • 2018-12-19
      • 2013-10-02
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      相关资源
      最近更新 更多