Ionic 和 Firebase 用户身份验证,页面限制
Ionic and Firebase User Authentication, page restriction
我构建了一个简单的应用程序,可让您使用 Firebase Auth 注册帐户并登录。
为此我使用了 Ionics starter "tab" 应用程序。我按照这里的教程进行操作:https://www.firebase.com/docs/web/libraries/angular/guide/user-auth.html#section-routers
用于阻止基于用户身份验证状态的路由。使用下面的代码,我已经阻止您在未登录的情况下访问仪表板选项卡,只是为了测试目的,但是您仍然可以访问它。我一步一步地跟着教程,这就是我所拥有的。
app.js
// Before anything else I define firebase, create a reference to the url,
// create a service for the url and create the Auth function.
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'firebase'])
.constant('FirebaseUrl', 'https://<firebase-name>.firebaseio.com/')
.service('rootRef', ['FirebaseUrl', Firebase])
.factory("Auth", ["$firebaseAuth", function($firebaseAuth) {
var ref = new Firebase("https://<firebase-name>.firebaseio.com/");
return $firebaseAuth(ref);
}])
...
app.js .运行()
// Then in my `.run()` function I listen for our state change that fails
// because it needs to authorized first.
.run(function($rootScope, $state, $ionicPlatform) {
// ionic init. code here
$rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error) {
// We can catch the error thrown when the $requireAuth promise is rejected
// and redirect the user back to the login page
if(error === "AUTH_REQUIRED") {
$state.go("login");
}
});
})
app.js.config()
// Finally in my `.config()` I have my $stateProvider set up and my dashboard
// tab is resolving as to have auth required to view.
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
// setup an abstract state for the tabs directive
.state('tab', {
url: '/tab',
abstract: true,
templateUrl: 'templates/tabs.html'
})
.state('login', {
url: '/login',
templateUrl: 'templates/login.html',
controller: 'LoginCtrl as ctrl'
})
.state('tab.dash', {
url: '/dash',
views: {
'tab-dash': {
templateUrl: 'templates/tab-dash.html',
controller: 'DashCtrl'
}
},
resolve : {
// controller will not be loaded until $waitForAuth resolves
// Auth refers to our $firebaseAuth wrapper in the example above
currentAuth: ["Auth", function(Auth) {
// $waitForAuth returns a promise so the resolve waits for it to complete
return Auth.$waitForAuth();
}]
}
})
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/login');
});
尽管如此,当我在登录页面(应用程序启动的地方)上单击临时 link 时,它仍然会将我带到仪表板页面,即使没有登录也是如此!
我在这里错过了什么?
在您发布的示例中它说在您发布在 firebase 网站上的示例中它说 return Auth.$requireAuth();
并且您有 return Auth.$waitForAuth();
我构建了一个简单的应用程序,可让您使用 Firebase Auth 注册帐户并登录。
为此我使用了 Ionics starter "tab" 应用程序。我按照这里的教程进行操作:https://www.firebase.com/docs/web/libraries/angular/guide/user-auth.html#section-routers
用于阻止基于用户身份验证状态的路由。使用下面的代码,我已经阻止您在未登录的情况下访问仪表板选项卡,只是为了测试目的,但是您仍然可以访问它。我一步一步地跟着教程,这就是我所拥有的。
app.js
// Before anything else I define firebase, create a reference to the url,
// create a service for the url and create the Auth function.
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'firebase'])
.constant('FirebaseUrl', 'https://<firebase-name>.firebaseio.com/')
.service('rootRef', ['FirebaseUrl', Firebase])
.factory("Auth", ["$firebaseAuth", function($firebaseAuth) {
var ref = new Firebase("https://<firebase-name>.firebaseio.com/");
return $firebaseAuth(ref);
}])
...
app.js .运行()
// Then in my `.run()` function I listen for our state change that fails
// because it needs to authorized first.
.run(function($rootScope, $state, $ionicPlatform) {
// ionic init. code here
$rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error) {
// We can catch the error thrown when the $requireAuth promise is rejected
// and redirect the user back to the login page
if(error === "AUTH_REQUIRED") {
$state.go("login");
}
});
})
app.js.config()
// Finally in my `.config()` I have my $stateProvider set up and my dashboard
// tab is resolving as to have auth required to view.
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
// setup an abstract state for the tabs directive
.state('tab', {
url: '/tab',
abstract: true,
templateUrl: 'templates/tabs.html'
})
.state('login', {
url: '/login',
templateUrl: 'templates/login.html',
controller: 'LoginCtrl as ctrl'
})
.state('tab.dash', {
url: '/dash',
views: {
'tab-dash': {
templateUrl: 'templates/tab-dash.html',
controller: 'DashCtrl'
}
},
resolve : {
// controller will not be loaded until $waitForAuth resolves
// Auth refers to our $firebaseAuth wrapper in the example above
currentAuth: ["Auth", function(Auth) {
// $waitForAuth returns a promise so the resolve waits for it to complete
return Auth.$waitForAuth();
}]
}
})
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/login');
});
尽管如此,当我在登录页面(应用程序启动的地方)上单击临时 link 时,它仍然会将我带到仪表板页面,即使没有登录也是如此!
我在这里错过了什么?
在您发布的示例中它说在您发布在 firebase 网站上的示例中它说 return Auth.$requireAuth();
并且您有 return Auth.$waitForAuth();