I’m utilizing AWS Cognito for authentication in my iOS app with Swift. Under is the loginCognito
operate I take advantage of to authenticate customers:
non-public func loginCognito(knowledge: AWSConfigData) async throws {
let loginsKey = "cognito-idp.(knowledge.area).amazonaws.com/(knowledge.userPoolId)"
let logins = [loginsKey: data.idToken]
let identityProviderManager = IdentityProviderManager(loginMaps: logins)
let credentialsProvider = AWSCognitoCredentialsProvider(
regionType: knowledge.regionType,
identityPoolId: knowledge.identityPoolId,
identityProviderManager: identityProviderManager
)
return attempt await withCheckedThrowingContinuation { continuation in
credentialsProvider.identityProvider.logins().continueWith { job in
if let end result = job.end result, let token = end result[loginsKey] as? String {
print("Login with Token:", token)
self.awsConfigData = knowledge
let configuration = AWSServiceConfiguration(
area: knowledge.regionType,
credentialsProvider: credentialsProvider
)
AWSServiceManager.default()?.defaultServiceConfiguration = configuration
Job {
let currentToken = attempt await self.getLoginsToken()
print("Present Token:", currentToken)
}
return continuation.resume(returning: ())
} else if let error = job.error {
return continuation.resume(throwing: error)
} else {
let error = NSError(area: "S3Error", code: -1, userInfo: [NSLocalizedDescriptionKey: "Get login error"])
return continuation.resume(throwing: error)
}
}
}
}
/// IdentityProviderManager for AWS Cognito
non-public class IdentityProviderManager: NSObject, AWSIdentityProviderManager {
non-public let loginMaps: [String: String]
init(loginMaps: [String: String]) {
self.loginMaps = loginMaps
}
func logins() -> AWSTask {
return AWSTask(end result: loginMaps as NSDictionary)
}
}
That is my getLoginsToken
operate to retrieve the present token:
non-public func getLoginsToken() async throws -> String {
guard let configuration = AWSServiceManager.default().defaultServiceConfiguration,
let credentialsProvider = configuration.credentialsProvider as? AWSCognitoCredentialsProvider,
let awsConfigData else {
let error = NSError(area: "S3Error", code: -1, userInfo: [NSLocalizedDescriptionKey: "Cannot get credentialsProvider"])
print("Token Error:", error)
throw error
}
return attempt await withCheckedThrowingContinuation { continuation in
credentialsProvider.identityProvider.logins().continueWith { job in
let loginsKey = "cognito-idp.(awsConfigData.area).amazonaws.com/(awsConfigData.userPoolId)"
if let end result = job.end result, let token = end result[loginsKey] as? String {
print("Token:", token)
return continuation.resume(returning: token)
} else if let error = job.error {
print("Token Error:", error)
return continuation.resume(throwing: error)
} else {
let error = NSError(area: "S3Error", code: -1, userInfo: [NSLocalizedDescriptionKey: "Get login error"])
return continuation.resume(throwing: error)
}
}
}
}
Challenge:
- On the primary login,
Login with Token
andPresent Token
are the identical. - Nevertheless, from the second login onwards,
Login with Token
is up to date, howeverPresent Token
stays the identical as the primary login. - I’ve tried utilizing
credentialsProvider.clearCredentials()
, but it surely didn’t clear up the problem.
How can I be sure that getLoginsToken()
at all times retrieves the newest token from Cognito?
Any assist could be enormously appreciated! Thanks upfront! 🙏