Class: Goldencobra::Api::V1::TokensController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/goldencobra/api/v1/tokens_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



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
# File 'app/controllers/goldencobra/api/v1/tokens_controller.rb', line 8

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

  if email.nil? || password.nil?
    render status: 400, json: { error: 'The request must contain ' +
                                    'the user email and password.' }
    return
  end

  @visitor = Visitor.find_by_email(email)

  if @visitor == nil
    render status: 401, json: { error: "Invalid email or password." }
    return
  end

  @visitor.ensure_authentication_token!

  if @visitor.valid_password?(password)
    render status: 200, json: { token: @visitor.authentication_token }
  else
    render status: 401, json: { error: "Invalid email or password." }
  end
end

#showObject



34
35
36
37
38
39
40
41
# File 'app/controllers/goldencobra/api/v1/tokens_controller.rb', line 34

def show
  unless current_user.present?
    render status: 400, json: { error: 'You are not logged in' }
    return
  end
  current_user.ensure_authentication_token!
  render status: 200, json: { token: current_user.authentication_token }
end