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_hash ⇒ Object



81
82
83
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 81

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

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



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

def generate_url(url, params = {})
  uri = URI(url)
  uri.query = params.to_query
  uri.to_s
end

#omniauth_failure ⇒ Object



73
74
75
76
77
78
79
# File 'app/controllers/devise_token_auth/auth_controller.rb', line 73

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_success ⇒ Object



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

def omniauth_success
  # pull resource class from omniauth return
  resource = request.env['omniauth.params']['resource_class'].constantize

  # find or create user by provider and provider uid
  @user = resource.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']
  })

  # 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_token ⇒ Object



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