cirandas.net

ref: master

plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb


 1
 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
39
40
41
42
43
44
class OauthClientPluginPublicController < PublicController

  skip_before_filter :login_required

  def callback
    auth = request.env["omniauth.auth"]
    auth_user = environment.users.where(email: auth.info.email).first
    if auth_user then login auth_user.person else signup auth end
  end

  def failure
    session[:notice] = _('Failed to login')
    redirect_to root_url
  end

  def destroy
    session[:user] = nil
    redirect_to root_url
  end

  protected

  def login person
    provider = OauthClientPlugin::Provider.find(session[:provider_id])
    auth = person.oauth_auths.where(provider_id: provider.id).first
    auth ||= person.oauth_auths.create! profile: person, provider: provider, enabled: true
    if auth.enabled? && provider.enabled?
      self.current_user = person.user
    else
      session[:notice] = _("Can't login with %s") % provider.name
    end

    redirect_to :controller => :account, :action => :login
  end

  def signup(auth)
    login = auth.info.email.split('@').first
    session[:oauth_data] = auth
    name = auth.info.name
    name ||= auth.extra && auth.extra.raw_info ? auth.extra.raw_info.name : ''
    redirect_to :controller => :account, :action => :signup, :user => {:login => login, :email => auth.info.email}, :profile_data => {:name => name}
  end

end