Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Oct 31, 2024

Enable sign-in with web UI

Prerequisites

Note: Social sign-in (OAuth) functionality is only available in iOS, macOS and visionOS.

When configuring social sign-in, it's important to exercise caution when designating attributes as "required." Different social identity providers have varied scopes in terms of the information they respond back to Cognito with. User pool attributes that are initially set up as "required" cannot be changed later, and may require you to migrate the users or create a new user pool.

For a full example, please follow the project setup walkthrough.

To use Auth in a macOS project, you'll need to enable the Keychain Sharing capability. In Xcode, navigate to your application target > Signing & Capabilities > + Capability, then select Keychain Sharing.

This capability is required because Auth uses the Data Protection Keychain on macOS as a platform best practice. See TN3137: macOS keychain APIs and implementations for more information on how Keychain works on macOS and the Keychain Sharing entitlement.

For more information on adding capabilities to your application, see Xcode Capabilities.

Configure Auth Category

This library's Cognito plugin currently supports the Authorization Code Grant OAuth Flow.

Update the auth/resource.ts file like the following to enable the sign-in and sign-out capabilities with a web ui.

export const auth = defineAuth({
loginWith: {
email: true,
externalProviders: {
callbackUrls: ["myapp://callback/"],
logoutUrls: ["myapp://signout/"],
},
},
});

Update Info.plist

Signin with web UI require the Amplify plugin to show up the signin UI inside a webview. After the signin process is complete it will redirect back to your app. You have to enable this in your app's Info.plist. Right click Info.plist and then choose Open As > Source Code. Add the following entry in the URL scheme:

<plist version="1.0">
<dict>
<!-- YOUR OTHER PLIST ENTRIES HERE -->
<!-- ADD AN ENTRY TO CFBundleURLTypes for Cognito Auth -->
<!-- IF YOU DO NOT HAVE CFBundleURLTypes, YOU CAN COPY THE WHOLE BLOCK BELOW -->
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>myapp</string>
</array>
</dict>
</array>
<!-- ... -->
</dict>

When creating a new SwiftUI app using Xcode 13 no longer require configuration files such as the Info.plist. If you are missing this file, click on the project target, under Info, Url Types, and click '+' to add a new URL Type. Add myapp to the URL Schemes. You should see the Info.plist file now with the entry for CFBundleURLSchemes.

Launch Web UI Sign In

You're now ready to launch sign in with web UI. The signInWithWebUI api require a presentationAnchor and for an iOS app it will be the main UIWindow of the app. The example code below assume that you are in a UIViewController where you can fetch the UIWindow instance by self.view.window.

func signInWithWebUI() async {
do {
let signInResult = try await Amplify.Auth.signInWithWebUI(presentationAnchor: self.view.window!)
if signInResult.isSignedIn {
print("Sign in succeeded")
}
} catch let error as AuthError {
print("Sign in failed \(error)")
} catch {
print("Unexpected error: \(error)")
}
}
func signInWithWebUI() -> AnyCancellable {
Amplify.Publisher.create {
try await Amplify.Auth.signInWithWebUI(presentationAnchor: self.view.window!)
}.sink {
if case let .failure(authError) = $0 {
print("Sign in failed \(authError)")
}
}
receiveValue: { signInResult in
if signInResult.isSignedIn {
print("Sign in succeeded")
}
}
}

Prefer private session during signIn

Starting Amplify 1.6.0, Amplify.Auth.signInWithWebUI automatically uses ASWebAuthenticationSession internally for iOS 13.0+. For older iOS versions, it will fall back to SFAuthenticationSession. This release also introduces a new preferPrivateSession flag to AWSAuthWebUISignInOptions during the sign in flow. If preferPrivateSession is set to true during sign in, the user will not see a web view displayed when they sign out. preferPrivateSession will set ASWebAuthenticationSession.prefersEphemeralWebBrowserSession internally and the authentication session will be private if the user's preferred browser supports it.

try await Amplify.Auth.signInWithWebUI(
presentationAnchor: self.view.window!,
options: .preferPrivateSession()
) {
...
}