我的AngularJS应用程序需要访问用户的LinkedIn个人资料.为了做到这一点,我需要将用户重定向到包含回调redirect_uri参数的LinkedIn URL,该参数将告诉LinkedIn将用户重定向回我的webapp并在URL中包含"代码"查询参数.这是传统的Oauth 2.0流程.
一切都很好,除了LinkedIn将用户重定向回以下URL:
http://localhost:8080/?code=XXX&state=YYY#/users/123/providers/LinkedIn/social-sites
Run Code Online (Sandbox Code Playgroud)
我想?code=XXX&state=YYY从URL中删除,以使其干净.用户不需要查看我从LinkedIn重定向收到的查询参数.
我尝试过$location.absUrl($location.path() + $location.hash()).replace(),但它会在URL中保留查询参数.
我也无法使用提取查询参数,例如"代码" ($location.search()).code.好像有吗?在上面的URL#之前欺骗Angular.
我正在处理的应用程序包含各种状态(使用ui-router),其中某些州要求您登录,其他州是公开可用的.
我创建了一个方法来有效地检查用户是否已登录,我当前遇到的问题实际上是在必要时重定向到我们的登录页面.应该注意的是,登录页面当前未放在AngularJS应用程序中.
app.run(function ($rootScope, $location, $window) {
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
if (toState.data.loginReq && !$rootScope.me.loggedIn) {
var landingUrl = $window.location.host + "/login";
console.log(landingUrl);
$window.open(landingUrl, "_self");
}
});
});
Run Code Online (Sandbox Code Playgroud)
console.log正确显示了预期的URL.在那之后,我几乎尝试了从$ window.open到window.location.href的所有内容,无论我尝试过什么都没有重定向发生.
编辑(已解决):
发现了这个问题.
var landingUrl = $window.location.host + "/login";
$window.open(landingUrl, "_self");
Run Code Online (Sandbox Code Playgroud)
变量landingUrl设置为'domain.com/login',这不适用于$ window.location.href(这是我尝试过的事情之一).但是在将代码更改为之后
var landingUrl = "http://" + $window.location.host + "/login";
$window.location.href = landingUrl;
Run Code Online (Sandbox Code Playgroud)
它现在有效.