The first step of Sina's oauth will guide the user to open a page, and then the url will redirect to the root directory of the website after the user allows it. The problem is as follows:
The url will have a code parameter, and the link will become XXX.XXX.com/?code=XXXX. When htmlMode # $locationProvider.html5Mode(false) is not used, the route will jump to XXX.XXX.com/? code=XXXX#/
Is there a good way to get the value of this code (currently using $window.location.search, because $location.search cannot get this value in this mode);
Is there a good way to remove this XXX.XXX.com/?code=XXXX#/ ?code=XXX (if not removed, every route jump link will be there)
My temporary solution to the above two problems is to replace $window.location.href, which will cause the entire page to refresh (fortunately, it is the homepage login,
It’s acceptable, but I feel like there should be more advanced and angularinic usage)
Test address http://llovebaimuda.herokuapp.com/ http://llovebaimuda.herokuapp.com/#/login#/login
Html5mode is not used because if you directly enter the url (for example: http://llovebaimuda.herokuapp.com/phone)会导致404,输入phone), it will cause 404. Enter
http://llovebaimuda.herokuapp.com/#/phone虽然浏览器显示http://llovebaimuda.herokuapp.com/phone,但是#/phone. Although the browser displays
phone, but
Is there a way for angular to use <script></script> directly in the page? For example, the Sina Weibo login component is a js, which will be loaded on the page
Then do some processing on a dom. Angular's operation on the dom is in the directive, and then compiled and linked. In short, it is Sina's login js widget
https://oauth.io/这个木有微博啊There is no relatively mature and reusable OAuth in Angular (China’s such as Sina Weibo, QQ, Douban),
oauth requires the following steps:
1. Guide users to authorize and obtain codeaccess_token
2. Actively initiate a request to the oauth website and obtain access_token
This step should be transparent to the user. Now my idea is to let the page actively jump to a page with authService. This authService actively obtains the data needed, and then saves
if 'code' of param_dict
$window.location.href = href + '#' + DEFAULT_URL
'use strict'https://oauth.io/
STATIC_URL = '/static'
DEFAULT_URL = '/phones'
phonecatApp = angular.module('phonecatApp', [
'ngRoute'
'ConfigServices'
'phonecatControllers'
'authControllers'
'phonecatServices'
'phonecatFilters'
'phonecatAnimations'
])
phonecatApp.config([
'$routeProvider'
'$locationProvider'
($routeProvider, $locationProvider, config) ->
$routeProvider
.when('/',{})
.when('/login',{
templateUrl: STATIC_URL + '/partials/auth/login.html'
controller: 'LoginCtrl'
})
.when('/auth/:code',{
})
.when('/phones', {
templateUrl: STATIC_URL + '/partials/phone/list.html'
controller: 'PhoneListCtrl'
})
.when('/phones/:phoneId', {
templateUrl: STATIC_URL + '/partials/phone/detail.html'
controller: 'PhoneDetailCtrl'
})
# Catch all
.otherwise({redirectTo: 'DEFAULT_URL'})
# Without server side support html5 must be disabled.
$locationProvider.html5Mode(false)
])
parse_query_dict = (query) ->
query = query.trim()
if query.length <= 1
return {}
query = query.substring(1)
query_dict = {}
list = query.split('&')
for q in list
k_v = q.split('=')
query_dict[k_v[0]] = k_v[1]
return query_dict
phonecatApp.run([
'$rootScope'
'$route'
'$location'
'$window'
($rootScope, $route, $location, $window) ->
$rootScope.$on('$locationChangeStart', (event, next, current) ->
if not $rootScope.user
if $location.path() == '/'
params = $window.location.search
if params
href = $window.location.href
href = href.replace(params,'')
param_dict = parse_query_dict(params)
if 'code' of param_dict
$window.location.href = href + '#' + DEFAULT_URL
# no logged user, we should be going to #login
if next.templateUrl == "partials/login.html"
# already going to #login, no redirect needed
else
# not going to #login, we should redirect now
#$location.path "/login"
)
])
Let me answer the first question:
To remove ?code=XXX, using location.href will definitely refresh the page. Use window.pushState instead.
Reference: MDN: Manipulate browser history
angularjs Sina Weibo cross-domain solution