Set up Amplify Auth
The Amplify Auth category provides an interface for authenticating a user. Behind the scenes, it provides the necessary authorization to the other Amplify categories. It comes with default, built-in support for Amazon Cognito User Pool and Identity Pool. The Amplify CLI helps you create and configure the auth category with an authentication provider.
Goal
To setup and configure your application with Amplify Auth and go through a simple api to check the current auth session.
Prerequisites
An application with Amplify libraries integrated and a minimum target of any of the following:
- iOS 13.0, using Xcode 14.1 or later.
- macOS 10.15, using Xcode 14.1 or later.
- tvOS 13.0, using Xcode 14.3 or later.
- watchOS 7.0, using Xcode 14.3 or later.
- visionOS 1.0, using Xcode 15 beta 2 or later. (Preview support - see below for more details.)
For a full example, please follow the project setup walkthrough.
Install Amplify Libraries
-
To install Amplify Libraries in your application, open your project in Xcode and select File > Add Packages....
-
Enter the Amplify Library for Swift GitHub repo URL (
https://github.com/aws-amplify/amplify-swift
) into the search bar and click Add Package.
- Lastly, choose AWSCognitoAuthPlugin and Amplify. Then click Add Package.
Set Up Backend Resources
The most common way to use Authentication with Amplify is via the Amplify CLI, which allows you to create new Amazon Cognito resources or import existing ones. However, you can also use the Amplify Studio console to configure authentication or use the Amplify.configure()
method to set up authentication with existing resources.
Prerequisites: Install and configure the Amplify CLI in addition to the Amplify libraries and necessary dependencies.
To start provisioning auth resources in the backend, go to your project directory and execute the command:
1amplify add auth
Enter the following when prompted:
1? Do you want to use the default authentication and security configuration?2 `Default configuration`3? How do you want users to be able to sign in?4 `Username`5? Do you want to configure advanced settings?6 `No, I am done.`
If you have previously enabled an Amplify category that uses Auth behind the scenes (e.g. API category), you can run the
amplify update auth
command to edit your configuration if needed.
To push your changes to the cloud, execute the command:
1amplify push
Upon completion, amplifyconfiguration.json
should be updated to reference provisioned backend auth resources. Note that these files should already be a part of your project if you followed the Project setup walkthrough.
Initialize Amplify Auth
To initialize the Amplify Auth category, pass in the AWSCognitoAuthPlugin to Amplify.add()
. When you are done calling add()
on each category that you need, you finish configuring Amplify by calling Amplify.configure()
.
Add the following imports:
1import Amplify2import AWSCognitoAuthPlugin
Configure Amplify at app launch
Configure Amplify in the App init
1@main2struct MyAmplifyApp: App {3
4 var body: some Scene {5 WindowGroup {6 ContentView()7 }8 }9
10 init() {11 do {12 try Amplify.add(plugin: AWSCognitoAuthPlugin())13 try Amplify.configure()14 print("Amplify configured with auth plugin")15 } catch {16 print("Failed to initialize Amplify with \(error)")17 }18 }19}
Upon building and running this application you should see the following in your console window:
1Amplify configured with auth plugin
Check the current auth session
You can now check the current auth session.
1func fetchCurrentAuthSession() async {2 do {3 let session = try await Amplify.Auth.fetchAuthSession() 4 print("Is user signed in - \(session.isSignedIn)")5 } catch let error as AuthError {6 print("Fetch session failed with error \(error)")7 } catch {8 print("Unexpected error: \(error)")9 }10}
The isSignedIn
property of the authSession
will be false since you haven't signed in to the category yet.
Authentication with Amplify
There are two ways to add authentication capabilities to your application.
Option 1: Use the Authenticator UI component
The Authenticator is a UI component that automatically integrates with your existing Amplify configuration and allows you to easily add the entire authentication flow to your application.
Visit Authenticator | Amplify UI for Swift to get started.
Option 2: Manually call the Authentication APIs
Follow the instructions in Sign In to learn about how to integrate the registration and authentication flows in your application with the Auth APIs.
Next Steps
Congratulations! You've successfully setup AWS Cognito Auth plugin. Check out the following links to see other Amplify Auth use cases: