ruby on rails - Google Oauth2 automatially renew token -
i'm adding oauth2 via google rails app using omniauth-google-oauth2 gem. have set according gem's readme. when click authorize button in app, directed google authorize app. once i'm sent redirect path, hash of data includes token, refresh_token, , expires_at (not expires_in), expires_at equal time.now.to_i. hash looks this:
=> auth.credentials => { token: 'token', refresh_token: 'refresh_token', expires_at: 1468603958 expires: true } what programmatically renew user's token every time log app, without forcing them go through initial authorization process. have similar token renewal flow set facebook, can't figure out how make work google, since token expires_at time now.
my question two-part:
- shouldn't value of
expires_atin future (e.g. 1 hour now, or 30 days now)? - how can programmatically refresh token using
refresh_tokengot when first authorized app? can't seem find single example in ruby on how this. wanted set method ongeneralcontrollerin app checksexpires_atvalue ofidentitymodel , runs logic renew token, can't figure out how renew token. seems problematic givenexpires_atvalue got google equaltime.now.to_i. if uses value, trigger infinite loop.
here's want do in pseudo code:
class generalcontroller < applicationcontroller before_action :renew_google_token, only: [:home] def renew_google_token # first identity expire. repeated unti lall identities updated current_user.identities.where(provider: :google).where("expires_at < (?)", time.now + 7.days).each |identity| # logic pings google , renews token new_token_hash = identity.update(token: new_token_hash.token, expires_at: time.at(new_token_hash.expires_at)) end end end
Comments
Post a Comment