This library works with the core
Convex Swift
library and provides support for using Auth0 in ConvexClientWithAuth.
The integration uses Auth0's Universal Login. Users are prompted to authenticate via a browser window and then seamlessly returned to your app UI.
First of all, if you haven't started a Convex application yet, head over to the Convex Swift iOS quickstart to get the basics down. It will get you up and running with a Convex dev deployment and a basic Swift application that communicates with it.
Once you have a working Convex + Swift application, you're ready to take the following steps to integrate with Auth0.
Note
There are a lot of moving parts to getting auth set up. If you run into trouble check out the Convex auth docs and join our Discord community to get help.
-
Follow the first three steps of the official Auth0 iOS quickstart ("Configure Auth0", "Install the SDK" and "Configure the SDK").
-
Update your Convex application to support auth. Create a
convex/auth.config.tsfile with the following content:export default { providers: [ { domain: "your-domain.us.auth0.com", applicationID: "yourclientid", }, ] }; -
Run
npx convex devto sync the config change. -
Add a dependency on this library to your Xcode project.
-
Then, wherever you have setup your Convex client with
ConvexClient, switch to usingConvexClientWithAuthand passAuth0Provideryou created.let client = ConvexClientWithAuth(deploymentUrl: "$YOUR_DEPLOYMENT_URL", authProvider: Auth0Provider())
-
Ensure that you update other references where
ConvexClientis defined as a parameter or field toConvexClientWithAuth.
At this point you should be able to use the login and logout methods on the client to perform
authentication with Auth0. Your Convex backend will receive the ID token from Auth0 and you'll be
able to
use authentication details in your backend functions.
The ConvexClientWithAuth.authState field is a Publisher that contains the latest authentication
state from the client. You can setup your UI to react to new authState values and show the
appropriate screens (e.g. login/logout buttons, loading screens, authenticated content).
The AuthState.authenticated value will contain the
Credentials
object received from Auth0 and you can use the data that it contains to customize the user
experience in your app.
If you would like your users to be able to launch your app directly into a signed in state after an
initial authentication, you can call the ConvexClientWithAuth.loginFromCache method and it will
automatically sign the user back in, refreshing credentials if needed. It will update the
authState flow just like calls to login and logout do for interactive operations.