AWS Amplify Swift API 登录 iOS 移动应用程序
Posted
技术标签:
【中文标题】AWS Amplify Swift API 登录 iOS 移动应用程序【英文标题】:AWS Amplify Swift API login iOS Mobile App 【发布时间】:2020-05-04 08:43:14 【问题描述】:我正在关注docs通过API实现登录,
class AWSUserPool
var userAuthenticationError: Error?
AWSMobileClient.default().signIn(username: <param username>, password: <param password>) (signInResult, error) in
if let error = error
print("\(error.localizedDescription)")
self.userAuthenticationError = error
else if let signInResult = signInResult
switch (signInResult.signInState)
case .signedIn:
print("User is signed in.")
case .smsMFA:
print("SMS message sent to \(signInResult.codeDetails!.destination!)")
default:
print("Sign In needs info which is not yet supported.")
我已经实例化了该类,并且在提供了正确的凭据后登录工作,我想向警报显示错误消息,但我可以打印可选的错误(未找到用户....)但我我无法将其分配给类变量。
import RxSwift
let observable: Observable<Error?> = Observable<Error?>.just(self.awsUserPool?.userAuthenticationError)
let disposeBag = DisposeBag()
observable.subscribe event in
print(event)
.disposed(by: disposeBag)
【问题讨论】:
【参考方案1】:我会首先围绕AWSMobileClient
编写一个反应式包装器,如下所示:
extension Reactive where Base: AWSMobileClient
func signIn(username: String, password: String) -> Observable<SignInResult>
return Observable.create observer in
self.base.signIn(username: username, password: password) (signinResult, error) in
if let signinResult = signinResult
observer.onNext(signinResult)
observer.onCompleted()
else
observer.onError(error ?? RxError.unknown)
return Disposables.create()
然后将错误放入 Observable 中,请执行以下操作:
class AWSUserPool
let userAuthenticationError: Observable<Error>
private let disposeBag = DisposeBag()
init(username: String, password: String)
let result = AWSMobileClient.default().rx.signIn(username: username, password: password)
.materialize()
result
.compactMap $0.element
.subscribe(onNext: signinResult in
switch signinResult.signInState
case .signedIn:
print("User is signed in.")
case .smsMFA:
print("SMS message sent to \(signinResult.codeDetails!.destination!)")
default:
print("Sign In needs info which is not yet supported.")
)
.disposed(by: disposeBag)
userAuthenticationError = result.compactMap $0.error
不过,我可能不会打扰 AWSUserPool……这取决于你。
【讨论】:
以上是关于AWS Amplify Swift API 登录 iOS 移动应用程序的主要内容,如果未能解决你的问题,请参考以下文章
如果我有登录用户的访问令牌,如何使用 AWS Amplify GraphQL API?
模拟适用于 Android 的 AWS Amplify Auth API
什么是用于在 Cognito 中登录的 AWS 开发工具包库(从后端不使用 Amplify)?
将Cognito的用户信息与AWS Amplify GraphQL关联起来。
将 Cognito 中的用户信息与 AWS Amplify GraphQL 关联
AWS Amplify SPA React + Cognito(已启用 Microsoft Azure Ad Enterprise SSO)+ Microsoft Graph API