Skip to content

Instantly share code, notes, and snippets.

@mobilequickie
Last active April 26, 2019 03:36
Show Gist options
  • Save mobilequickie/77c60e2204e759ed0a72bde0478c0885 to your computer and use it in GitHub Desktop.
Save mobilequickie/77c60e2204e759ed0a72bde0478c0885 to your computer and use it in GitHub Desktop.
Login with Amazon using AWSMobileClient integration sample main ViewController
//
// ViewController.swift
// AuthLWA
//
// Created by Hills, Dennis on 4/26/19.
// Copyright © 2018 Hills, Dennis. All rights reserved.
//
// Gist: https://gist.github.com/mobilequickie/77c60e2204e759ed0a72bde0478c0885
//
import UIKit
import LoginWithAmazon
import AWSMobileClient // #1
class ViewController: UIViewController, AIAuthenticationDelegate {
@IBOutlet weak var btnLWALogin: UIButton!
@IBOutlet weak var btnLWALogout: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
initializeAWSMobileClient() // #2 Initialize the AWSMobileClient
}
// #3 Initializing the AWSMobileClient and take action based on current user state
func initializeAWSMobileClient() {
AWSMobileClient.sharedInstance().initialize { (userState, error) in
if let userState = userState {
switch(userState){
case .signedIn: // is Signed IN
print("Logged In")
print("Cognito Identity Id (authenticated): \(String(describing: AWSMobileClient.sharedInstance().identityId))")
case .signedOut: // is Signed OUT
print("Logged Out")
print("Cognito Identity Id (unauthenticated): \(String(describing: AWSMobileClient.sharedInstance().identityId))")
case .signedOutUserPoolsTokenInvalid: // User Pools refresh token INVALID
print("User Pools refresh token is invalid or expired.")
case .signedOutFederatedTokensInvalid: // e.g. Facebook, Google, or Login with Amazon refresh token is INVALID
print("Federated refresh token is invalid or expired.")
default:
AWSMobileClient.sharedInstance().signOut()
}
} else if let error = error {
print(error.localizedDescription)
}
}
}
// Begin LWA integration
// User taps [Login with Amazon] button
@IBAction func onClickLWALogin(_ sender: Any) {
LoginWithAmazonProxy.sharedInstance.login(delegate: self)
}
// User taps [Logout]
@IBAction func onClickLWALogout(_ sender: Any) {
AMZNAuthorizationManager.shared().signOut { (error) in
if((error) != nil) {
print("error signing out: \(String(describing: error))")
} else {
print("Logout successfully!")
DispatchQueue.main.async(execute: { () -> Void in
self.btnLWALogin.isEnabled = true
self.btnLWALogout.isEnabled = false
})
}
}
}
// The login request succeeded and the user is now authenticated via LWA
func requestDidSucceed(_ apiResult: APIResult!) {
print("LWA Succeeded!")
DispatchQueue.main.async(execute: { () -> Void in
self.btnLWALogin.isEnabled = false
self.btnLWALogout.isEnabled = true
})
if (apiResult.api == API.authorizeUser) {
AIMobileLib.getAccessToken(forScopes: ["profile"], withOverrideParams: nil, delegate: self)
}
else {
print("Success! Token: \(apiResult.result ?? "nil")")
}
}
// The login request to LWA failed
func requestDidFail(_ errorResponse: APIError!) {
print("Error: \(errorResponse.error.message ?? "nil")")
}
// End LWA integration
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment