我的按钮触发TwitterAuth功能,
<div ng-controller="HomePage">
<h1>New Home Page</h1>
<button ng-click="TwitterAuth()">Log In</button>
<button ng-click="logOut()">Log Out</button>
{{authData}}
</div>
我被重定向到twitter,我登录得很好,然后被重定向回同一页面,但现在的URL是
http://www.mypage.com/?__firebase_request_key="xJhS4ObQtpzUdbJdIESkQKZJqZ3C51t1"
并且不会进一步刷新或更改网址。我觉得这可能是问题,也许?
我正在使用Node Express创建SPA。
var express = require('express');
var app = express();
var cacheTime = 86400000*365; // one year
app.use('/img/icons', express.static(__dirname + '/public/img/icons',{ maxAge: cacheTime }));
app.use('/img', express.static(__dirname + '/public/img',{ maxAge: cacheTime }));
app.use('/templates', express.static(__dirname + '/public/templates',{ maxAge: cacheTime }));
app.use('/js', express.static(__dirname + '/public/js',{ maxAge: cacheTime }));
app.use('/css', express.static(__dirname + '/public/css',{ maxAge: cacheTime }));
app.use('/partials', express.static(__dirname + '/public/partials',{ maxAge: cacheTime }));
app.all('/*', function(req, res, next) {
res.sendFile('/public/index.html', { root: __dirname });
});
app.listen(8080);
然后我使用带有html5Mode的Angular的路由,我已经尝试禁用html5Mode并且没有任何区别。
var app = angular.module('app', ['ngRoute', "firebase"])
app.config(function ($routeProvider, $locationProvider) {
$routeProvider.
when('/', {
templateUrl: 'templates/home.html',
controller: 'HomePage'
}).
when('/test/:countryName', {
template: '<h1>TODO create country detail view</h1>',
controller: 'HomePage2'
}).
otherwise({
redirectTo: '/'
});
$locationProvider.html5Mode(true);
});
页面不会再次刷新,页面也不会在页面或控制台中显示$ scope.authData。
app.factory("Auth", ["$firebaseAuth",
function($firebaseAuth) {
var ref = new Firebase("https://1212.firebaseio.com");
return $firebaseAuth(ref);
}
]);
app.controller("HomePage", ["$scope", "Auth", function ($scope, Auth) {
$scope.auth = Auth;
// any time auth status updates, add the user data to scope
$scope.auth.$onAuth(function (authData) {
$scope.authData = authData;
console.log($scope.authData);
});
如果我使用authWithOAuthPopUp然后它工作正常,但我不喜欢弹出窗口,并且更喜欢整页重定向。
$scope.TwitterAuth = function () {
var ref = new Firebase("https://1212.firebaseio.com");
ref.authWithOAuthRedirect("twitter", function (error) {
if (error) {
console.log("Authentication Failed!", error);
} else {
// We'll never get here, as the page will redirect on success.
}
});
}
$scope.logOut = function (user) {
var ref = new Firebase("https://1212.firebaseio.com");
ref.unauth();
};
}
]);
但是,如果我再次访问Twitter,在同一个会话中登录然后再次返回,则auth数据会在页面和控制台中显示3次。
我无法要求用户两次登录,这里发生了什么?