棱角分明的UI替换'?“与来自Facebook的OAuth重定向'#' [英] angular-ui replace'?' with '#' on redirect from facebook oauth

查看:164
本文介绍了棱角分明的UI替换'?“与来自Facebook的OAuth重定向'#'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施的Facebook登录ouath在angularjs没有SDK。

I'm implementing facebook ouath login in angularjs without SDK.

一切正常,除了一件事。

Everything works as expected except one thing.

在上登录按钮时会被重定向至Facebook登录页面,全成登录后用户点击,Facebook的火灾REDIRECT_URI的URL,用户再次是在应用程序。

When user click on login button, which redirects to facebook login page, after successfull login, facebook fires redirect_uri URL, and user is again in the app.

问题是,UI的路由器(大概)取代?在路径'#',所以

Problem is, that ui-router (probably) replaces '?' with '#' in path, so

的http://本地主机/ fbauth的access_token = XXX和放大器; code = YYY 结果
就结果
的http://本地主机/ fbauth#的access_token = XXX和放大器; code = YYY

由于这一点,我不能使用 $ stateParams 来获取对象,查询参数。

Because of that, i cannot use $stateParams to get object with query params.

令人惊奇的是,当我在浏览器中手动输入或点击链接
的http://本地主机/ fbauth的access_token = XXX和放大器; code = YYY 结果
 一切工作正常,和UI路由器不会取代?以#。

Suprisingly, when I manually enter in browser or click link to http://localhost/fbauth?access_token=xxx&code=yyy
everything works properly, and ui-router does not replace '?' with '#'.

我想,这对redirection(重定向)方案本身有关。

I guess, that it's related to redirection scenario itself.

有人能指出我我做错了什么,或者如何改变在这种情况下UI路由器行为?

Can someone point me what I do wrong, or how to change ui-router behaviour in this case?

这是国家的,处理FB重定向:

This is the state, that handles fb redirect:

.state('fbauth', {
  url: '/fbauth?access_token&code&expires_in',
  templateUrl: 'static/public/public.html',
  controller: 'publicCtrl'
});

PS UI的路由器设置在HTML5模式下工作,并具 $ locationProvider.html5Mode(真);

推荐答案

您可以将此功能的决心添加到您的状态,这将取代使用查询字符串URL的哈希网址:

You can add this resolve function to your state, which will replace a hash url with the query string url:

resolve: {
    'urlFix': ['$location', function($location){
        $location.url($location.url().replace("#","?"));
     }]
}


  • 因此,一个的 / fbauth#的access_token = 123456&放URL; code = ABCDE&安培; expires_in = 99999999 将被自动重定向

  • 成为 / fbauth的access_token = 123456&放大器; code = ABCDE&安培; expires_in = 99999999

  • $ stateParams 将无误。

  • 的值是 {的access_token:123456,code:ABCDE,expires_in:99999999}

  • $ location.url()不如果该位置已经匹配时有更新,因此国家不会重定向没有 present。

    • So a url of /fbauth#access_token=123456&code=abcde&expires_in=99999999 would be redirected automatically
    • Becoming /fbauth?access_token=123456&code=abcde&expires_in=99999999
    • $stateParams would be populated correctly.
    • The value would be {access_token: "123456", code: "abcde", expires_in: "99999999"}
    • $location.url() doesn't update if the location matches already, so the state won't redirect when there is no # present.
    • <!DOCTYPE html>
      <html>
      <head>
          <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
          <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js"></script>
          <meta charset="utf-8">
          <title>FBAUTH</title>
      </head>
      <body ng-app="app">
          <base href="/">
          <div ui-view></div>
          <script>
              angular.module('app', ['ui.router'])
                  .controller('publicCtrl', ['$scope','$stateParams', function($scope, $stateParams) {
                      // $stateParams should be correctly set (even for hash route)
                      console.log($stateParams);
                  }])
                  .config(['$locationProvider','$stateProvider', function($locationProvider, $stateProvider){
                      $stateProvider
                          .state("fbauth",
                          {
                              url: '/fbauth?access_token&code&expires_in',
                              templateUrl: 'fbauth.html',
                              controller: 'publicCtrl',
                              resolve: {
                                  'urlFix': ['$location', function($location){
                                      $location.url($location.url().replace("#","?"));
                                  }]
                              }
                          });
      
                      $locationProvider.html5Mode(true);
                  }]);
          </script>
      </body>
      </html>
      

      这篇关于棱角分明的UI替换'?“与来自Facebook的OAuth重定向'#'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆