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



54
55
56
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 54

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

#omniauth_failureObject



47
48
49
50
51
52
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 47

def omniauth_failure
  render json: {
    success: false,
    errors: [params[:message]]
  }, status: 500
end

#omniauth_successObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 22

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

  @token = SecureRandom.urlsafe_base64(nil, false)

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

  # 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

#validate_tokenObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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
    }
  else
    render json: {
      success: false,
      errors: ["Invalid login credentials"]
    }, status: 401
  end
end