With Devise installed Customize routing before and after login.
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.
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.
before_action: authenticate_user!
is a helper method for devise.
By writing this, only the authenticated user will perform each action.
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