class Api::V2::SessionsController

Authenticates user before accessing the api

Information returns in <user_info>;

{:name=>"foo bar",
:id=>229,
:email=>"foo@bar.com",
:bio=>"",
:contributions=>0,
:projects=>0,
:collaborations=>0,
:institute=>"UOK",
:profile_url=>"http://localhost:5000/users/foobar",
:avatar=>
 {:original=>"/avatar.png",
  :thumb=>"/assets/thumb_avatar.png",
  :medium=>"/assets/medium_avatar.png"},
:latest_activity=>Thu, 19 Mar 2015 15:26:01 UTC +00:00,
:badges=>[{:name=>"", :url=>""}],
:auth_token=>"bbnikcdUqUX1yzfb7XsL"}

Public Instance Methods

create() click to toggle source

User sign in

/api/v2/sign_in(.:format)

Input params;

{:user => {:email => “???”, :password => “???”}}

Ex;

post "create", {:user => {:email=>foo@bar.com, :password => "xxxxxxx"}, :format=>:json}

Output;

On success - {:status => "success", :user => <user_info>}
On failed - {:status => "failed", :notice => "..."}
# File app/controllers/api/v2/sessions_controller.rb, line 45
def create
  resource = warden.authenticate!(scope: resource_name, recall: "#{controller_path}#failure")
  sign_in(resource_name, resource)
  resource.restore_authentication_token!

  render json: {status: "success", user: resource.info(auth: true)}
end
destroy() click to toggle source

User sign out

/api/v2/sign_out(.:format)

Input params;

:user_email, :user_token

Ex;

post "destroy", {:user_email => foo@bar.com, :user_token => "xxxxxxx"}

Output;

On success - HTTP Response 204
On failed - {:status => "failed", :notice => "..."}
# File app/controllers/api/v2/sessions_controller.rb, line 71
def destroy
  user = User.find_by_authentication_token(params[:user_token])
  sign_out(user)

  # REFERENCE: http://www.tagwith.com/question_121651_simple-token-authentication-signout-for-rails-json-api
  # user.authentication_token = ""
  # user.save

  render json: {status: "success", notice: "Logout successful"}

rescue Exception => e
    logger.error(e.message)
    render json: {status: "failed", notice: "Error: Logout failed"}
end
failure() click to toggle source
# File app/controllers/api/v2/sessions_controller.rb, line 86
def failure
  warden.custom_failure!
  render json: {status: "failed", notice: "Login information is incorrect, please try again."}
end