Devise change URLs - ruby-on-rails

In my application the URLs for sign in, sign up and and sign out were as:
Sign in: /users/sign_in
Sign up: /users/sign_up
Sign out: /users/sign_out
I followed https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes link and changed the URLs as below which is working
Sign in URL should be changed to /login instead of /users/sign_in
Sign up URL should be changed to /register instead of /users/sign_up
Sign out URL should be changed to /logout instead of /users/sign_out
The issue that I am facing is that even if I hit the old URL like /users/sign_in I am able to access the sign in screen which should not be the case.
I had also checked for redirecting the URLs like:
get "/users/sign_in" => redirect("/login")
But it is not working.
Can someone suggest something?

Changed the position of routes and it worked for me.

Related

always redirecting to fallback location when using -> redirect_back fallback_location: root_path always

I'm using omniauth for fb & google logins. However, instead of redirect to page with the login, it re-directs to fallback page all the time.
When I check request.referrer, it is blank. request.original_url is the callback URL for the login. http://localhost:3000/auth/facebook/callback?code="
Here is the route: get "/auth/:provider/callback", to: "sessions#create".
Seems like callback is somehow deleting the request.referrer. I thought on local it might be due to http to https switch. However, it persists when I deploy to and everything is https.

When I click on login the url gets updated with parameters but session is not created?

I am using devise for authentication. Recently when I integrated bootstrap into my rails app the devise login system stopped working.
When I enter the email and password the URL gets updated with the parameters of the user but I am not getting redirected to the homepage and the session is not getting created.
As I am using the logged in condition to display the options in the navbar and the logout option is not showing so I think the session is not getting created.
Like the normal URL of the signin page is,
https://example.com/users/sign_in
after I click on the login button after entering my details, the url changes to this I stay at the login page.
https://example.com/users/sign_in?utf8=%E2%9C%93&authenticity_token=OeR8e3thZu4SnUQeNO8zYpHJ4vWMxiIaWcHkEbRDb9o%3D&user[email]=na.nishantarora%40gmail.com&user[password]=hashfloat&user[remember_me]=0&commit=Log+in
I am not able to figure out where the problem is, I have tried adding a method in my application controller for redirection but it didn't work. Here is the method I tried using,
def after_sign_in_path_for(resource)
current_user
end
Over that I also want the user to get redirected to his own profile after he logs in to the website.
and here is what my routes file looks like,
Rails.application.routes.draw do
get 'static_pages/home'
get 'static_pages/help'
devise_for :users
resources :users
root 'static_pages#home'
end

Rails return to specific location with route

I have a link on a page that is for users who are not signed in yet. I want this link to redirect to the login_url, which upon logging in will redirect the user back to that page they were on.
Something like this would be great but I don't know how to route this.
Currently, I have a redirect_back helper but that only works on specific actions where you need to be signed in. This is just a link.
The login url is /login and I would like something like this:
("/login?return_to=" + #user.username)
for the link so when you click the link (say on the user jcl), it takes you to /login?return_to=jcl. Then after signing in with a POST request, it returns you to that user's page.
Is there specific routes or helpers that can accomplish this?
you can use a before_filter to save the url in the session and before login you redirect the user to the url in the session.

Devise: Redirect to "/signup" on registration failure

I wanted to redirect to "/signup" page after a user fails the registration process. How do you do it?
Every time the registration fails, it redirects to "/users" which is not what I wanted, I want it to redirect to the registration page.
TIA
You have to write a custom method in the initializer and call it.
See this post here, hope it gets you where you need to!
Devise redirect after login fail

Rails, Devise: invalid login redirects to root page instead of displaying errors

For some reason, my app redirects failed session creation to the root page of the application, instead of displaying errors on why the login failed, such as, wrong password.
I captured the http response:
Request URL:http://localhost:3000/users/sign_in
Request Method:POST
Status Code:302 Moved Temporarily
My code performs a :
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
If I change the url to user_session_path - I get the following:
Invalid email or password.
However, it is just rendered as simple text.
Request URL:http://localhost:3000/users/sign_in.user
Request Method:POST
Status Code:401 Unauthorized
I have omniauth and cancan gems installed. Not sure where to start looking - any ideas?
(on another note, I do not see the route session in rake routes, but if I perform a correct login, it logs me in. How is this possible?)
Thank you!
I found that my app had a CustomFailure class in the lib folder. This always redirect to the root upon login. After removing this failure app, the app redirected me back to the login screen after a failed login.
According to Rails - Devise - Error messages when signing in? , the errors are not given to form fields, but to flash messages. So you need to include them to the default devise new session view.
I still did not figure out why session_path works, and why user_session_path produces the results posted in my question.

Resources