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.
We attach a login function to the button.
We can then create a startDitto function that:
- Gets the access token from Auth0;
- Starts the Ditto instance; and
- Creates a liveQuery
The provider name given to the Ditto Client must match a provider name in the Portal (e.g., replit-auth).
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:
Our ContentView can now display the number of cars, and you can add a button for adding an item to the database:
Log out
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.
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.