diff --git a/app/models/authentication.rb b/app/models/authentication.rb index b8ef8b3..e74ac3f 100644 --- a/app/models/authentication.rb +++ b/app/models/authentication.rb @@ -12,6 +12,14 @@ class Authentication < ApplicationRecord expires_at <= Time.current 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 return nil unless provider == 'google' @@ -20,10 +28,7 @@ class Authentication < ApplicationRecord c.refresh_token = refresh_token c.expires_at = expires_at if expires_at < 1.minute.from_now - c.refresh! - 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? + refresh_from(c) end end end