How to change Devise 401 redirect address? - ruby-on-rails

I have a Rails 3.2.7 app with Devise and I wanna know how do I change the url to which Devise redirects the user on 401 after he tries to access a page he has no access to. This address defaults to http://localhost:3000/users/sign_in but I wanna change it to point to another action? How do I do that? Do I have to monkey patch Devise into my app? I find this idea very awkward...
I appreciate any help,
Thanks!

You're going to have to monkey patch it, unfortunately. You can see a good tutorial on how to do it here: devise wiki

Related

Ruby on rails action filters

In my application I have at this point a authentication function through oauth, but before of that I would like to have a login page, so I want to use Devise to create that. The problem that I am facing is that bought Device and my authentication method require a before_filter, and both methods redirect to a page, and because of that I get an error saying that I have to redirects. How can I fix this problem?
Thank you
I don't know explicitly what you are doing and what are your actual requirements but as far as I know you don not need before filter. you need to provide both options for login, authentication with devise and Omniauth.
I am referring you to some links consult it.
Mandatory:
http://www.samionrails.blogspot.com/2013/08/google-omni-auth-with-devise-tutorial.html
Optional:
https://github.com/intridea/omniauth/wiki/Managing-Multiple-Providers
http://www.samionrails.blogspot.com/2013/08/authenticate-user-using-omniauth.html

Rails + Devise + API + User Registration

I am new to ruby and rails and so far I managed to setup user management using devise. Right now I am trying to integrate support for mobile Android and iOS apps. So far it is possible for them to login and logout and get an authentication token. But in addition to that I would also like them to be able to register.
Now, as I understand it I have to do a post to
http://localhost:3000/users/sign_up
How does this post look like? And how do I get a JSON response? I found this on stackoverflow.
"utf8=✓&authenticity_token=n5vXMnlzrXefnKQEV4SmVM8cFdHDCUxMYWEBMHp9fDw%3D&user[email]=asd%40fasd.org&user[password]=321&user[password_confirmation]=1233&commit=Sign+up"
Unfortunately this does not work - I am getting the message "Bad request". I also do have a couple of questions about this example. What is the authenticity_token for? How do I get one? This is not the devise token authentication I guess as the user is not even in a position to have one at this point.
Also, after a successful login I would like to bundle the "registration successful" message with a generated devise authentication token. So I guess I have to somehow extend devise`s existing registration controller.
Thank you very much in advance!
Devise already has all this setup. Based on your signup path, I infer that you mounted Devise onto http://localhost:3000/users. Devise includes all the controllers and views that are required, including the log in form, the sign up form, the email confirmation form and the password reset forms.
GET http://localhost:3000/users/sign_up is actually a form for users to signup at. The form on that page will POST to http://localhost:3000/users/, which goes to Devise's registration controller's create action.
Assuming there is no action/view already at /users/sign_up, the sign up form should be there, go check if it is there (assuming you set up devise_for correctly in your routes.rb file).

Change Omniauth authorization URL

I'm using Omniauth with Devise using the google_oauth2 strategy.
It works well, but now I'd like to change the authorize path with something of my choice.
Actually it's http://localhost:3000/users/auth/google_oauth2 while I'd like a much simpler http://localhost:3000/login since it's simple to remember.
It would not raise any error since I've disabled the Devise database authenticable (the only way to login is through a google account).
How can I do?
Thanks.
You can see how to change url prefix here How to change route of omniauth from /auth/:provider to /myapp/auth/:provider
So it is almost what you need.

Rails 3 get request referrer

I'm using Rails 3.0.9, Ruby 1.9.2, Devise 1.3.4 and has a need in Devise session new view to access the request url parameters although I see this as a general Rails request handling question. Essentially, a request is made to a Devise authenticated resource which redirects the user to the login screen. In the login view, I need access to the request url, eg, this is the request url called in the beginning
http://mysite.com/article/5?type=blah
In the redirect login page, I need access to that URL, anyone know how I can do this?
You can use request.referer or request.env['HTTP_REFERER'] in your controller to get the referer url.
I have just found out that WEBrick handles request.referrer incorrectly. But don't worry. Unicorn handles it right.
I did't test that on other servers. You should check this with yours. I don't think that you use WEBrick as a production server.

Devise access params in Devise sessions view

I'm on Rails 3.0.9, Ruby 1.9.2, Devise 1.3.4
URL to an authenticated Devise resource for my app, e.g.,
http://myblog.com/article/5?type=blah
This will cause Devise to redirect to a login page. I have a need on Devise login page, sessions#new.html.erb, to conditionally display a drop down depending on type and I have no idea how to access it from sessions#new.html.erb. I tried params and that didn't work, ideas?
I found a workaround, Devise stores that info in a session variable
session[:user_return_to]
Hope this helps someone else out of a jam if they need to do the same thing

Resources