Created
December 7, 2018 22:55
-
-
Save dmennis/a08f3d5c1441002e3dbfcdb5b41c3bff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// AuthLWA | |
// | |
// Created by Hills, Dennis on 12/7/18. | |
// Copyright © 2018 Hills, Dennis. All rights reserved. | |
// | |
// Requires LoginWithAmazonProxy via Gist here: https://gist.github.com/mobilequickie/56916503a41ebb2374fea241ede26eab | |
// This gist: https://gist.github.com/mobilequickie/47a238e073043a271425f7ffe9d56d5e | |
// | |
import UIKit | |
import LoginWithAmazon | |
import AWSMobileClient | |
class ViewController: UIViewController, AIAuthenticationDelegate { | |
@IBOutlet weak var lblIdentityId: UILabel! | |
@IBOutlet weak var btnLWALogin: UIButton! | |
@IBOutlet weak var btnLWALogout: UIButton! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
initializeAWSMobileClient() // Initialize the AWSMobileClient | |
lblIdentityId.text = AWSMobileClient.sharedInstance().identityId | |
} | |
// Initializing the AWSMobileClient and take action based on current user state | |
func initializeAWSMobileClient() { | |
AWSMobileClient.sharedInstance().initialize { (userState, error) in | |
self.addUserStateListener() // Register for user state changes | |
if let userState = userState | |
switch(userState){ | |
case .signedIn: // is Signed IN | |
print("Logged In") | |
print("Cognito Identity Id (authenticated): \(AWSMobileClient.sharedInstance().identityId))") | |
DispatchQueue.main.async { | |
self.btnLWALogin.isEnabled = false | |
self.btnLWALogout.isEnabled = true | |
} | |
case .signedOut: // is Signed OUT | |
print("Logged Out") | |
DispatchQueue.main.async { | |
self.btnLWALogin.isEnabled = true | |
self.btnLWALogout.isEnabled = false | |
} | |
case .signedOutUserPoolsTokenInvalid: // User Pools refresh token INVALID | |
print("User Pools refresh token is invalid or expired.") | |
DispatchQueue.main.async { | |
//self.showSignIn() // If implemented drop-in auth UI for User Pools call that UI here. | |
} | |
case .signedOutFederatedTokensInvalid: // Login with Amazon, Facebook, or Google refresh token is INVALID | |
print("Federated refresh token is invalid or expired.") | |
DispatchQueue.main.async { | |
self.btnLWALogin.isEnabled = true | |
self.btnLWALogout.isEnabled = false | |
} | |
default: | |
AWSMobileClient.sharedInstance().signOut() | |
} | |
} else if let error = error { | |
print(error.localizedDescription) | |
} | |
} | |
} | |
func addUserStateListener() { | |
AWSMobileClient.sharedInstance().addUserStateListener(self) { (userState, info) in | |
switch (userState) { | |
case .guest: | |
print("user is not signed in (unauthenticated)") | |
DispatchQueue.main.async(execute: { () -> Void in | |
self.lblIdentityId.text = AWSMobileClient.sharedInstance().identityId | |
}) | |
case .signedIn: | |
DispatchQueue.main.async(execute: { () -> Void in | |
self.btnLWALogin.isEnabled = false | |
self.btnLWALogout.isEnabled = true | |
}) | |
print("user is signed in (authenticated)") | |
DispatchQueue.main.async(execute: { () -> Void in | |
self.lblIdentityId.text = AWSMobileClient.sharedInstance().identityId | |
}) | |
case .signedOut: | |
print("user signed out") | |
DispatchQueue.main.async(execute: { () -> Void in | |
self.btnLWALogin.isEnabled = true | |
self.btnLWALogout.isEnabled = false | |
}) | |
DispatchQueue.main.async(execute: { () -> Void in | |
self.lblIdentityId.text = AWSMobileClient.sharedInstance().identityId | |
}) | |
case .signedOutUserPoolsTokenInvalid: | |
print("need to login again.") // Show drop-in auth UI for Cognito User Pools if you have basic auth enabled | |
case .signedOutFederatedTokensInvalid: // Login with Amazon refresh token has expired. Re-authenticate | |
LoginWithAmazonProxy.sharedInstance.login(delegate: self) | |
default: | |
print("unsupported") | |
} | |
} | |
} | |
// User taps [Login with Amazon] button | |
@IBAction func onClickLWALogin(_ sender: Any) { | |
LoginWithAmazonProxy.sharedInstance.login(delegate: self) | |
} | |
// User taps [Logout] | |
@IBAction func onClickLWALogout(_ sender: Any) { | |
LoginWithAmazonProxy.sharedInstance.logout(delegate: self) | |
} | |
func AMZNSignOutRequestHandler(_ apiResult: APIResult!) { | |
print("Sign out callback") | |
} | |
// Login with Amazon - Successful login callback | |
func requestDidSucceed(_ apiResult: APIResult!) { | |
switch (apiResult.api) { | |
case API.authorizeUser: | |
LoginWithAmazonProxy.sharedInstance.getAccessToken(delegate: self) | |
case API.getAccessToken: | |
print("LWA Access Token: \(apiResult.result)") | |
guard let LWAtoken = apiResult.result as? String else { return } | |
// Get the user profile from LWA | |
LoginWithAmazonProxy.sharedInstance.getUserProfile(delegate: self) | |
// To federate Login with Amazon (LWA) as a sign-in provider, pass tokens to AWSMobileClient.sharedInstance().federatedSignIn() | |
AWSMobileClient.sharedInstance().federatedSignIn(providerName: IdentityProvider.amazon.rawValue, token: LWAtoken ) { (userState,err) in | |
if let error = err { | |
print("Federated SignIn failed for LWA: \(error.localizedDescription)") | |
} | |
} | |
case API.getProfile: | |
print("LWA User Profile: \(apiResult.result)") | |
case API.clearAuthorizationState: | |
print("user logged out from LWA") | |
// Sign out from AWSMobileClient | |
AWSMobileClient.sharedInstance().signOut() | |
default: | |
print("unsupported") | |
} | |
} | |
// Login with Amazon - callback error | |
func requestDidFail(_ errorResponse: APIError!) { | |
print("Error: \(errorResponse.error.message ?? "nil")") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment