ruby - How can i refresh token using omniauth gem? - Rails 4.2 -
so far have managed login users google plus using omniauth-google-oauth2 gem.
as far concerned access token expires @ time. how can refresh token google api lets once week if user logins google plus on website in week now?
below code have in sessioncontroller.rb
def google_oauth2 auth_hash = request.env['omniauth.auth'] @identity = identity.find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"]) if @identity log_in @identity.user flash[:success] = "welcome, #{@identity.user.name}!" else random_password = securerandom.urlsafe_base64(n=6) #@user = create_with_omniauth(auth_hash) @user = user.new( name:auth_hash['info']['name'], email:auth_hash['info']['email'], password:random_password, password_confirmation:random_password, activated:true ) @user.identities.build( uid: auth_hash['uid'], provider: auth_hash['provider'], image_url: auth_hash['info']['image'], oauth_token: auth_hash['credentials']['token'], oauth_refresh_token: auth_hash['credentials']['refresh_token'], oauth_expires_at: auth_hash['credentials']['expires_at'] ) if @user.save! log_in @user flash[:success] = "hello, #{@user.name}! account has been created you!" else flash[:warning] = "there error while trying authenticate you..." end end redirect_to root_path end
some attributes saved in user table , in identity table has_many identities, belongs user model because in future want add multiple providers.
Comments
Post a Comment