2015-12-21 06:44:11 +08:00
|
|
|
#encoding: utf-8
|
|
|
|
class UsersController < ApplicationController
|
|
|
|
def register
|
2015-11-23 14:13:41 +08:00
|
|
|
@user = User.new
|
|
|
|
render 'register', layout: 'register'
|
2015-12-21 06:44:11 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def register_confirm
|
2016-01-04 13:54:35 +08:00
|
|
|
@user = User.new params.require(:user).permit(:username,:email,:password,:password_confirmation)
|
2015-11-23 14:13:41 +08:00
|
|
|
if @user.save
|
|
|
|
to_login @user
|
|
|
|
redirect_to root_path
|
|
|
|
else
|
|
|
|
render 'register', layout: 'register'
|
|
|
|
end
|
2015-12-21 06:44:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def login
|
2016-01-04 13:54:35 +08:00
|
|
|
return redirect_to(login_path(from: referer)) unless params[:from].present?
|
2015-11-24 14:13:14 +08:00
|
|
|
@user = User.new
|
|
|
|
render 'login', layout: 'register'
|
2015-12-21 06:44:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def login_confirm
|
2015-11-24 14:13:14 +08:00
|
|
|
@user = User.find_by username: params[:user][:username]
|
|
|
|
if @user && @user.check_password(params[:user][:password])
|
|
|
|
to_login @user
|
|
|
|
@user.update_attribute :last_login_time, DateTime.now
|
2016-01-04 13:54:35 +08:00
|
|
|
redirect_to (params[:from].present? ? params[:from] : root_path)
|
2015-11-24 14:13:14 +08:00
|
|
|
else
|
|
|
|
flash[:error] = '用户名或密码错误'
|
|
|
|
render 'login', layout: 'register'
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
flash[:error] = '用户名或密码错误'
|
|
|
|
render 'login', layout: 'register'
|
2015-12-21 06:44:11 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def logout
|
2015-11-24 14:13:14 +08:00
|
|
|
session[:user_id] = nil
|
|
|
|
redirect_to referer
|
2015-12-21 06:44:11 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def to_login(user)
|
2015-11-24 14:13:14 +08:00
|
|
|
session[:user_id] = user.id
|
2015-12-21 06:44:11 +08:00
|
|
|
|
|
|
|
end
|
2015-11-23 14:13:41 +08:00
|
|
|
end
|