Class: API::V1::LoginController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/api/v1/login_controller.rb

Overview

The API login controller.

Instance Method Summary (collapse)

Instance Method Details

- (Object) get_auth_token

Get the authenticaton token for the customer.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/api/v1/login_controller.rb', line 8

def get_auth_token
  email = params[:email]
  password = params[:password]

  if email.nil?
    render :status => 400, :json => { :error => { :message => 'The request must contain a valid email.', :code => 'InvalidRequest' }}
    return
  end

  if password.nil?
    render :status => 400, :json => { :error => { :message => 'The request must contain a valid password.', :code => 'InvalidRequest' }}
    return
  end

  customer = Customer.find_by_email(email.downcase)

  if customer.nil?
    render :status => 400, :json => { :error => { :message => 'User not found.', :code => 'InvalidRequest' }}
    return
  end

  if customer.valid_password?(password)
    customer.ensure_authentication_token
    render :status => 200, :json => { :auth_token => customer.get_devise_auth_token }
  else
    render :status => 400, :json => { :error => { :message => 'Invalid email or password.', :code => 'InvalidRequest' }}
  end
end

- (Object) sign_in_with_facebook

Sign the customer in via facebook



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/api/v1/login_controller.rb', line 64

def 
  auth_token = params[:auth_token]

  if auth_token.nil?
    render :status => 400, :json => { :error => { :message => 'The request must contain a valid authentication token.', :code => 'InvalidRequest' }}
    return
  end

  graph = Koala::Facebook::API.new(auth_token)
  facebook_data = graph.get_object('me')

  if facebook_data.nil?
    render :status => 400, :json => { :error => { :message => 'Facebook data is empty.', :code => 'FacebookError' }}
    return
  end

  customer = Customer.from_facebook(auth_token, facebook_data)

  if customer.nil?
    render :status => 400, :json => { :error => { :message => 'Facebook sign in error.', :code => 'SignInError' }}
    return
  end

  if customer.persisted?

    # create default device token
    customer.ensure_authentication_token
    render :status => 200, :json => { :auth_token => customer.get_devise_auth_token }
  else
    render :status => 400, :json => { :error => { :message => 'Facebook sign in error.', :code => 'SignInError' }}
  end
end

- (Object) sign_out

Sign the customer out from the system and invalidate the authentication token.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/api/v1/login_controller.rb', line 39

def sign_out
  email = params[:email]
  auth_token = params[:auth_token]

  if email.nil?
    render :status => 400, :json => { :error => { :message => 'The request must contain a valid email.', :code => 'InvalidRequest' }}
    return
  end

  customer = Customer.find_by_email(email.downcase)

  if auth_token.nil?
    render :status => 400, :json => { :error => { :message => 'The request must contain a valid authentication token.', :code => 'InvalidRequest' }}
    return
  end

  if customer.remove_devise_auth_token(auth_token)
    render :status => 200, :json => { :message => 'Logout was successful.', :code => 'OK' }
  else
    render :status => 400, :json => { :error => { :message => 'Logout went wrong.', :code => 'LogoutError' }}
  end
end