From 79ed0885e22b9f7cd5e39d5a0a2f790e95e47fcc Mon Sep 17 00:00:00 2001 From: Asaf Shen Date: Tue, 5 Sep 2023 10:36:55 +0300 Subject: [PATCH] Replace password session (#43) * Replace password - return a session * build and readme * rename * small readme adjustments --- README.md | 46 +++++++++++++++++++++++++++ src/internal/http/DescopeClient.swift | 4 +-- src/internal/routes/Password.swift | 4 +-- src/sdk/Callbacks.swift | 3 +- src/sdk/Routes.swift | 3 +- 5 files changed, 54 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1158a98..01a9844 100644 --- a/README.md +++ b/README.md @@ -293,3 +293,49 @@ code the app produces. ```swift let authResponse = try await Descope.totp.verify(loginId: "andy@example.com", code: "987654") ``` + +### Password Authentication + +Authenticate users using a password. + +#### Sign Up with Password + +To create a new user that can later sign in with a password: + +```swift +let authResponse = try await Descope.password.signUp(loginId: "andy@example.com", password: "securePassword123!", details: SignUpDetails( + name: "Andy Rhoads" +)) +``` + +#### Sign In with Password + +Authenticate an existing user using a password: + +```swift +let authResponse = try await Descope.password.signIn(loginId: "andy@example.com", password: "securePassword123!") +``` + +#### Update Password + +If you need to update a user's password: + +```swift +try await Descope.password.update(loginId: "andy@example.com", newPassword: "newSecurePassword456!", refreshJwt: "user-refresh-jwt") +``` + +#### Replace Password + +To replace a user's password by providing their current password: + +```swift +let authResponse = try await Descope.password.replace(loginId: "andy@example.com", oldPassword: "SecurePassword123!", newPassword: "NewSecurePassword456!") +``` + +#### Send Password Reset Email + +Initiate a password reset by sending an email: + +```swift +try await Descope.password.sendReset(loginId: "andy@example.com", redirectURL: "exampleauthschema://my-app.com/handle-reset") +``` diff --git a/src/internal/http/DescopeClient.swift b/src/internal/http/DescopeClient.swift index cfc590f..b1358f8 100644 --- a/src/internal/http/DescopeClient.swift +++ b/src/internal/http/DescopeClient.swift @@ -108,8 +108,8 @@ class DescopeClient: HTTPClient { ]) } - func passwordReplace(loginId: String, oldPassword: String, newPassword: String) async throws { - try await post("auth/password/replace", body: [ + func passwordReplace(loginId: String, oldPassword: String, newPassword: String) async throws -> JWTResponse { + return try await post("auth/password/replace", body: [ "loginId": loginId, "oldPassword": oldPassword, "newPassword": newPassword, diff --git a/src/internal/routes/Password.swift b/src/internal/routes/Password.swift index f0a13c0..931b737 100644 --- a/src/internal/routes/Password.swift +++ b/src/internal/routes/Password.swift @@ -18,8 +18,8 @@ class Password: DescopePassword { try await client.passwordUpdate(loginId: loginId, newPassword: newPassword, refreshJwt: refreshJwt) } - func replace(loginId: String, oldPassword: String, newPassword: String) async throws { - try await client.passwordReplace(loginId: loginId, oldPassword: oldPassword, newPassword: newPassword) + func replace(loginId: String, oldPassword: String, newPassword: String) async throws -> AuthenticationResponse { + try await client.passwordReplace(loginId: loginId, oldPassword: oldPassword, newPassword: newPassword).convert() } func sendReset(loginId: String, redirectURL: String?) async throws { diff --git a/src/sdk/Callbacks.swift b/src/sdk/Callbacks.swift index dffb30d..4ddb94b 100644 --- a/src/sdk/Callbacks.swift +++ b/src/sdk/Callbacks.swift @@ -675,7 +675,8 @@ public extension DescopePassword { /// - loginId: The existing user's loginId. /// - oldPassword: The user's current password. /// - newPassword: The new password to set for the user. - func replace(loginId: String, oldPassword: String, newPassword: String, completion: @escaping (Result) -> Void) { + /// - Returns: An ``AuthenticationResponse`` value upon successful replacement and authentication. + func replace(loginId: String, oldPassword: String, newPassword: String, completion: @escaping (Result) -> Void) { Task { do { completion(.success(try await replace(loginId: loginId, oldPassword: oldPassword, newPassword: newPassword))) diff --git a/src/sdk/Routes.swift b/src/sdk/Routes.swift index 49eaa9b..d8c3230 100644 --- a/src/sdk/Routes.swift +++ b/src/sdk/Routes.swift @@ -518,7 +518,8 @@ public protocol DescopePassword { /// - loginId: The existing user's loginId. /// - oldPassword: The user's current password. /// - newPassword: The new password to set for the user. - func replace(loginId: String, oldPassword: String, newPassword: String) async throws + /// - Returns: An ``AuthenticationResponse`` value upon successful replacement and authentication. + func replace(loginId: String, oldPassword: String, newPassword: String) async throws -> AuthenticationResponse /// Sends a password reset email to the user. ///