What is the proper way to get the user info? This is the flow with Cognito Auth:
User signs in -> Lambda post confirmation creates a 1:1 user in Dynamo using email as unique identifier.
How do I get the Unique identifier of the current signed in user? I'm getting a weird id if I do: Amplify.Auth.getCurrentUser()
I get something like:
Optional(AmplifyPlugins.AWSAuthUser(username: "201cdc94-48cc-48a9-9393-9025f13b6fb3", userId: "201cdc94-48cc-48a9-9393-9025f13b6fb3"))
When I try:
Amplify.Auth.fetchUserAttributes() { result in
switch result {
case .success(let attributes):
print("User attributes - \(attributes)")
case .failure(let error):
print("Fetching user attributes failed with error \(error)")
}
}
I see the email there, how do I get that email? I tried attributes.email
and doesn't work
What is the correct way to get the Unique Identifier of the current user signed in?
EDIT: Below is a variant I came up with but I feel it's wrong, it can't be this hard... any ideas?
func fetchAttributes() -> String {
var userId:String = ""
Amplify.Auth.fetchUserAttributes() { result in
switch result {
case .success(let attributes):
for x in attributes {
if x.key.rawValue == "email" {
print(x.value)
userId = x.value
}
}
case .failure(let error):
print("Fetching user attributes failed with error \(error)")
}
}
return userId
}
As usual the Amplify Support is null... I ended up figuring it out on my own. So it seems Amplify doesn't provide the Unique Identifier for security reasons.
An intentional decision with Amplify Auth was to avoid any public methods exposing credentials or manipulating them.
So I decided to instead of creating the DynamoDB with the Unique Identifier to create it with the Sub Id which can be fetched like:
let usersub = try identityProvider.getUserSub().get()
More here