Auth0 allows you to quickly add authentication and access user profile information in your application. This guide demonstrates how to integrate Auth0 with an Ionic (Angular) & Capacitor application using the Auth0 Angular SDK.
To use Auth0 services, you need to have an application set up in the Auth0 Dashboard. The Auth0 application is where you will configure how you want authentication to work for your project.
Use the interactive selector to create a new Auth0 application or select an existing application that represents the project you want to integrate with. Every application in Auth0 is assigned an alphanumeric, unique client ID that your application code will use to call Auth0 APIs through the SDK.
Any settings you configure using this quickstart will automatically update for your application in the Dashboard, which is where you can manage your applications in the future.
If you would rather explore a complete configuration, you can view a sample application instead.
A callback URL is a URL in your application that you would like Auth0 to redirect users to after they have authenticated. If not set, users will not be returned to your application after they log in.
A logout URL is a URL in your application that you would like Auth0 to redirect users to after they have logged out. If not set, users will not be able to log out from your application and will receive an error.
To be able to make requests from your native application to Auth0, set the following Allowed Origins in your Application Settings.
Lastly, be sure that the Application Type for your application is set to Native in the Application Settings.
Run the following command within your project directory to install the Auth0 Angular SDK:
npm install @auth0/auth0-angular
Was this helpful?
The SDK exposes several types that help you integrate Auth0 with your Angular application idiomatically, including a module and an authentication service.
This quickstart and sample make use of some of Capacitor's official plugins. Install these into your app using the following command:
npm install @capacitor/browser @capacitor/app
Was this helpful?
@capacitor/browser- allows you to interact with the device's system browser and is used to open the URL to Auth0's authorizaction endpoint
@capacitor/app- allows you to subscribe to high-level app events, useful for handling callbacks from Auth0
The SDK exports
AuthModule, a module that contains all the services required for the SDK to function. This module should be registered with your application and be configured with your Auth0 domain and Client ID.
AuthModule.forRoot function takes the following configuration:
domainvalue present under the Settings of the application you created in the Auth0 Dashboard, or your custom domain if you are using Auth0's custom domains feature.
clientId: The Client ID value present under the Settings of the application you created in the Auth0 Dashboard.
authorizationParams.redirect_uri: The URL to redirect your users after they authenticate with Auth0.
Now that you have configured your app with the Auth0 Angular SDK, run your application to verify that the SDK is initializing without error, and that your application runs as it did before.
In a Capacitor application, the Capacitor's Browser plugin performs a redirect to the Auth0 Universal Login Page. Set the
openUrl parameter on the
loginWithRedirect function to use
Browser.open so that the URL is opened using the device's system browser component (SFSafariViewController on iOS, and Chrome Custom Tabs on Android).
loginWithRedirect function tells the SDK to initiate the login flow, using the
Browser.open function to open the login URL with the platform's system browser component by setting the
openUrl parameter. This provides a way for your user to log in to your application. Users redirect to the login page at Auth0 and do not receive any errors.
Once users logs in with the Universal Login Page, they redirect back to your app via a URL with a custom URL scheme. The
appUrlOpen event must be handled within your app. You can call the
handleRedirectCallback method from the Auth0 SDK to initialize the authentication state.
You can only use this method on a redirect from Auth0. To verify success, check for the presence of the
state parameters in the URL.
Browser.close() method should close the browser when this event is raised.
Note that the
appUrlOpen event callback is wrapped in
ngZone.run. Changes to observables that occur when
handleRedirectCallback runs are picked up by the Angular app. To learn more, read Using Angular with Capacitor. Otherwise, the screen doesn't update to show the authenticated state after log in.
appUrlOpen to your application's
App component and log in. The browser window should close once the user authenticates and logs in to your app.
Now that users can log in, you need to configure a way to log out. Users must redirect to the Auth0 logout endpoint in the browser to clear their browser session. Again, Capacitor's Browser plugin should perform this redirect so that the user does not leave your app and receive a suboptimal experience.
To achieve this with Ionic and Capacitor in conjunction with the Auth0 SDK:
logout, and pass your redirect URL back as the
openUrlparameter to a callback that uses the Capacitor browser plugin to open the URL using
Provide a way for your users to log out of your application. Verify the redirect to Auth0 and then to the address you specified in the
returnTo parameter. Make sure users are no longer logged in to your application.
The Auth0 SDK retrieves the profile information associated with logged-in users in whatever component you need, such as their name or profile picture, to personalize the user interface. The profile information is available through the
user$ property exposed by
Provide a way for your users to see their user profile details within the app and verify you are able to retrieve and see your profile information on screen once you have logged in.
Excellent work! If you made it this far, you should now have login, logout, and user profile information running in your application.
This concludes our quickstart tutorial, but there is so much more to explore. To learn more about what you can do with Auth0, check out:
Did it work?
Any suggestion or typo?Edit on GitHub
Sign up for an or to your existing account to integrate directly with your own tenant.