Skip to content

Commit 59f6b18

Browse files
authored
feat(Auth): Implementation of getCurrentUser api (#455)
1 parent 43e3a70 commit 59f6b18

10 files changed

+56
-41
lines changed

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import AWSMobileClient
1010

1111
/// Auth plugin that uses AWS Cognito UserPool and IdentityPool.
1212
///
13-
/// The implicitly unwrapped optionals in this class are assigned in the `configure` method in `AWSCognitoAuthPlugin+Configure`
14-
/// extension. Make sure to call `Amplify.configure` after adding the plugin to `Amplify`.
13+
/// The implicitly unwrapped optionals in this class are assigned in the `configure` method in
14+
/// `AWSCognitoAuthPlugin+Configure` extension. Make sure to call `Amplify.configure` after adding the
15+
/// plugin to `Amplify`.
1516
final public class AWSCognitoAuthPlugin: AuthCategoryPlugin {
1617

1718
/// A queue that regulates the execution of operations.

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/ClientBehavior/AWSCognitoAuthPlugin+ClientBehavior.swift

-4
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,4 @@ extension AWSCognitoAuthPlugin {
161161
queue.addOperation(operation)
162162
return operation
163163
}
164-
165-
public func getCurrentUser() -> AuthUser? {
166-
return nil
167-
}
168164
}

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/ClientBehavior/AWSCognitoAuthPlugin+UserBehavior.swift

+4
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,8 @@ extension AWSCognitoAuthPlugin {
9090
queue.addOperation(operation)
9191
return operation
9292
}
93+
94+
public func getCurrentUser() -> AuthUser? {
95+
return authenticationProvider.getCurrentUser()
96+
}
9397
}

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Dependency/AuthenticationProviderAdapter.swift

+7-6
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ class AuthenticationProviderAdapter: AuthenticationProviderBehavior {
1616
self.awsMobileClient = awsMobileClient
1717
}
1818

19-
func signInUsername() -> Result<String, AuthError> {
19+
func getCurrentUser() -> AuthUser? {
2020

21-
if let username = awsMobileClient.username() {
22-
return .success(username)
21+
guard let username = awsMobileClient.getUsername() else {
22+
return nil
2323
}
24-
// TODO: Fix the error here
25-
return .failure(AuthError.unknown(""))
26-
24+
guard let sub = awsMobileClient.getUserSub() else {
25+
return nil
26+
}
27+
return AWSAuthUser(username: username, userId: sub)
2728
}
2829
}

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Dependency/AuthenticationProviderBehavior.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protocol AuthenticationProviderBehavior {
3131
func signOut(request: AuthSignOutRequest,
3232
completionHandler: @escaping (Result<Void, AuthError>) -> Void)
3333

34-
func signInUsername() -> Result<String, AuthError>
34+
func getCurrentUser() -> AuthUser?
3535

3636
func resetPassword(request: AuthResetPasswordRequest,
3737
completionHandler: @escaping (Result<AuthResetPasswordResult, AuthError>) -> Void)

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Models/AWSAuthUser.swift

+7
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@ import Amplify
99

1010
public struct AWSAuthUser: AuthUser {
1111

12+
/// The username for the logged in user
13+
///
14+
/// Value maps to the username of a user in AWS Cognito User Pool. This value is set by AWS Cognito and not by the
15+
/// user and does not always map with the username used to signIn.
1216
public var username: String
1317

18+
/// User Id for the logged in user
19+
///
20+
/// UserId value maps to the sub value of a user in AWS Cognito User Pool. This value will be unique for a user.
1421
public var userId: String
1522

1623
}

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Service/AWSMobileClient/AWSMobileClientAdapter.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,14 @@ class AWSMobileClientAdapter: AWSMobileClientBehavior {
107107
awsMobileClient.signOut()
108108
}
109109

110-
func username() -> String? {
110+
func getUsername() -> String? {
111111
return awsMobileClient.username
112112
}
113113

114+
func getUserSub() -> String? {
115+
return awsMobileClient.userSub
116+
}
117+
114118
func verifyUserAttribute(attributeName: String,
115119
completionHandler: @escaping ((UserCodeDeliveryDetails?, Error?) -> Void)) {
116120
awsMobileClient.verifyUserAttribute(attributeName: attributeName,

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/Service/AWSMobileClient/AWSMobileClientBehavior.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ protocol AWSMobileClientBehavior {
5252

5353
func signOutLocally()
5454

55-
func username() -> String?
55+
func getUsername() -> String?
56+
57+
func getUserSub() -> String?
5658

5759
func verifyUserAttribute(attributeName: String,
5860
completionHandler: @escaping ((UserCodeDeliveryDetails?, Error?) -> Void))

Podfile.lock

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
PODS:
2-
- AWSAuthCore (2.13.3):
3-
- AWSCore (= 2.13.3)
4-
- AWSCognitoIdentityProvider (2.13.3):
2+
- AWSAuthCore (2.13.4):
3+
- AWSCore (= 2.13.4)
4+
- AWSCognitoIdentityProvider (2.13.4):
55
- AWSCognitoIdentityProviderASF (= 1.0.1)
6-
- AWSCore (= 2.13.3)
6+
- AWSCore (= 2.13.4)
77
- AWSCognitoIdentityProviderASF (1.0.1)
8-
- AWSCore (2.13.3)
9-
- AWSMobileClient (2.13.3):
10-
- AWSAuthCore (= 2.13.3)
11-
- AWSCognitoIdentityProvider (= 2.13.3)
8+
- AWSCore (2.13.4)
9+
- AWSMobileClient (2.13.4):
10+
- AWSAuthCore (= 2.13.4)
11+
- AWSCognitoIdentityProvider (= 2.13.4)
1212
- CwlCatchException (1.0.2)
1313
- CwlPreconditionTesting (1.1.1):
1414
- CwlCatchException
@@ -24,7 +24,7 @@ DEPENDENCIES:
2424
- SwiftLint
2525

2626
SPEC REPOS:
27-
https://cdn.cocoapods.org/:
27+
trunk:
2828
- AWSAuthCore
2929
- AWSCognitoIdentityProvider
3030
- AWSCognitoIdentityProviderASF
@@ -50,11 +50,11 @@ CHECKOUT OPTIONS:
5050
:tag: 1.2.0
5151

5252
SPEC CHECKSUMS:
53-
AWSAuthCore: 93b71ac38596e75790015c7299c725bb09ae1d5f
54-
AWSCognitoIdentityProvider: 3480819d983e78eda7a43e141273756784be30cb
53+
AWSAuthCore: 72b4bd064c28985ea5345c62759632d511de342a
54+
AWSCognitoIdentityProvider: a2d1d2857316519c018c03ac8205cf78154db5c0
5555
AWSCognitoIdentityProviderASF: f94f1a502e72ef3d0a1de93e10bf7a79c8698118
56-
AWSCore: fc361166b595e3c0183c55987743b295646c39d2
57-
AWSMobileClient: 9149b664d1a89279bd49d36598c7afc38a63d7d1
56+
AWSCore: 06beea22b6bdbfe9f5ab5015c6901e8475c746a8
57+
AWSMobileClient: 33ecce524fbc011d543dae8306ca29a43ecb72e5
5858
CwlCatchException: 70a52ae44ea5d46db7bd385f801a94942420cd8c
5959
CwlPreconditionTesting: d33a4e4f285c0b885fddcae5dfedfbb34d4f3961
6060
SwiftFormat: b72e592ea0979aeee53f6052abff291181364933

Pods/Manifest.lock

+13-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)