作者:青峰的兔子饭团 | 来源:互联网 | 2023-05-16 08:20
IhavebuiltasimpleappthatletsyouregisteranaccountusingFirebaseAuthandlogin.我已经构建了一个
I have built a simple app that lets you register an account using Firebase Auth and login.
我已经构建了一个简单的应用程序,可让您使用Firebase Auth注册帐户并登录。
I used Ionics starter "tab" application for this. I followed the tutorial here: https://www.firebase.com/docs/web/libraries/angular/guide/user-auth.html#section-routers
我为此使用了Ionics启动器“tab”应用程序。我按照教程:https://www.firebase.com/docs/web/libraries/angular/guide/user-auth.html#section-routers
for preventing routes based on the Auth state of the user. using the code below, i've prevented you from accessing the dashboard tab without being logged in just for testing purposes however, you can still access it. I followed the tutorial step by step and this is what I have.
用于根据用户的Auth状态阻止路由。使用下面的代码,我已经阻止您访问仪表板选项卡而不是为了测试目的登录,但是,您仍然可以访问它。我一步一步地按照教程,这就是我所拥有的。
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://.firebaseio.com/')
.service('rootRef', ['FirebaseUrl', Firebase])
.factory("Auth", ["$firebaseAuth", function($firebaseAuth) {
var ref = new Firebase("https://.firebaseio.com/");
return $firebaseAuth(ref);
}])
...
app.js .run()
// 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');
});
regardless of all this, when I click a temporary link on the login page (where the app starts) it still takes me to the dashboard page, even when not logged in!
无论如何,当我点击登录页面上的临时链接(应用程序启动的地方)时,即使没有登录,它仍然会将我带到仪表板页面!
What am I missing here?
我在这里想念的是什么?
1 个解决方案