When creating an application with Rails, I think that there are many people who implement the login function using devise. Among them, many people have heard that "Let's log in easily!" To the portfolio.
However, in my experience, I had a lot of trouble implementing it''. The reason is simply
"because I had to do something I didn't know about devise" ``.
So in this article,
While organizing this area, I would like to output the "Easy Login Implementation Procedure".
In addition, this article is based on this wonderful article. There is also a detailed explanation on YouTube, so please have a look.
Qiita | How to implement easy login / guest login function (for portfolio)
Before we get into the implementation, let's first control the `` easy login flow''.
Easy login flow
1.Easy login button click
2-1.Any email address([email protected])If is not in the users table, add another column and create a user
2-2.Any email address([email protected])Get it if it is in the users table
3.Log in as the created / acquired user
4.Redirect to your favorite page(Mostly the top page)
In other words, all you need to do is the next step.
Required process
1.Set the controller path for easy login for routing
2.Create a controller for easy login
3.Place the link in the view
We will implement it according to this process.
Then, we will implement in the order of `` routing → controller → view''.
Add the following description to the routing file.
routes.rb
devise_scope :user do
post 'users/guest_sign_in', to: 'users/guest_sessions#new_guest'
end
This will generate the following routing:
Prefix | Verb | URI Pattern | Controller#Action |
---|---|---|---|
users_guest_sign_in | POST | /users/guest_sign_in | users/guest_sessions#new_guest |
Organize this content.
app / controllers / users / guest_sessions_controller.rb
new_guest action
So let's create a controller in the specified directory and implement a simple login action.
We will implement the new_guest
action in app / controllers / users / guest_sessions_controller.rb
.
[email protected]
Please note the above points.
app/controllers/users/guest_sessions_controller.rb
class Users::SessionsController < Devise::SessionsController
def new_guest
user = User.find_or_create_by!(email: "[email protected]") do |user|
#Add required columns in block(Add nickname for yourself)
user.nickname = "Test user"
user.password = SecureRandom.urlsafe_base64
# user.confirmed_at = Time.now #Required if using Confirmable
end
#Login(devise method)
sign_in user
#Redirect to top page
redirect_to root_path
end
end
find_or_create_by!
Is a method like this:
By the way, this time the purpose is to show the code to be implemented, so please see the following article to supplement the namespace and password part (Qiita is an article I wrote).
Place a link in the view where you want to implement a simple login button. For my app, I added the following code to the top page.
top page
<%= link_to 'Guest login', users_guest_sign_in_path, method: :post %>
That's it! (The GIF below has changed the details for my app)
It is the job of the model to interact with the DB to get and generate records. So I think it would be better to separate it as follows.
app/controllers/users/guest_sessions_controller.rb
class Users::SessionsController < Devise::SessionsController
def new_guest
user = User.guest
#Login(devise method)
sign_in user
#Redirect to top page
redirect_to root_path
end
end
app/models/user.rb
class User < ApplicationRecord
#(Omitted)
def self.guest
find_or_create_by!(email: "[email protected]") do |user|
user.nickname = "Test user"
user.password = SecureRandom.urlsafe_base64
end
end
end
I think there are various articles, but the basics are as shown here, so please refer to them!
Recommended Posts