Skip to content

Instantly share code, notes, and snippets.

@ffullenk
Last active August 29, 2015 14:02
Show Gist options
  • Save ffullenk/cac019c865a2ea918e54 to your computer and use it in GitHub Desktop.
Save ffullenk/cac019c865a2ea918e54 to your computer and use it in GitHub Desktop.
TokensController
namespace :api do
namespace :v1 do
post "tokens" => "tokens#create"
delete "tokens/:token" => "tokens#destroy"
end
end
class Api::V1::TokensController < ApplicationController
skip_before_filter :verify_authenticity_token
respond_to :json
def create
email = params[:email]
password = params[:password]
if request.format != :json
render :status=>406, :json=>{:message=>"The request must be json"}
return
end
if email.nil? or password.nil?
render :status=>400, :json=>{:message=>"The request must contain the user email and password."}
return
end
@user=User.find_by_email(email.downcase)
if @user.nil?
logger.info("User #{email} failed signin, user cannot be found.")
render :status=>401, :json=>{:message=>"Email o password no válidos."}
return
end
@user.ensure_authentication_token
@user.save!
if not @user.valid_password?(password)
logger.info("User #{email} failed signin, password \"#{password}\" is invalid")
render :status=>401, :json=>{:message=>"Invalid email or passoword."}
else
render :status=>200, :json=>{:token=>@user.authentication_token}
end
end
def destroy
@user=User.find_by_authentication_token(params[:id])
if @user.nil?
logger.info("Token not found.")
render :status=>404, :json=>{:message=>"Invalid token."}
else
@user.reset_authentication_token!
render :status=>200, :json=>{:token=>params[:id]}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment