2017-09-02 55 views
0

有没有人将auth0-lock-passwordless整合到离子2+混合应用中?Auth0无密码锁定离子2+应用

我不知道如何处理的回调URL

我有此代码为角服务,称为auth.service.ts:

import {Storage} from '@ionic/storage'; 
import {AuthHttp, JwtHelper, tokenNotExpired} from 'angular2-jwt'; 
import {Injectable} from '@angular/core'; 
import Auth0LockPasswordless from 'auth0-lock-passwordless' 

@Injectable() 
export class AuthService { 
    private clientID = "XXXXXXXXXXX"; 
    private domain = "myapp.eu.auth0.com"; 
    jwtHelper: JwtHelper = new JwtHelper(); 
    lock: any; 
    local: Storage = new Storage() 
    user: Object; 

    constructor(private authHttp: AuthHttp) { 
    // If there is a profile saved in local storage 
    this.lock = new Auth0LockPasswordless (this.clientID, this.domain) 

    this.local.ready().then(() => { 
     this.local.get('profile').then(profile => { 
      this.user = JSON.parse(profile); 
     }) 
    }).catch(error => { 
     console.log(error); 
    }); 
    } 

    public authenticated() { 
    // Check if there's an unexpired JWT 
    return tokenNotExpired(); 
    } 

    public login() { 
    // Show the Auth0 Lock widget 
    this.lock.emailcode({ 
     responseType: 'code', 
     authParams: { 
     scope: 'openid email', 
     device: 'Mobile device' 
     } 
    }, (err, profile, token, accessToken, state, refreshToken) => { 
     // CALLBACK CODE 
     if (err) { 
     // IF THERE'S AN ERROR, THIS CODE IS EXECUTED 
     alert(JSON.stringify(err)) 
     } else { 
     // IF EVERYTHING GOES FINE, THIS CODE IS NEVER CALLED, IT IS SENT TO A CALLBACK URL 
     this.local.ready().then(() => { 
     alert("profile: " + JSON.stringify(profile)) 
     alert("id_token: " + JSON.stringify(token)) 
     alert("refresh_token: " + JSON.stringify(refreshToken)) 
     alert("state: " + JSON.stringify(state)) 
     alert("err: " + JSON.stringify(err)) 
     this.local.set('profile', JSON.stringify(profile)); 
     this.local.set('id_token', token); 
     this.local.set('refresh_token', refreshToken); 
     this.user = profile; 
     }) 
     } 
    });  
    } 

    public logout() { 
    this.local.ready().then(() => { 
     this.local.remove('profile'); 
     this.local.remove('id_token'); 
     this.local.remove('refresh_token'); 
     this.user = null; 
    }) 
    } 
} 

我试图设置Auth0应用作为本地和单个网页,没有成功。

下面是从那里我得到了我的灵感来自于一些网站:

要处理我想我可以用离子Deeplinks回调,但去之前我想确认这是正确的方式。

一个问题是:Deeplinks和Cordova自定义URL插件有什么区别?

+0

显示代码plis – Ivaro18

+0

添加代码和详细信息 –

回答

1

最好的方法是使用他们的托管auth页面,auth0-lock-passwordless被弃用,你会遇到一些问题。

电子邮件路径会返回到一个典型的Web场景中的新回调页面。因此,您需要一个活动身份验证回调页面,检查它是否与令牌一起返回,然后验证该页面。由于这是一个混合应用程序,可能有解决办法。 Auth0的支持是好的,但我认为他们没有考虑过这样的混合应用程序。

如果您打算使用多种登录方法,则可能需要设置不同的子帐户。托管页面需要将特定的js添加到他们的控制台中,我再也无法访问该代码。

简而言之,auth0-lock-passwordless路径将不起作用。