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



68
69
70
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 68

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

#omniauth_failureObject



61
62
63
64
65
66
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 61

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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

  # 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
  }
  @user.save

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

  # 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