I'm supporting the guest access of the app so later on the user can create the account if wanted. I've enabled unauthenticated logins doing amplify update auth
. If I were to login an online registered user I would do:
func signIn(username: String, password: String) {
Amplify.Auth.signIn(username: username, password: password) { result in
switch result {
case .success:
print("Sign in succeeded")
case .failure(let error):
print("Sign in failed \(error)")
}
}
}
But how do I trigger the guest event (loggin in locally as guest) and then get it's unique id assigned to the device?
I found that I can get an id of the device like this:
Amplify.Auth.fetchAuthSession().id
However once I read that value multiple times I see different ids, is there a way to get a single unique id of the current device?
I ended up using this:
// Gets signed in User
func getUser() async -> AuthUser {
async let user = Amplify.Auth.getCurrentUser()
return await user!
}
Of course you have to check for session first because I'm unwrapping it above and if it's nil your app will crash. After the user is returned you can do stuff like user.id or something of that kind 🤓
NOTE: I'm using the async
function introduced in Swift 5.5, if you don't have it yet, just remove async/await from it