[RUBY] Set up pre-login routing in Devise

Thing you want to do

With Devise installed Customize routing before and after login.

Premise

When Devise is installed, the root path is / users / sign_in by default. Therefore, if you try to go to the top page without logging in, devise will jump to the sign-in page without permission.

Method

If you want to move to a specific page without logging in before_action :authenticate_user! Must be written in the corresponding controller. If login authentication is required for all actions, describe it in application_controller. You can save the trouble of writing in each controller.

What is before_action: authenticate_user!

before_action: authenticate_user! is a helper method for devise. By writing this, only the authenticated user will perform each action.

If you want to perform only a specific action without logging in

For example, when you say "I want to display only the top page and about page even if I am not logged in ..." Specify in a form that excludes the corresponding action as shown below.

before_action :authenticate_user!, except: [:top, about]


By doing this, only the top action and about action can be displayed even when logged out. If you are not logged in, other actions will not be displayed even if you type the URL directly.

▼ Reference Devise authentication_user! https://skillhub.jp/courses/137/lessons/978

Also, this time I mentioned that I will write it in application_controller, If you do not want to add login authentication settings to controllers other than the user controller You can also create a hierarchy in a directory and create a controller that only applies to that directory.

Please refer to the following. ▼ Reference https://qiita.com/ryuuuuuuuuuu/items/bf7e2ea18ef29254b3dd

Recommended Posts

Set up pre-login routing in Devise
Set up a webhook in Shopify's custom app
Set CookieSpec in HttpClient 4.3
Set up Gradle multi-project in IntelliJ to build JAR file
Set up Multipart Resolver to allow file uploads in Spring
How to set up a proxy with authentication in Feign
Set context-param in Spring Boot
ActiveRecord :: NotNullViolation in Devise error
Set up GitLab with docker
Helper methods available in devise
Set up Gitolite on CentOS 7
Set up ansible-playbook on Ubuntu 20.04