Skip to content
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

Authenticated Route Constraints #1147

Merged
merged 4 commits into from
Jun 23, 2011
Merged

Authenticated Route Constraints #1147

merged 4 commits into from
Jun 23, 2011

Conversation

sj26
Copy link
Contributor

@sj26 sj26 commented Jun 18, 2011

Allows routing based on authentication state, optionally by scope.

The example included in the comments for #authenticated:

authenticated :admin do
  root :to => 'admin/dashboard#show'
end

authenticated do
  root :to => 'dashboard#show'
end

root :to => 'landing#show'

@josevalim
Copy link
Contributor

Thanks for the pull request, but why would I use authenticated instead of authenticated?

@vijaydev
Copy link

@josevalim what's the question again? :)

@josevalim
Copy link
Contributor

Hahaha, LOL, sorry.

The question is: why would I use authenticated instead of the existing authenticate?

@sj26
Copy link
Contributor Author

sj26 commented Jun 18, 2011

@josevalim: because authenticate forces authentication, authenticated only checks for it.

With authenticated I can provide the same path twice but route differently based on authentication state. A classic example is github itself. Unauthenticated users see a landing page extorting github's virtue, authenticated users see a dashboard of recent activity and repositories, both at the root URL.

@josevalim
Copy link
Contributor

Oh, that's great. I like it. Could you please provide tests then?

@sj26
Copy link
Contributor Author

sj26 commented Jun 19, 2011

Awesome, yeah, I wanted to get feedback before doing so. I'll chuck some together now, cheers!

@josevalim
Copy link
Contributor

Hey mate, any news? I am planning to release Devise 1.4 in the next 24 hours. So if you can add tests, we can get it in!

@sj26
Copy link
Contributor Author

sj26 commented Jun 23, 2011

Oh man, okay, I'll get cracking.

(Opposite timezone fail.)

sj26 added 2 commits June 23, 2011 10:44
Warden::Proxy#authenticated? and Warden::Proxy#unauthenticated? don't try strategies first.
@sj26
Copy link
Contributor Author

sj26 commented Jun 23, 2011

Fully tested. Caught a problem the last commit, too. -.-

josevalim added a commit that referenced this pull request Jun 23, 2011
Authenticated Route Constraints
@josevalim josevalim merged commit f43a7c4 into heartcombo:master Jun 23, 2011
@deepakinseattle
Copy link

This is a great addition to devise. Thanks sj26 for contributing and josevalim for merging!

@volkanunsal
Copy link

This feature would be even better if we could specify some pages that be shown only to unauthenticated users. Like the registration and login pages, for instance. Then anyone requesting those pages can be redirected to the landing page for that model.

@josevalim
Copy link
Contributor

unauthenticated was added to Devise later with exactly this behavior.

@sj26
Copy link
Contributor Author

sj26 commented Aug 24, 2011

... or you could just have later routes which, implicitly, are unauthenticated:

authenticated do
  root :to => :dashboard
end

# unauthenticated:
root :to => :home

@sj26
Copy link
Contributor Author

sj26 commented Aug 24, 2011

Oh, nevermind, I get you might want to have routes only accessible to unauthenticated users unmasked by authenticated routes.

Also, useful for skipping a whole section of unauthenticated routes as an efficiency gain.

@volkanunsal
Copy link

Isn't unauthenticated the same as the default root path? What I had in mind was more like a way of making sure authenticated users never get to see "new registration" and "new session" pages. It would be a way of doing the same thing as what this line from my registrations_controller.rb is doing right now:

redirect_to stored_location_for(current_user) if signed_in?

My proposed syntax for it would be:

authenticated, :force =>[:registrations,:sessions] do 
    as :user do
      root :to      => "pages#index"
    end
end

(Unless there is already a way of doing the same from the controller that I am not aware of.)

@codyolsen
Copy link

@sj26, @josevalim: Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

6 participants