2016-11-12 81 views
0

我正在考慮使用Auth0在我的nodejs API上籤署我的用戶。我該如何使用Auth0與RESTful API?

我正在使用MySQL數據庫簽署他們,我也想使用Facebook,以便他們可以註冊和登錄。

我遇到了回調的問題,因爲我的API不應該通過瀏覽器訪問。只有一個web應用程序或移動應用程序應該訪問它。我如何在我的移動應用上實現對我的登錄/登錄表單輸入的處理,以使用我應用Auth0的API?

謝謝你的回答。

回答

0

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 protected]", 
    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'); 
    }; 
}