Save authentication after access token refresh

This commit is contained in:
bumi 2020-04-28 09:39:24 +02:00
parent fa348cdeeb
commit da70fef2f4

View File

@ -12,6 +12,14 @@ class Authentication < ApplicationRecord
expires_at <= Time.current expires_at <= Time.current
end end
def refresh_from(client_secret)
client_secret.refresh!
self.access_token = client_secret.access_token if client_secret.access_token.present?
self.refresh_token = client_secret.refresh_token if client_secret.refresh_token.present?
self.expires_at = Time.zone.at(client_secret.expires_at) if client_secret.expires_at.present?
save
end
def google_authorization def google_authorization
return nil unless provider == 'google' return nil unless provider == 'google'
@ -20,10 +28,7 @@ class Authentication < ApplicationRecord
c.refresh_token = refresh_token c.refresh_token = refresh_token
c.expires_at = expires_at c.expires_at = expires_at
if expires_at < 1.minute.from_now if expires_at < 1.minute.from_now
c.refresh! refresh_from(c)
self.access_token = c.access_token if c.access_token.present?
self.refresh_token = c.refresh_token if c.refresh_token.present?
self.expires_at = Time.zone.at(c.expires_at) if c.expires_at.present?
end end
end end
end end