Class: Users::OmniauthCallbacksController

Inherits:
Devise::OmniauthCallbacksController
  • Object
show all
Defined in:
app/controllers/users/omniauth_callbacks_controller.rb

Instance Method Summary collapse

Instance Method Details

#githubObject


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/users/omniauth_callbacks_controller.rb', line 2

def github
   = env["omniauth.auth"].extra.raw_info.
  github_token = env["omniauth.auth"].credentials.token
  github_user  = User.where(:github_login => ).first

  if github_user.nil? && github_org_id = Errbit::Config.github_org_id
    # See if they are a member of the organization that we have access for
    # If they are, automatically create an account
    client = Octokit::Client.new(access_token: github_token)
    org_ids = client.organizations.map { |org| org.id }
    if org_ids.include?(github_org_id)
      github_user = User.create(name: env["omniauth.auth"].extra.raw_info.name, email: env["omniauth.auth"].extra.raw_info.email)
    end
  end

  # If user is already signed in, link github details to their account
  if current_user
    # ... unless a user is already registered with same github login
    if github_user && github_user != current_user
      flash[:error] = "User already registered with GitHub login '#{}'!"
    else
      # Add github details to current user
      update_user_with_github_attributes(current_user, , github_token)
      flash[:success] = "Successfully linked GitHub account!"
    end
    # User must have clicked 'link account' from their user page, so redirect there.
    redirect_to user_path(current_user)
  elsif github_user
    # Store OAuth token
    update_user_with_github_attributes(github_user, , github_token)
    flash[:success] = I18n.t "devise.omniauth_callbacks.success", :kind => "GitHub"
     github_user, :event => :authentication
  else
    flash[:error] = "There are no authorized users with GitHub login '#{}'. Please ask an administrator to register your user account."
    redirect_to new_user_session_path
  end
end