Class: Api::V1::SessionsController

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

Instance Method Summary collapse

Instance Method Details

#createObject



7
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
36
37
38
39
# File 'app/controllers/kebab_remote_api/api/v1/sessions_controller.rb', line 7

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

  # Checking whether format is json or not
 if request.format != :json
  render status: 406, json: { message: I18n.t('kebab_remote_api.not_json') }
    return
  end

  # Checking for empty email or password
  if email.nil? or password.nil?
    render status: 400, json: { message: I18n.t('kebab_remote_api.empty_input') }
    return
  end

  @admin = Admin.where(email: email.downcase).first

  # Checking whether user exists
  if @admin.nil?
    render status: 401, json: { message: I18n.t('kebab_remote_api.invalid_field') }
    return
  end

  @admin.ensure_authentication_token!

  # Finally, checking for password
  unless @admin.valid_password?(password)
    render status: 401, json: { message: I18n.t('kebab_remote_api.invalid_field') }
  else
    render status: 200, json: @admin.as_json.merge(success: true)
  end
end

#destroyObject



41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/kebab_remote_api/api/v1/sessions_controller.rb', line 41

def destroy
  @admin = Admin.find_by_authentication_token(params[:id])

  if @admin.nil?
    render status: 404, json: { message: I18n.t('kebab_remote_api.invalid_token') }
  else
    @admin.reset_authentication_token!
    render status: 200, json: { token: params[:id] }
  end
end