-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update sign in step to confirmSignInWithOTP
- Loading branch information
Showing
14 changed files
with
225 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
Sources/Authenticator/Views/ConfirmSignInWithOTPView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Amplify | ||
import SwiftUI | ||
|
||
/// Represents the content being displayed when the ``Authenticator`` is in the ``AuthenticatorStep/confirmSignInWithOTP`` step. | ||
public struct ConfirmSignInWithOTPView<Header: View, | ||
Footer: View>: View { | ||
@Environment(\.authenticatorState) private var authenticatorState | ||
@ObservedObject private var state: ConfirmSignInWithCodeState | ||
private let content: ConfirmSignInWithCodeView<Header, Footer> | ||
|
||
/// Creates a `ConfirmSignInWithMFACodeView` | ||
/// - Parameter state: The ``ConfirmSignInWithCodeState`` that is observed by this view | ||
/// - Parameter headerContent: The content displayed above the fields. Defaults to ``ConfirmSignInWithMFACodeHeader`` | ||
/// - Parameter footerContent: The content displayed bellow the fields. Defaults to ``ConfirmSignInWithMFACodeFooter`` | ||
public init( | ||
state: ConfirmSignInWithCodeState, | ||
@ViewBuilder headerContent: () -> Header = { | ||
ConfirmSignInWithOTPHeader() | ||
}, | ||
@ViewBuilder footerContent: () -> Footer = { | ||
ConfirmSignInWithOTPFooter() | ||
} | ||
) { | ||
self.state = state | ||
self.content = ConfirmSignInWithCodeView( | ||
state: state, | ||
headerContent: headerContent, | ||
footerContent: footerContent | ||
) | ||
} | ||
|
||
public var body: some View { | ||
content | ||
.onAppear { | ||
state.message = .info( | ||
message: state.localizedMessage(for: state.deliveryDetails) | ||
) | ||
} | ||
} | ||
|
||
/// Sets a custom error mapping function for the `AuthError`s that are displayed | ||
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed. | ||
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self { | ||
state.errorTransform = errorTransform | ||
return self | ||
} | ||
} | ||
|
||
/// Default header for the ``ConfirmSignInWithOTPView``. It displays the view's title | ||
public struct ConfirmSignInWithOTPHeader: View { | ||
public init() {} | ||
public var body: some View { | ||
DefaultHeader( | ||
title: "authenticator.confirmSignInWithOTP.title".localized() | ||
) | ||
} | ||
} | ||
|
||
/// Default footer for the ``ConfirmSignInWithOTPView``. It displays the "Back to Sign In" button | ||
public struct ConfirmSignInWithOTPFooter: View { | ||
@Environment(\.authenticatorState) private var authenticatorState | ||
|
||
public init() {} | ||
public var body: some View { | ||
Button("authenticator.confirmSignInWithCode.button.backToSignIn".localized()) { | ||
authenticatorState.move(to: .signIn) | ||
} | ||
.buttonStyle(.link) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,14 +83,22 @@ class ConfirmSignInWithCodeStateTests: XCTestCase { | |
} | ||
} | ||
|
||
func testDeliveryDetails_onConfirmSignInWithMFACodeStep_shouldReturnDetails() throws { | ||
func testDeliveryDetails_onConfirmSignInWithSMSMFACodeStep_shouldReturnDetails() throws { | ||
let destination = DeliveryDestination.sms("123456789") | ||
authenticatorState.mockedStep = .confirmSignInWithMFACode(deliveryDetails: .init(destination: destination)) | ||
|
||
let deliveryDetails = try XCTUnwrap(state.deliveryDetails) | ||
XCTAssertEqual(deliveryDetails.destination, destination) | ||
} | ||
|
||
func testDeliveryDetails_onConfirmSignInWithEmailMFACodeStep_shouldReturnDetails() throws { | ||
let destination = DeliveryDestination.email("[email protected]") | ||
authenticatorState.mockedStep = .confirmSignInWithMFACode(deliveryDetails: .init(destination: destination)) | ||
|
||
let deliveryDetails = try XCTUnwrap(state.deliveryDetails) | ||
XCTAssertEqual(deliveryDetails.destination, destination) | ||
} | ||
|
||
func testDeliveryDetails_onUnexpectedStep_shouldReturnNil() throws { | ||
let destination = DeliveryDestination.sms("123456789") | ||
authenticatorState.mockedStep = .confirmSignUp(deliveryDetails: .init(destination: destination)) | ||
|
Oops, something went wrong.