在 Rails 实现用户注册和登录功能是非常方便的,比如可以使用 Devise 这类实现了完整功能的 gem 扩展包。也可以使用 Rails 自带的 has_secure_password 来自已打造。下面就是尝试使用 has_secure_password 来实现用户注册和登录功能。
准备工作
创建项目:
1
|
rails new user_login |
has_secure_password 中加密的功能需要用到 bcrypt ,所以需要在项目中启用 bcrypt gem 包。进入项目目录,修改 Gemfile 文件如下内容:
1
2
|
# Use ActiveModel has_secure_password gem 'bcrypt' , '~> 3.1.7' |
保存后退出,执行 bundle install 命令安装新启用的 gem 包。
创建用户模块
操作和管理用户信息需要先创建保存用户的数据表和模型:
1
|
rails g model user name:string password_digest:string |
password_digest 这个字段是用来保存加密混淆后的密码串的,必须提供并且不能更改成其它名称,否则无法正常使用 has_secure_password 提供的功能。
然后在用户模块中引入 has_secure_password 功能:
1
2
3
4
|
# app/models/user.rb class User < ActiveRecord::Base has_secure_password end |
创建用户数据表:
1
|
rake db:migrate |
实现注册功能
创建一个 Applicant(申请者) 控制器用来处理用户注册:
1
|
rails g controller applicants new create |
applicants 控制器提供了两个方法:
- new: 用来处理注册界面
- create: 用来保存注册信息
上面命令创建的控制器方法,默认使用的都是 get 请求。保存注册信息的 create 方法使用的是 post 请求。所以需要到 config/routes.rb 中修改如下内容:
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 %> |
这样就简单的实现了注册功能。
实现登录功能
创建一个 Session(会话) 控制器用来处理用户登录和退出:
1
|
rails g controller sessions new create |
这里在 sessions 控制器上默认创建了 2 个方法:
- new: 用来处理登录界面
- create 用来处理登录流程
跟注册一样,需要修改 create 的默认路由为 post:
1
2
3
|
# config/routes.rb post 'sessions/create' |
完成会话控制器的功能:
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 |
完成会话登录界面:
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 %> |