Advertisement
Guest User

Untitled

a guest
Apr 12th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.74 KB | None | 0 0
  1. class ApplicationController < ActionController::API
  2.     include ActionController::HttpAuthentication::Basic::ControllerMethods
  3.     include ActionController::HttpAuthentication::Token::ControllerMethods
  4.     before_filter :authenticate_user_from_token,except: [:token]
  5.  
  6.     def token
  7.         authenticate_with_http_basic do |email, password|
  8.             user = User.find_by(email: email)
  9.             if user && user.password == password
  10.                 render json: {token: user.auth_token}
  11.             else
  12.                 render json: {error: 'Incorect credentials'}, status: 401
  13.             end
  14.         end
  15.     end
  16.  
  17.     private
  18.  
  19.     def authenticate_user_from_token
  20.         unless authenticate_with_http_token { |token, options| User.find_by(auth_token: token) }
  21.             render json: { error: 'Bad Token'}, status:401
  22.         end
  23.     end
  24. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement