authentication - Auth0 和 Angular2 : how to set callbackURL and catch the token?

标签 authentication angular angular2-routing auth0

我正在尝试使用 Auth0 实现无密码身份验证和 angular2 (2.0.0-rc.6),使用angular2-webpack-starterauth0-lock-passwordless .

表单正常显示,并且使用以下代码发送验证电子邮件:

this.lock.magiclink({
    callbackURL:"http://localhost:3000/#/sandbox"
    });

问题发生在我点击电子邮件中的魔术链接之后:


My questions are:

  1. How to have Auth0 actually redirect to callbackURL?
  2. How can I catch the token with the new angular2 router, even though the uri is misformed? (question mark missing before the query)

最佳答案

经过一番努力,我找到了解决方法。

TL;DR;使用 PathLocationStrategy (HTML 5 PushState),而不是“哈希 URL”样式。


在 Auth0 控制台(客户端设置)中的允许的注销 URL允许的来源下面,指定了:

Notice that querystrings and hash information are not taken into account when validating these URLs.

所以我认为它也可能适用于允许的回调 URL,即使没有指定。

这可以解释为什么 callbackURL 被忽略。


诀窍是去掉 URL 中的哈希 (#);哈希值是 Angular2 Webpack Starter 中的默认 LocationStrategy .

为此,请在 app.modules.ts 中,将 RouterModule.forRoot(ROUTES, { useHash: true }) 更改为 RouterModule.forRoot(ROUTES) , { useHash: false })


虽然它应该有效,但我遇到了另一个问题:当您重新加载页面时,它会显示一个空白页面,并显示以下消息:

<% if (webpackConfig.htmlElements.headTags) { %>

经过一番谷歌搜索后,我发现了 fix in this page .

修复方法是删除“webpack-dev-server”中的尖号 (^):“^2.1.0-beta.2”(devDependencies、package.json),然后重新安装包:

  • 将“^2.1.0-beta.2”替换为“2.1.0-beta.2”

然后,在控制台/终端中输入:npm install webpack-dev-server


现在我所要做的就是更新回调URL,如下所示:

this.lock.magiclink({
  callbackURL:"http://localhost:3000/sandbox"
});

并在 Auth0 客户端设置的允许回调 URL 中插入:

http://localhost:3000/sandbox

然后保存。

现在,成功登录后(当我单击电子邮件中的魔术链接时),它会打开一个带有以下 URL 的浏览器窗口:

http://localhost:3000/sandbox#access_token=xxxxxxxxxxx&id_token=yyyyyyyyyyy&token_type=Bearer

它应该留在那里。捕获并保存 token 现在应该是微不足道的......

关于authentication - Auth0 和 Angular2 : how to set callbackURL and catch the token?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40471360/

相关文章:

javascript - Angular 2 子路由

git - 如何通过ssh git clone而无需输入密码?

authentication - OpenID 提供程序可以使用 Kerberos 或其他 "alternate"身份验证机制吗?

php - CakePHP 3 - 多字段身份验证登录

javascript - 应用程序范围内可访问和可变的变量

Angular2根据全局设置为路由加载不同的组件

regex - Perl 正则表达式 |如何从文件中排除单词

angular - 有没有办法在 ngFor 中声明特定类型

angular - 将图像 src 的授权 header 传递到 Ionic 页面中的远程服务器

Angular 2 final - 以编程方式更改 URL 上的路由参数