It is convenient to implement user registration and login functions in Rails, such as the full functionality of a gem expansion pack that can be implemented using devise. You can also use Rails to bring your own Has_secure_password from the built. The following is an attempt to use Has_secure_password to implement user registration and login capabilities.
Preparatory work
To create a project:
The encryption functionality in Has_secure_password requires Bcrypt, so the Bcrypt gem package needs to be enabled in the project. Enter the project directory and modify the Gemfile file as follows:
# Use Activemodel has_secure_password
gem ' bcrypt ', ' ~> 3.1.7 '
Exit after saving, and perform the bundle install command to install the newly enabled gem package.
Creating user Modules
Manipulating and managing user information requires creating a data table and model that saves the user:
Rails g model User name:string Password_digest:string
Password_digest This field is used to save encrypted cryptographic strings, must be provided and cannot be changed to other names, otherwise the functionality provided by Has_secure_password will not be used properly.
The Has_secure_password feature is then introduced into the user module:
# APP/MODELS/USER.RB
class User < ActiveRecord::Base
Has_secure_password
End
To create a user data table:
Implementing the Registration function
Create a applicant (requester) controller to process user registration:
Rails g Controller Applicants new Create
The applicants Controller provides two methods:
- NEW: Used to process the registration interface
- Create: To save registration information
The Controller method that is created by the command above is used by default for GET requests. The Create method that holds the registration information uses a POST request. So you need to modify the following in CONFIG/ROUTES.RB:
Post ' Applicants/create '
To complete the registration controller function:
# APP/CONTROLLERS/APPLICANTS_CONTROLLER.RB
class Applicantscontroller < Applicationcontroller
def new
@user = user.new
end
def create
@user = User.create (user_params)
if @user. Save
redirect_to: Sessions_new
Else
render "new"
end
-end private
def user_params
Params.require ( : User). Permit (: Name,:p assword,:p assword_confirmation)
End
Complete the Registration interface function:
<!--app/views/applicants/new.html.erb-->
This simply realizes the registration function.
Implement login function
Create a session controller to handle user logins and exits:
Rails g Controller Sessions New Create
Here, 2 methods are created by default on the sessions controller:
- NEW: Used to process the login interface
- Create to process the login process
As with registration, you need to modify the default route for Create to post:
# config/routes.rb
post ' sessions/create '
To complete the function of the session controller:
# APP/CONTROLLERS/SESSIONS_CONTROLLER.RB
class Sessionscontroller < Applicationcontroller
def new
End
def create
user = User.find_by (name:user_params[:name]). Try (: Authenticate, user_params[:p Assword])
if user
render plain:sprintf ("Welcome,%s!", User.Name)
else
flash.now[:login_error] = "Invalid Username or password "
render" new "End"
private
def user_params
Params.require (: session). Permit (: Name,:p assword)
End
Completing the Session Logon interface:
<!--app/views/sessions/new.html.erb-->