Class: AuthsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
lib/generators/authkit/templates/app/controllers/auths_controller.rb

Overview

The AuthsController is used for connecting accounts only. The user must be logged in for the connection to work. This controller is not used for creating a new session.

Instance Method Summary collapse

Instance Method Details

#callbackObject



18
19
20
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 'lib/generators/authkit/templates/app/controllers/auths_controller.rb', line 18

def callback
  # If we are not connecting we want to logout any existing user
  logout unless connecting?

  if connecting?
    if auth && auth.user == current_user
      # This is an auth that is already connected to this user (success, noop)
      redirect_to settings_path
    elsif auth && auth.user_id != current_user.id
      # This is an auth that is connected to another user (error)
      deny_user("Sorry, this account is already connected to another account", settings_path)
    else
      # Success, add the auth and redirect to settings
      @auth ||= current_user.auths.build(auth_params)

      if current_user.save
        redirect_to 
      else
        flash[:error] = "Sorry, there was an error connecting this account"
        redirect_to accounts_path
      end
    end
  else
    # Could have a check here for login/sign up action to be explicit
    #
    # If login and auth does not exist, confirm that they want to signup (not attach)
    # If signup and auth exists, confirm that they have selected the right account (already exists)
    if auth
      (auth.user)
      redirect_to 
    elsif auth_email.present? && User.where(email: auth_email.downcase).count > 0
      deny_user("Sorry, the email address associated with this account is already connected to an existing user", )
    else
      @signup = Signup.new_with_oauth(auth_params, {kind: @kind})

      if .save
        (.user)
        redirect_to 
      else
        flash[:error] = "Sorry, there was an error connecting this account (#{@signup.errors.full_messages.to_sentence})"
        redirect_to 
      end
    end
  end
end

#connectObject

Adjust scope here for particular sets of user using the session

 session[:google_oauth_scope] = 'userinfo.email, userinfo.profile, adsense, adsense.readonly'

You can also reset it to the default using nil


15
16
# File 'lib/generators/authkit/templates/app/controllers/auths_controller.rb', line 15

def connect
end

#disconnectObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/generators/authkit/templates/app/controllers/auths_controller.rb', line 64

def disconnect
  # TODO: you may want to change this lookup to use uid and provider
  @auth = current_user.auths.where(params[:id])
  @auth.destroy
  respond_to do |format|
    format.json { head :no_content }
    format.html {
      redirect_to accounts_path
    }
  end
end

#failureObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/generators/authkit/templates/app/controllers/auths_controller.rb', line 76

def failure
  flash[:error] = "Sorry, there was an error connecting this account: #{params[:message]}"
  if connecting?
    redirect_to settings_path
  elsif signing_up?
    redirect_to 
  else
    redirect_to 
  end
end