Class: DeviseTokenAuth::AuthController

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

Instance Method Summary collapse

Instance Method Details

#auth_hashObject



72
73
74
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 72

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

#omniauth_failureObject



64
65
66
67
68
69
70
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 64

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



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
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 21

def omniauth_success
  # find or create user by provider and provider uid
  @user = User.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)

  # 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 + 2.weeks
  }

  # 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']
  })

  # 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

#validate_tokenObject



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

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