SDK Guides
...
Swift
Tutorials in Swift

OnlineWithAuthentication: Swift Tutorial

For the full source code, see the getditto > sample-authentication-permissions repository in GitHub.

Assuming you have a login button in your SwiftUI ContentView, we want to create a new ObservedObject that we can subscribe to for updates to the authentication status.

Swift
Text


We attach a login function to the button.

Swift


We can then create a startDitto function that:

  1. Gets the access token from Auth0;
  2. Starts the Ditto instance; and
  3. Creates a liveQuery

The provider name given to the Ditto Client must match a provider name in the Portal (e.g., replit-auth).

Swift


To pass the token to your server route you created in the previous section, you need to create an AuthDelegate class that is passed to the Ditto constructor:

Swift


Our ContentView can now display the number of cars, and you can add a button for adding an item to the database:

Swift


Log out​

Swift


And then we can write the logout function and attach it to the button. We also recommend calling ditto.auth.logout with a callback function that evicts any data from the local database.

Swift


To make this usable for real-world applications, you can retrieve the user's profile details such as email, username, and full name. See the official Auth0 documentation for your platform to add that functionality to your application

Yay! You now have a fully functioning onlineWithAuthentication app. Build and run it on a device.