Skip to content

Commit

Permalink
Update getSession and refreshSession
Browse files Browse the repository at this point in the history
Developers can pass the access and refresh tokens respectively. refreshSession
will also be able to return the output of the new session.

This is a temporary solution: a better one will be made in a future update.
  • Loading branch information
MasterJ93 committed Dec 11, 2024
1 parent 990851d commit 3a700fd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public class ATProtocolConfiguration: SessionConfiguration {
/// - Important: ``ATProtocolConfiguration/authenticate(authenticationFactorToken:)``,
/// ``ATProtocolConfiguration/createAccount(email:handle:existingDID:inviteCode:verificationCode:verificationPhone:password:recoveryKey:plcOperation:)``,
/// ``ATProtocolConfiguration/deleteSession()``,
/// ``ATProtocolConfiguration/getSession()``, and
/// ``ATProtocolConfiguration/getSession(by:)``, and
/// ``ATProtocolConfiguration/deleteSession()`` will not work when initializing
/// ATProtocolConfiguration with this initializer.
///
Expand Down Expand Up @@ -301,21 +301,26 @@ public class ATProtocolConfiguration: SessionConfiguration {
}

/// Fetches an existing session using an access token.
///
///
/// When the method completes, ``ATProtocolConfiguration/session`` will be updated with an
/// instance of an authenticated user session within the AT Protocol. It may also have logging
/// information, as well as the URL of the Personal Data Server (PDS).
///
/// - Parameter accessToken: The access token used for the session. Optional.
/// Defaults to `nil`.
///
/// - Returns: Information of the user account's current session straight from the service
/// (if there is one) or `nil` (if there isn't one).
/// - Throws: An ``ATProtoError``-conforming error type, depending on the issue. Go to
/// ``ATAPIError`` and ``ATRequestPrepareError`` for more details.
public func getSession() async throws -> ComAtprotoLexicon.Server.GetSessionOutput? {
public func getSession(by accessToken: String? = nil) async throws -> ComAtprotoLexicon.Server.GetSessionOutput? {
do {
guard let session = self.session?.accessToken else { return nil }
// Check if accessToken has anything inserted. If not, use the accessToken from the UserSession object.
guard let sessionToken = accessToken ?? self.session?.accessToken else { return nil }
guard sessionToken == sessionToken else { return nil }

let response = try await ATProtoKit().getSession(
by: session,
by: sessionToken,
pdsURL: self.pdsURL
)

Expand All @@ -331,16 +336,24 @@ public class ATProtocolConfiguration: SessionConfiguration {
/// new instance of an authenticated user session within the AT Protocol. It may also have
/// logging information, as well as the URL of the Personal Data Server (PDS).
///
/// - Parameter refreshToken: The refresh token used for the session. Optional.
/// Defaults to `nil`.
///
/// - Returns: Information of the user account's new session.
/// - Throws: An ``ATProtoError``-conforming error type, depending on the issue. Go to
/// ``ATAPIError`` and ``ATRequestPrepareError`` for more details.
public func refreshSession() async throws {
public func refreshSession(by refreshToken: String? = nil) async throws -> ComAtprotoLexicon.Server.RefreshSessionOutput? {
do {
guard let refreshToken = self.session?.refreshToken else { return }
// Check if accessToken has anything inserted. If not, use the accessToken from the UserSession object.
guard let sessionToken = refreshToken ?? self.session?.refreshToken else { return nil }
guard sessionToken == sessionToken else { return nil }

_ = try await ATProtoKit().refreshSession(
refreshToken: refreshToken,
let response = try await ATProtoKit().refreshSession(
refreshToken: sessionToken,
pdsURL: self.pdsURL
)

return response
} catch {
throw error
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/ATProtoKit/ATProtoKit.docc/Misc/0210AuthFlowChange.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ Here are the changes being made:
`ATProtoKit` method|Proxy Method
---:|:---
``ATProtoKit/ATProtoKit/createSession(with:and:authenticationFactorToken:pdsURL:)``|``ATProtocolConfiguration/authenticate(authenticationFactorToken:)``
``ATProtoKit/ATProtoKit/getSession(by:pdsURL:)``|``ATProtocolConfiguration/getSession()
``ATProtoKit/ATProtoKit/refreshSession(refreshToken:pdsURL:)``| ``ATProtocolConfiguration/refreshSession()``
``ATProtoKit/ATProtoKit/getSession(by:pdsURL:)``|``ATProtocolConfiguration/getSession(by:)``
``ATProtoKit/ATProtoKit/refreshSession(refreshToken:pdsURL:)``| ``ATProtocolConfiguration/refreshSession(by:)``
``ATProtoKit/ATProtoKit/deleteSession(refreshToken:pdsURL:)``|``ATProtocolConfiguration/deleteSession()``
``ATProtoKit/ATProtoKit/createAccount(email:handle:existingDID:inviteCode:verificationCode:verificationPhone:password:recoveryKey:plcOperation:pdsURL:)``| ``ATProtocolConfiguration/createAccount(email:handle:existingDID:inviteCode:verificationCode:verificationPhone:password:recoveryKey:plcOperation:)``

Expand Down

0 comments on commit 3a700fd

Please sign in to comment.