Class: Api::V1::TokensController

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

Instance Method Summary collapse

Instance Method Details

#createObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/api/v1/tokens_controller.rb', line 5

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