Class: DeviseTokenAuth::SessionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/devise_token_auth/sessions_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#resource_class

Instance Method Details

#createObject



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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/devise_token_auth/sessions_controller.rb', line 13

def create
  # Check
  field = (resource_params.keys.map(&:to_sym) & resource_class.authentication_keys).first

  @resource = nil
  if field
    q_value = resource_params[field]

    if resource_class.case_insensitive_keys.include?(field)
      q_value.downcase!
    end

    q = "#{field.to_s} = ? AND provider='email'"

    if ActiveRecord::Base.connection.adapter_name.downcase.starts_with? 'mysql'
      q = "BINARY " + q
    end

    @resource = resource_class.where(q, q_value).first
  end

  if @resource and valid_params?(field, q_value) and @resource.valid_password?(resource_params[:password]) and (!@resource.respond_to?(:active_for_authentication?) or @resource.active_for_authentication?)
    # create client id
    @client_id = SecureRandom.urlsafe_base64(nil, false)
    @token     = SecureRandom.urlsafe_base64(nil, false)

    @resource.tokens[@client_id] = {
      token: BCrypt::Password.create(@token),
      expiry: (Time.now + DeviseTokenAuth.token_lifespan).to_i
    }
    @resource.save

    (:user, @resource, store: false, bypass: false)

    yield if block_given?

    render json: {
      data: @resource.token_validation_response
    }

  elsif @resource and not (!@resource.respond_to?(:active_for_authentication?) or @resource.active_for_authentication?)
    render json: {
      success: false,
      errors: [ I18n.t("devise_token_auth.sessions.not_confirmed", email: @resource.email) ]
    }, status: 401

  else
    render json: {
      errors: [I18n.t("devise_token_auth.sessions.bad_credentials")]
    }, status: 401
  end
end

#destroyObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/devise_token_auth/sessions_controller.rb', line 66

def destroy
  # remove auth instance variables so that after_filter does not run
  user = remove_instance_variable(:@resource) if @resource
  client_id = remove_instance_variable(:@client_id) if @client_id
  remove_instance_variable(:@token) if @token

  if user and client_id and user.tokens[client_id]
    user.tokens.delete(client_id)
    user.save!

    yield if block_given?

    render json: {
      success:true
    }, status: 200

  else
    render json: {
      errors: [I18n.t("devise_token_auth.sessions.user_not_found")]
    }, status: 404
  end
end

#get_auth_paramsObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/devise_token_auth/sessions_controller.rb', line 97

def get_auth_params
  auth_key = nil
  auth_val = nil

  # iterate thru allowed auth keys, use first found
  resource_class.authentication_keys.each do |k|
    if resource_params[k]
      auth_val = resource_params[k]
      auth_key = k
      break
    end
  end

  # honor devise configuration for case_insensitive_keys
  if resource_class.case_insensitive_keys.include?(auth_key)
    auth_val.downcase!
  end

  return {
    key: auth_key,
    val: auth_val
  }
end

#newObject



7
8
9
10
11
# File 'app/controllers/devise_token_auth/sessions_controller.rb', line 7

def new
  render json: {
    errors: [ I18n.t("devise_token_auth.sessions.not_supported")]
  }, status: 405
end

#resource_paramsObject



93
94
95
# File 'app/controllers/devise_token_auth/sessions_controller.rb', line 93

def resource_params
  params.permit(devise_parameter_sanitizer.for(:sign_in))
end

#valid_params?(key, val) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/controllers/devise_token_auth/sessions_controller.rb', line 89

def valid_params?(key, val)
  resource_params[:password] && key && val
end