Sha256: 7290575ecd04cb484d873630fa2ef6aafadfb178aa7b6b49b28cdb3af75300ef

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

module Vxod
  class LoginWithOpenid
    def initialize(app)
      @app = app
    end

    attr_reader :app

    def login
      identity = Db.identity.find_by_openid(provider, openid)

      if identity.nil?
        identity = Db.identity_create(provider, openid, email, firstname, lastname)
      end

      if Email.valid?(identity.user.email)
        app.authentify(identity.user.auth_key)
        app.redirect_to_after_login
      else
        app.authentify_for_fill_user_data(identity.user.auth_key)
        app.redirect(Vxod.config.fill_user_data_path)
      end
    end

    def save_user_data
      if Email.valid?(app.params['email'])
        user = Db.user.find_by_auth_key(app.auth_key_for_fill_user_data)
        user.email      = app.params['email']
        user.firstname  = app.params['firstname']
        user.lastname   = app.params['lastname']
        user.save!

        app.authentify(user.auth_key)
        app.redirect_to_after_login
      else
        false
      end
    end

  private

    def info
      app.omniauth_hash[:info]
    end

    def email
      if info && info[:email]
        info[:email]
      else
        nil
      end
    end

    def firstname
      if info && info[:first_name]
        info[:first_name]
      else
        nil
      end
    end

    def lastname
      if info && info[:last_name]
        info[:last_name]
      else
        nil
      end
    end

    def openid
      app.omniauth_hash[:uid]
    end

    def provider
      app.omniauth_hash[:provider]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vxod-0.0.2 lib/vxod/login_with_openid.rb