Skip to content

Commit b5970c2

Browse files
authored
fix: Setting the right username value when using EMAIL or PHONE_NUMBER as usernameAttributes in Cognito (#20)
1 parent c9111c9 commit b5970c2

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

Sources/Authenticator/Configuration/AmplifyConfiguration.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ struct CognitoConfiguration {
120120
case username = "USERNAME"
121121
case email = "EMAIL"
122122
case phoneNumber = "PHONE_NUMBER"
123+
124+
init?(from authUserAttributeKey: AuthUserAttributeKey) {
125+
switch authUserAttributeKey {
126+
case .email:
127+
self = .email
128+
case .phoneNumber:
129+
self = .phoneNumber
130+
default:
131+
return nil
132+
}
133+
}
123134
}
124135

125136
enum SignUpAttribute: String, Decodable {

Sources/Authenticator/States/SignUpState.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import SwiftUI
1111

1212
/// The state observed by the Sign Up content view, representing the ``Authenticator`` is in the ``AuthenticatorStep/signUp`` step.
1313
public class SignUpState: AuthenticatorBaseState {
14-
/// The Sign Up ``Field``s that are be displayed
14+
/// The Sign Up ``Field``s that are displayed
1515
private(set) public var fields: [Field] = []
1616

1717
/// Attempts to confirm the new password using the provided values.
@@ -36,6 +36,10 @@ public class SignUpState: AuthenticatorBaseState {
3636
attributes.append(
3737
AuthUserAttribute(key, value: field.value)
3838
)
39+
// Check if the current AuthUserAttribute is defined to be the usernameAttribute in Cognito's config
40+
if configuration.usernameAttribute == CognitoConfiguration.UsernameAttribute(from: key) {
41+
username = field.value
42+
}
3943
}
4044
}
4145
}

Tests/AuthenticatorTests/Mocks/MockAuthenticationService.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@ class MockAuthenticationService: AuthenticationService {
6969
// MARK: - Sign Up
7070

7171
var signUpCount = 0
72+
var signUpParams: (username: String, password: String?)? = nil
7273
var mockedSignUpResult: AuthSignUpResult?
7374
func signUp(username: String, password: String?, options: AuthSignUpRequest.Options?) async throws -> AuthSignUpResult {
7475
signUpCount += 1
76+
signUpParams = (username, password)
77+
7578
if let mockedSignUpResult = mockedSignUpResult {
7679
return mockedSignUpResult
7780
}

Tests/AuthenticatorTests/States/SignUpStateTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,29 @@ class SignUpStateTests: XCTestCase {
6262
await task.value
6363
}
6464
}
65+
66+
func testSignUp_withEmailAsUsernameAttribute_shouldSetEmailAsUsername() async {
67+
authenticatorState.configuration.usernameAttributes = [.email]
68+
await state.configure(with: [.email()])
6569

70+
let emailField = await state.fields.first(where: {$0.field.attributeType == .email})
71+
emailField?.value = "email@email.com"
72+
73+
try? await state.signUp()
74+
XCTAssertEqual(authenticationService.signUpParams?.username, emailField?.value)
75+
}
76+
77+
func testSignUp_withPhoneNumberAsUsernameAttribute_shouldSetPhoneAsUsername() async {
78+
authenticatorState.configuration.usernameAttributes = [.phoneNumber]
79+
await state.configure(with: [.phoneNumber()])
80+
81+
let phoneNumberField = await state.fields.first(where: {$0.field.attributeType == .phoneNumber})
82+
phoneNumberField?.value = "+12345678910"
83+
84+
try? await state.signUp()
85+
XCTAssertEqual(authenticationService.signUpParams?.username, phoneNumberField?.value)
86+
}
87+
6688
func testConfigure_withFields_shouldPopulateFields_addingVerificationMechanism() {
6789
authenticatorState.configuration.verificationMechanisms = [
6890
.email,

0 commit comments

Comments
 (0)