最新のAngular + Firebaseを使用してログイン認証システムを設定しようとしています。 home.htmlにはlogin + signupリンクが含まれており、login.htmlに行き、資格情報を追加するだけで正常に動作します(submittet時に正しいUIDを記録します)。しかし、dash.htmlにルーティングすることになっていますが、home.htmlに戻ります。Angularfire ngRoute /問題を解決し、そうでない場合に移動します
私はそれを削除すると問題が消えるため、私の解決機能に問題があるように見えました。しかし、私はまだそれが必要だと思っています(必要?)。
私はログインしていますが(home.htmlにリダイレクトされています)、URLからdash.htmlにアクセスすることはできますが、dash.htmlでログアウト機能を使用すると再びアクセスできません。 しかし私は最初にhome.htmlにリダイレクトされている理由を理解できません。
は、ここでは、コードの一部、感謝任意のヘルプです:
マイ.RUN、.configをとルート。
app.run(['$rootScope', '$location',
function($rootScope, $location){
$rootScope.$on('$routeChangeError',
function(event, next, previous, error){
if(error === 'AUTH_REQUIRED'){
$location.path('/home');
}
});
}]);
app.config(['$routeProvider', '$locationProvider', function($routeProvider,
$locationProvider){
$routeProvider
.when('/home', {
templateUrl: '/home.html',
controller: 'homeController'
})
.when('/login', {
templateUrl: '/login.html',
controller: 'loginController',
resolve: {
'currentAuth': ['Auth', function(Auth){
return Auth.$waitForAuth();
}]
}
})
.when('/dash', {
templateUrl: '/dash.html',
controller: 'dashController',
resolve: {
'currentAuth': ['Auth', function(Auth){
return Auth.$requireAuth();
}]
}
})
.otherwise({ redirectTo: '/home' });
}]);
マイログインコントローラ:
app.controller('loginController', ['currentAuth', '$scope', '$firebaseAuth',
'Auth', '$location', '$rootScope',
function(currentAuth, $scope, $firebaseAuth, Auth, $location, $rootScope){
var ref = new Firebase('https://url.firebaseio.com');
$scope.auth = $firebaseAuth(ref);
$scope.loginUser = function(){
$scope.auth = Auth;
$scope.auth.$authWithPassword({
email:$scope.email,
password:$scope.password
}, {
remember: 'sessionOnly'
}).then(function(authData) {
console.log('Logged in as: ', authData.uid);
$scope.auth.$onAuth(function(authData) {
$rootScope.auth = true;
$scope.auth = Auth;
$location.path('/dash.html');
})
}).catch(function(error) {
console.log('There was an error: ', error);
});
};
}]);
そして、私の工場とモジュール: VARアプリ= angular.module( 'アプリ'、[ 'firebase'、 'ngRoute']);
app.factory('Auth', ['$firebaseAuth',
function($firebaseAuth){
var ref = new Firebase('https://url.firebaseio.com');
return $firebaseAuth(ref);
}]);
Firebaseについてはよく知らないけど、サービスとコントローラの両方が同じURLを使ってFirebaseの新しいインスタンスを作成するのは気にかかります。サービス内のインスタンスがそうではないにもかかわらず、コントローラ内のインスタンスが新しい状態を持つと考えても、それはルーティングで使用されているインスタンスであると考えてください。 – GillesC
@GillesC:おそらく、私はこれには興味がないと思っていますが、認可されていればダッシュにアクセスできることは明らかです。ログインすると、/ homeにリダイレクトする前に、すぐにURLのフラッシュを見る/ダッシュすることができます。 – mtorn
「AUTH_REQUIRED」が動作しない可能性がありますか?私はcurrentAuthの解決策に何かがあるように感じています。 – mtorn