Ios AWSMobileHubHelper将用户池添加为AWSIdentityProvider,如何处理“func登录(completionHandler:(AnyObject,NSError)->;Void)`?
我想将AWS用户池作为AWSIdentityProvider添加到我的iOS应用程序中 但是对于AWSMobileHubHelper,没有什么指导 所以我自己也试过了 在我的Ios AWSMobileHubHelper将用户池添加为AWSIdentityProvider,如何处理“func登录(completionHandler:(AnyObject,NSError)->;Void)`?,ios,swift,amazon-web-services,Ios,Swift,Amazon Web Services,我想将AWS用户池作为AWSIdentityProvider添加到我的iOS应用程序中 但是对于AWSMobileHubHelper,没有什么指导 所以我自己也试过了 在我的LoginViewController中,我将AWS用户池视为其他提供程序,如下所示: final class LoginViewController: UIViewController { @IBOutlet weak var usernameTextField: UITextField! @IBOutl
LoginViewController
中,我将AWS用户池视为其他提供程序,如下所示:
final
class LoginViewController: UIViewController {
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var loginButton: UIButton!
var passwordAuthenticationCompletion: AWSTaskCompletionSource!
var completionHandler: ((AnyObject, NSError) -> Void)?
override func viewDidLoad() {
super.viewDidLoad()
AWSFacebookSignInProvider.sharedInstance().setPermissions(["public_profile"]);
AWSGoogleSignInProvider.sharedInstance().setScopes(["profile", "openid"])
AWSGoogleSignInProvider.sharedInstance().setViewControllerForGoogleSignIn(self)
}
@IBAction private func facebookLogin() {
handleLogin(signInProvider: AWSFacebookSignInProvider.sharedInstance())
}
@IBAction private func googleLogin() {
handleLogin(signInProvider: AWSGoogleSignInProvider.sharedInstance())
}
@IBAction private func myLogin() {
handleLogin(signInProvider: LoginProvider.sharedInstance())
}
private func handleLogin(signInProvider signInProvider: AWSSignInProvider) {
title = "Loging ..."
AWSIdentityManager.defaultIdentityManager().loginWithSignInProvider(signInProvider) { (result, error) in
switch error {
case let error? where error.domain != "success":
print("Login failed.")
default:
print("Login succeed.")
}
}
}
}
我的LoginProvider
code:
final
class LoginProvider: NSObject {
static func sharedInstance() -> LoginProvider {
return _sharedInstance
}
static private let _sharedInstance = LoginProvider()
lazy var pool: AWSCognitoIdentityUserPool = {
let serviceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil)
let poolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: "clientId", clientSecret: "clientSecret", poolId: "poolId")
AWSCognitoIdentityUserPool.registerCognitoIdentityUserPoolWithConfiguration(serviceConfiguration, userPoolConfiguration: poolConfiguration, forKey: "UserPool")
let result = AWSCognitoIdentityUserPool(forKey: "UserPool")
result.delegate = self
return result
}()
}
extension LoginProvider: AWSCognitoIdentityInteractiveAuthenticationDelegate {
func startPasswordAuthentication() -> AWSCognitoIdentityPasswordAuthentication {
return loginViewController
}
func startMultiFactorAuthentication() -> AWSCognitoIdentityMultiFactorAuthentication {
fatalError("Identity MultiFactor Authentication Not Supportted!")
}
private var loginViewController: LoginViewController {
return LoginViewController.sharedInstance
}
}
extension LoginProvider: AWSIdentityProvider {
var identityProviderName: String {
return pool.identityProviderName
}
func token() -> AWSTask {
return pool.token()
}
}
extension LoginProvider: AWSSignInProvider {
var loggedIn: Bool {
@objc(isLoggedIn) get {
return currentUser?.signedIn ?? false
}
}
var imageURL: NSURL? {
return nil
}
var userName: String? {
return currentUser?.username
}
func login(completionHandler: (AnyObject, NSError) -> Void) {
loginViewController.completionHandler = completionHandler
loginViewController.doLogin()
}
func logout() {
currentUser?.signOut()
}
func reloadSession() {
currentUser?.getSession()
}
func interceptApplication(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
return true
}
func interceptApplication(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return false
}
private var currentUser: AWSCognitoIdentityUser? {
return pool.currentUser()
}
}
我已扩展了我的LoginViewController
,以支持该过程:
extension LoginViewController: AWSCognitoIdentityPasswordAuthentication {
func doLogin() {
pool.getUser(usernameTextField.text!).getSession(usernameTextField.text!, password: passwordTextField.text!, validationData: nil).continueWithBlock { task -> AnyObject? in
print("pool.getUser().getSession")
print("task.result:", task.result)
print("task.error:", task.error)
if let session = task.result as? AWSCognitoIdentityUserSession {
print("session.idToken:", session.idToken?.tokenString)
print("session.accessToken:", session.accessToken?.tokenString)
print("session.refreshToken:", session.refreshToken?.tokenString)
print("session.expirationTime:", session.expirationTime)
}
switch (task.result, task.error) {
case let (_, error?):
self.completionHandler?("", error)
self.completionHandler = nil
case let (result?, _):
self.completionHandler?(result, NSError(domain: "success", code: -1, userInfo: nil))
self.completionHandler = nil
default: break
}
return nil
}
}
func getPasswordAuthenticationDetails(authenticationInput: AWSCognitoIdentityPasswordAuthenticationInput, passwordAuthenticationCompletionSource: AWSTaskCompletionSource) {
self.passwordAuthenticationCompletion = passwordAuthenticationCompletionSource
}
func didCompletePasswordAuthenticationStepWithError(error: NSError?) {
switch error {
case let error?:
Queue.Main.execute {
self.completionHandler?("", error)
self.completionHandler = nil
}
default: break
}
}
private var pool: AWSCognitoIdentityUserPool! {
return LoginProvider.sharedInstance().pool
}
}
extension LoginViewController {
@nonobjc static let navigationController = UIStoryboard(name: "Login", bundle: nil).instantiateInitialViewController() as! UINavigationController
static var sharedInstance: LoginViewController {
return navigationController.viewControllers[0] as! LoginViewController
}
}
现在我可以登录到我的用户池,但我不能将其与联邦id池集成,它认为我在使用completionHandler
时给出了错误的结果
在
LoginViewController
的func-doLogin()
中,当我得到getSession()
的结果,即AWSCognitoIdentityUserSession
时,我如何将其转换为其他需要的completionHandler
结果?嗨,目前移动中心助手框架不支持使用用户池登录。您必须直接与Cognito进行集成。谢谢。好的,我会试着用Cognito,谢谢。我也有类似的问题。我可以顺利通过,甚至可以从cognito那里得到一个唯一的id,但我没有得到任何角色。。我认为我们需要亚马逊的一个例子来说明如何整合AWS Cognito用户池/联合身份和移动中心助手最好的方法是移动中心整合所有这些东西,然后为我们生成工作代码,但目前这只是一个功能要求。毕竟我找到了AWSMobileHubHelper的源代码,它为我提供了有用的信息,我改进了我的解决方案,但仍然存在一些问题。