在 Rails 實(shí)現(xiàn)用戶注冊和登錄功能是非常方便的,比如可以使用 Devise 這類實(shí)現(xiàn)了完整功能的 gem 擴(kuò)展包。也可以使用 Rails 自帶的 has_secure_password 來自已打造。下面就是嘗試使用 has_secure_password 來實(shí)現(xiàn)用戶注冊和登錄功能。
準(zhǔn)備工作
創(chuàng)建項(xiàng)目:
1
|
rails new user_login |
has_secure_password 中加密的功能需要用到 bcrypt ,所以需要在項(xiàng)目中啟用 bcrypt gem 包。進(jìn)入項(xiàng)目目錄,修改 Gemfile 文件如下內(nèi)容:
1
2
|
# Use ActiveModel has_secure_password gem 'bcrypt' , '~> 3.1.7' |
保存后退出,執(zhí)行 bundle install 命令安裝新啟用的 gem 包。
創(chuàng)建用戶模塊
操作和管理用戶信息需要先創(chuàng)建保存用戶的數(shù)據(jù)表和模型:
1
|
rails g model user name:string password_digest:string |
password_digest 這個(gè)字段是用來保存加密混淆后的密碼串的,必須提供并且不能更改成其它名稱,否則無法正常使用 has_secure_password 提供的功能。
然后在用戶模塊中引入 has_secure_password 功能:
1
2
3
4
|
# app/models/user.rb class User < ActiveRecord::Base has_secure_password end |
創(chuàng)建用戶數(shù)據(jù)表:
1
|
rake db:migrate |
實(shí)現(xiàn)注冊功能
創(chuàng)建一個(gè) Applicant(申請者) 控制器用來處理用戶注冊:
1
|
rails g controller applicants new create |
applicants 控制器提供了兩個(gè)方法:
- new: 用來處理注冊界面
- create: 用來保存注冊信息
上面命令創(chuàng)建的控制器方法,默認(rèn)使用的都是 get 請求。保存注冊信息的 create 方法使用的是 post 請求。所以需要到 config/routes.rb 中修改如下內(nèi)容:
1
|
post 'applicants/create' |
完成注冊控制器功能:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# 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 , :password , :password_confirmation ) end end |
完成注冊界面功能:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<!-- app/views/applicants/ new .html.erb --> <h1>注冊</h1> <% if @user .errors.any? %> <ul> <% @user .errors.full_messages. each do |message| %> <li><%= message %></li> <% end %> </ul> <% end %> <%= form_for @user , url: :applicants_create do |f| %> <p> <%= f.label :name %> <%= f.text_field :name %> </p> <p> <%= f.label :password %> <%= f.password_field :password %> </p> <p> <%= f.label :password_confirmation %> <%= f.password_field :password_confirmation %> </p> <p><%= f.button "提交" %></p> <% end %> |
這樣就簡單的實(shí)現(xiàn)了注冊功能。
實(shí)現(xiàn)登錄功能
創(chuàng)建一個(gè) Session(會(huì)話) 控制器用來處理用戶登錄和退出:
1
|
rails g controller sessions new create |
這里在 sessions 控制器上默認(rèn)創(chuàng)建了 2 個(gè)方法:
- new: 用來處理登錄界面
- create 用來處理登錄流程
跟注冊一樣,需要修改 create 的默認(rèn)路由為 post:
1
2
3
|
# config/routes.rb post 'sessions/create' |
完成會(huì)話控制器的功能:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# 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[ :password ]) if user render plain: sprintf( "welcome, %s!" , user.name) else flash.now[ :login_error ] = "invalid username or password" render "new" end end private def user_params params.require( :session ).permit( :name , :password ) end end |
完成會(huì)話登錄界面:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<!-- app/views/sessions/ new .html.erb --> <h1>登錄</h1> <% if flash[ :login_error ] %> <p><%= flash[ :login_error ] %></p> <% end %> <%= form_for :session , url: :sessions_create do |f| %> <p> <%= f.label :name %> <%= f.text_field :name %> </p> <p> <%= f.label :password %> <%= f.password_field :password %> </p> <p><%= f.button "登錄" %></p> <% end %> |