Skip to content

Instantly share code, notes, and snippets.

@hayduke19us
Created January 7, 2015 18:17
Show Gist options
  • Select an option

  • Save hayduke19us/c0bc35026e6f2c267491 to your computer and use it in GitHub Desktop.

Select an option

Save hayduke19us/c0bc35026e6f2c267491 to your computer and use it in GitHub Desktop.
class Api::V1::Scout::SessionsController < Devise::SessionsController
skip_before_filter :verify_authenticity_token
prepend_before_filter :allow_params_authentication!, only: :create
prepend_before_filter :require_no_authentication, :only => [:create ]
include Devise::Controllers::Helpers
respond_to :json
def create
if correct_headers?
resource = Scout.find_by_email(request.headers["Email"])
if resource.valid_password?(request.headers["Password"])
sign_in(resource_name, resource)
resource.save!
render json: { success: true,
message: "Scout has signed in",
token: ApiKey.create!.access_token }
else
render json: { success: false,
message: "Wrong email and password combination" }
end
else
render json: { success: false,
message: "Missing Email or Password" }
end
end
def destroy
token = ApiKey.find_by_access_token request.headers["Token"]
unless token.nil?
token.destroy
scout = Scout.find_by_email request.headers["Email"]
sign_out scout if scout
render json: {message: "you have been signed out" }
else
render json: {message: "token no longer exist sign in again"}
end
end
def respond_to_on_destroy
#Bullshit empty method to override Devise
end
protected
def correct_headers? header1="Password", header2="Email"
request.headers[header1] && request.headers[header2] ? true : false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment