Class: DeviseTokenAuth::AuthController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/devise_token_auth/auth_controller.rb

Instance Method Summary collapse

Instance Method Details

#auth_hashObject



85
86
87
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 85

def auth_hash
  request.env['omniauth.auth']
end

#devise_controller?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 101

def devise_controller?
  true
end

#devise_mappingObject

necessary for access to devise_parameter_sanitizers



115
116
117
118
119
120
121
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 115

def devise_mapping
  if request.env['omniauth.params']
    Devise.mappings[request.env['omniauth.params']['resource_class'].underscore.to_sym]
  else
    request.env['devise.mapping']
  end
end

#generate_url(url, params = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 123

def generate_url(url, params = {})
  auth_url = url

  # ensure that hash-bang is present BEFORE querystring for angularjs
  unless url.match(/#/)
    auth_url += '#'
  end

  # add query AFTER hash-bang
  auth_url += "?#{params.to_query}"

  return auth_url
end

#omniauth_failureObject



77
78
79
80
81
82
83
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 77

def omniauth_failure
  @error = params[:message]

  respond_to do |format|
    format.html { render :layout => "omniauth_response", :template => "devise_token_auth/omniauth_failure" }
  end
end

#omniauth_successObject



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
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 24

def omniauth_success
  # find or create user by provider and provider uid
  @user = resource_name.where({
    uid:      auth_hash['uid'],
    provider: auth_hash['provider']
  }).first_or_initialize

  # create client id
  @client_id = SecureRandom.urlsafe_base64(nil, false)
  @token     = SecureRandom.urlsafe_base64(nil, false)

  @auth_origin_url = generate_url(request.env['omniauth.params']['auth_origin_url'], {
    token:     @token,
    client_id: @client_id,
    uid:       @user.uid
  })

  # set crazy password for new oauth users. this is only used to prevent
  # access via email sign-in.
  unless @user.id
    p = SecureRandom.urlsafe_base64(nil, false)
    @user.password = p
    @user.password_confirmation = p
  end

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

  # sync user info with provider, update/generate auth token
  @user.assign_attributes({
    nickname: auth_hash['info']['nickname'],
    name:     auth_hash['info']['name'],
    image:    auth_hash['info']['image'],
    email:    auth_hash['info']['email']
  })

  # assign any additional (whitelisted) attributes
  extra_params = whitelisted_params
  @user.assign_attributes(extra_params) if extra_params

  # don't send confirmation email!!!
  @user.skip_confirmation!

  @user.save!

  # render user info to javascript postMessage communication window
  respond_to do |format|
    format.html { render :layout => "omniauth_response", :template => "devise_token_auth/omniauth_success" }
  end
end

#resource_nameObject

pull resource class from omniauth return



106
107
108
109
110
111
112
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 106

def resource_name
  if request.env['omniauth.params']
    request.env['omniauth.params']['resource_class'].constantize
  else
    super
  end
end

#validate_tokenObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 7

def validate_token
  # @user will have been set by set_user_token concern
  if @user
    render json: {
      success: true,
      data: @user.as_json(except: [
        :tokens, :confirm_success_url, :reset_password_redirect_url, :created_at, :updated_at
      ])
    }
  else
    render json: {
      success: false,
      errors: ["Invalid login credentials"]
    }, status: 401
  end
end

#whitelisted_paramsObject



89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 89

def whitelisted_params
  whitelist = devise_parameter_sanitizer.for(:sign_up)

  whitelist.inject({}){|coll, key|
    param = request.env['omniauth.params'][key.to_s]
    if param
      coll[key] = param
    end
    coll
  }
end