asciinema.org/app/middleware/omni_auth_user_loader.rb

37 lines
705 B
Ruby
Raw Normal View History

class OmniAuthUserLoader
def initialize(app)
@app = app
end
def call(env)
omniauth = env['omniauth.auth']
if omniauth
env['asciiio.user'] = find_user(omniauth) || build_user(omniauth)
end
@app.call(env)
end
private
def find_user(omniauth)
2013-08-25 14:34:02 +00:00
query = { :provider => omniauth['provider'], :uid => omniauth['uid'].to_s }
User.where(query).first
end
def build_user(omniauth)
user = User.new
user.provider = omniauth['provider']
user.uid = omniauth['uid']
user.nickname = omniauth['info']['nickname']
user.name = omniauth['info']['name']
user.avatar_url = OauthHelper.get_avatar_url(omniauth)
user
end
end