Route:
var app = angular
.module('defexApp', [
'ngAnimate',
'ngCookies',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch',
'firebase'
])
.constant('FIREBASE_URL', 'https://defex.firebaseio.com/')
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/landing.html',
})
.when('/add', {
templateUrl: 'views/defects.html',
controller: 'DefectCtrl'
})
.when('/login', {
templateUrl: 'views/login.html',
controller: 'AuthCtrl',
resolve: {
currentUser: function(Auth) {
return Auth.signedIn();
}
}
})
.when('/register',{
templateUrl: 'views/register.html',
controller: 'AuthCtrl',
resolve: {
currentUser: function(Auth) {
return Auth.signedIn();
}
}
})
.when('/defects/:defectId', {
templateUrl: 'views/defectview.html',
controller: 'DefectViewCtrl',
resolve: {
currentUser: function(Auth) {
return Auth.resolveUser();
}
}
})
.when('/defectlist', {
templateUrl: 'views/defectList.html',
controller: 'DefectCtrl',
resolve: {
currentUser: function(Auth) {
return Auth.resolveUser();
}
}
})
.otherwise({
redirectTo: '/'
});
});
Service:
'use strict';
app.factory('Auth', function($firebase, $firebaseAuth, FIREBASE_URL) {
var ref = new Firebase(FIREBASE_URL);
var auth = $firebaseAuth(ref);
var Auth = {
register: function(user) {
return auth.$createUser({email: user.email, password: user.password});
},
login: function(user) {
return auth.$authWithPassword({email: user.email, password: user.password});
},
logout: function() {
return auth.$unauth();
},
resolveUser: function() {
return auth.$requireAuth();
},
signedIn: function() {
return auth.$waitForAuth();
},
user: {}
};
return Auth;
});
I don't know if I am using $waitForAuth or $requireAuth correctly. All I want to do is for the views /defects/:defectid and /defectlist, I want to make sure the user is authenticated before loading the view. So I used the function which uses $requireAuth. However, regardless if I am signed in or not, I am able to type in /defectlist in the address bar and have the view load which I am sure is not right. Also, how do I check if a user is signed in? I want to ng-hide or ng-show based on if a user is signed in or not and thought I could use $getAuth? I am not sure. Any help or advice would be greatly appreciated!
showing/hiding elements and re-routing users depending on login state -- use the code from the docs to populate a user object that you store in your service. You can either have a resolve method in your routing config that checks to see if user object is null, or in your controller, when it loads, check to see if user object is null -- if it is null, re-route. If you don't want to re-route but just want to show/hide elements, use ng-show="user" or ng-hide="user"
// Create a callback which logs the current auth state
function authDataCallback(authData) {
if (authData) {
//store the authData object in your service
} else {
//clear the authData object in your service
//re-route user to logged out page
console.log("User is logged out");
}
}
// Register the callback to be fired every time auth state changes
var ref = new Firebase("https://<your-firebase>.firebaseio.com");
ref.onAuth(authDataCallback);
I believe this is how they want you to check for currently signed in users now:
FirebaseUser mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
see this example
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.