Skip to content

Commit

Permalink
feat(auth): Add magic link API public definitions (#3369)
Browse files Browse the repository at this point in the history
* Add magic link API public definitions

* Address review comments

* Address review comments
  • Loading branch information
thisisabhash authored Nov 20, 2023
1 parent 7b507f3 commit 4d940a6
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 1 deletion.
19 changes: 18 additions & 1 deletion Amplify/Categories/Auth/AuthCategory+ClientBehavior.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,22 @@ extension AuthCategory: AuthCategoryBehavior {
) async throws {
try await plugin.verifyTOTPSetup(code: code, options: options)
}


// MARK: - Passwordless Auth

public func signInWithMagicLink(
username: String,
flow: AuthPasswordlessFlow,
redirectURL: String,
options: AuthSignInRequest.Options?
) async throws -> AuthSignInResult {
try await plugin.signInWithMagicLink(username: username, flow: flow, redirectURL: redirectURL, options: options)
}

public func confirmSignInWithMagicLink(
challengeResponse: String,
options: AuthConfirmSignInRequest.Options?
) async throws -> AuthSignInResult {
try await plugin.confirmSignInWithMagicLink(challengeResponse: challengeResponse, options: options)
}
}
30 changes: 30 additions & 0 deletions Amplify/Categories/Auth/AuthCategoryBehavior.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,34 @@ public protocol AuthCategoryBehavior: AuthCategoryUserBehavior, AuthCategoryDevi
options: VerifyTOTPSetupRequest.Options?
) async throws

// MARK: - Passwordless Auth

/// Initiates Sign in with Magiclink
///
/// Invoke this operation to start sign in with Magic Link flow
///
/// - Parameters:
/// - username: username used for sign in
/// - flow: `AuthPasswordlessFlow` type - can be `.signUpAndSignIn` or `.signIn`
/// - redirectURL: URL format to be used for the Magic Link
/// - options: Parameters specific to plugin behavior
func signInWithMagicLink(
username: String,
flow: AuthPasswordlessFlow,
redirectURL: String,
options: AuthSignInRequest.Options?
) async throws -> AuthSignInResult

/// Confirms Sign in with Magiclink flow
///
/// Invoke this operation to confirm sign in with Magic Link flow and sign in the user
///
/// - Parameters:
/// - challengeResponse: challengeResponse contained in the Magic Link received by the user
/// - options: Parameters specific to plugin behavior
func confirmSignInWithMagicLink(
challengeResponse: String,
options: AuthConfirmSignInRequest.Options?
) async throws -> AuthSignInResult

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Foundation

/// Delivery destination for the Auth Passwordless flows
///
public enum AuthPasswordlessDeliveryDestination {
case phone
case email
}
20 changes: 20 additions & 0 deletions Amplify/Categories/Auth/Models/AuthPasswordlessFlow.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Foundation

/// Auth Passwordless flow types
///
/// `.signUpAndSignIn` will create a new user and initiate signing in the user
/// `.signIn` will initiate signing in an existing user
public enum AuthPasswordlessFlow {
case signUpAndSignIn(
userAttributes: [String: String]?,
clientMetadata: [String: String]?
)
case signIn(clientMetadata: [String: String]?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,20 @@ extension AWSCognitoAuthPlugin: AuthCategoryBehavior {
return try await task.value
}
}

public func signInWithMagicLink(
username: String,
flow: AuthPasswordlessFlow,
redirectURL: String,
options: AuthSignInRequest.Options?
) async throws -> AuthSignInResult {
throw AuthError.unknown("Not Implemented")
}

public func confirmSignInWithMagicLink(
challengeResponse: String,
options: AuthConfirmSignInRequest.Options?
) async throws -> AuthSignInResult {
throw AuthError.unknown("Not Implemented")
}
}
8 changes: 8 additions & 0 deletions AmplifyTestCommon/Mocks/MockAuthCategoryPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ class MockAuthCategoryPlugin: MessageReporter, AuthCategoryPlugin {
) async throws {
fatalError()
}

func signInWithMagicLink(username: String, flow: AuthPasswordlessFlow, redirectURL: String, options: AuthSignInRequest.Options?) async throws -> AuthSignInResult {
fatalError()
}

func confirmSignInWithMagicLink(challengeResponse: String, options: AuthConfirmSignInRequest.Options?) async throws -> AuthSignInResult {
fatalError()
}

public func confirm(userAttribute: AuthUserAttributeKey,
confirmationCode: String,
Expand Down

0 comments on commit 4d940a6

Please sign in to comment.