ambition/app/controllers/sessions_controller.rb

30 lines
711 B
Ruby
Raw Normal View History

class SessionsController < ApplicationController
2016-12-16 13:55:32 +08:00
# 除登录之外,其余接口必须在登录状态下访问
before_action :authenticate, except: [ :create ]
skip_before_action :verify_authenticity_token, :only => [:create,:destroy,:show]
2017-01-01 17:00:27 +08:00
# 用户登录
def create
user = User.find_by(email: params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
render status: :ok, text: 'ok'
else
render status: :unauthorized, text: 'account or password is not correct'
end
end
def destroy
2016-12-16 13:55:32 +08:00
session.delete :user_id
@current_user &&= nil
render status: :ok, nothing: true
end
2016-12-16 13:55:32 +08:00
def show
render 'show'
end
end