-
Notifications
You must be signed in to change notification settings - Fork 232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restricting Access to Certain Routes #85
Comments
I recommend using the $q module for this. Try this: // use make sure to inject the $q library
var isAdmin = function() {
// create promise
dfd = $q.defer();
$auth.validateUser()
.then(function(user) {
if (user.admin) {
// resolve the promise if user is admin
dfd.resolve();
} else {
// reject the promise if user is not admin
dfd.reject();
}
});
// return the $q promise
return dfd.promise;
}
if (adminRoute($location.url()) {
// if user is not admin, redirect to sign-in page
isAdmin().catch(function() {
$location.path('/sign_in');
})
} |
Thanks for the fast response. I couldn't get this to work (same problem as before). But, this does seem to work:
and
|
@jrogozen - closing this issue, please re-open if you run into any more trouble. |
nbrustein
pushed a commit
to nbrustein/ng-token-auth
that referenced
this issue
May 27, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
This is my first time implementing login/oAuth so first I'd just like to say a huge thank you for providing this and devise-token-auth. It's been frustrating, but I've learned a lot and feel like I've made some significant progress.
I'm using Rails as an api/backend with an Angular frontend. Currently, I have oAuth sign in working, but I'd like to make some routes ('/admin/*'), restricted to a single member. I don't really want to deal with having multiple user types, because at the moment, there will only be one admin. I've tried playing around in my head with a couple ways of doing this ... but I'm guessing that the easiest way to do it would be to make an 'admin' boolean in my user table.
I then want to set a $rootScope.$on('$routeChangeStart) to see if someone is trying to access '/admin/x' and if they aren't signed in + an admin, to redirect them to homepage.
Currently, this check looks something like this ...
This doesn't seem to work. I think that because $auth.validateUser(); is returning a promise, and then I'm doing the boolean check after it's resolved, I miss the time period in which I can successfully redirect using $location.path() in the $routeChangeStart function.
Is this an okay way of doing this? Are there other security concerns I should be aware of? Is there a fix to this?
Thanks so much!
The text was updated successfully, but these errors were encountered: