Advertisement
Guest User

Heru Quipper Question Updated

a guest
Oct 17th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.71 KB | None | 0 0
  1. # file application_controller
  2. def render_error(payload, status_code = :bad_request)
  3.   # status code can be override to 403, 404, or even 500 as needed
  4.   render json: {error: payload, success: false}.to_json, status: status_code
  5. end
  6.  
  7. def render_success(payload, includes = nil, status_code = :ok)
  8.   # status code can be override to 201 for new item creation
  9.   render json: {data: payload, success: true}.to_json(:include => includes), status: status_code
  10. end
  11.  
  12. # file auth_controller
  13. def auth
  14.    user = User.where(email: params[:username]).first
  15.    if user && user.authenticate(params[:password])
  16.      render_success(user)
  17.    else
  18.      render_error("wrong username or password", :unauthorized)
  19.    end
  20. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement