Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export ability to get session without validation #373

Merged
merged 1 commit into from
May 9, 2024

Conversation

leoMehlig
Copy link
Contributor

This PR gives the client the ability to get the session without validating it first. This can be useful to display user info while offline.
The changes in SessionManager are required, since before the shouldValidateExpiration parameter was ignored if a task is already running.

@leoMehlig leoMehlig requested a review from grdsdev as a code owner May 9, 2024 11:24
@grdsdev
Copy link
Collaborator

grdsdev commented May 9, 2024

Hey, I was wondering if we could maybe use something like what I'm patching below.

Flutter lib already provides properties such as currentSession and currentUser, so let's use the same API.

From a9b6b59a38b292de8ca9f4060dace7ce636a0268 Mon Sep 17 00:00:00 2001
From: Guilherme Souza <grsouza@pm.me>
Date: Thu, 9 May 2024 08:43:16 -0300
Subject: [PATCH] feat(auth): get current session without validation

---
 Sources/Auth/AuthClient.swift | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/Sources/Auth/AuthClient.swift b/Sources/Auth/AuthClient.swift
index f7f67efb..ffab37e6 100644
--- a/Sources/Auth/AuthClient.swift
+++ b/Sources/Auth/AuthClient.swift
@@ -26,6 +26,20 @@ public final class AuthClient: Sendable {
       try await sessionManager.session()
     }
   }
+  
+  /// Returns the current session, if any.
+  ///
+  /// The session returned by this property may be expired. Use ``session`` for a session that is guaranteed to be valid.
+  public var currentSession: Session? {
+    try? configuration.localStorage.getSession()?.session
+  }
+
+  /// Returns the current user, if any.
+  ///
+  /// The user returned by this property may be outdated. Use ``user(jwt:)`` method to get an up-to-date user instance.
+  public var currentUser: User? {
+    try? configuration.localStorage.getSession()?.session.user
+  }
 
   /// Namespace for accessing multi-factor authentication API.
   public let mfa = AuthMFA()
@@ -890,8 +904,7 @@ public final class AuthClient: Sendable {
   /// - Parameter jwt: Takes in an optional access token jwt. If no jwt is provided, user() will
   /// attempt to get the jwt from the current session.
   ///
-  /// Should be used only when you require the most current user data. For faster results,
-  /// session.user is recommended.
+  /// Should be used only when you require the most current user data. For faster results, ``currentUser`` is recommended.
   public func user(jwt: String? = nil) async throws -> User {
     var request = HTTPRequest(url: configuration.url.appendingPathComponent("user"), method: .get)

@leoMehlig
Copy link
Contributor Author

@grdsdev Yes that would also work!

@leoMehlig
Copy link
Contributor Author

@grdsdev I applied your patch to the branch.

@grdsdev grdsdev merged commit 4b01556 into supabase:main May 9, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants