SDK Setup Guides
...
Tutorials in Swift
OnlineWithAuthentication: Swift Tutorial
for the full source code, see the getditto > https //github com/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 class profileviewmodel observableobject { // your authentication code will go here } struct contentview view { @observedobject var viewmodel profileviewmodel = profileviewmodel() var body some view { button("login") padding() } } we attach a login function to the button class profileviewmodel observableobject { let credentialsmanager = credentialsmanager(authentication auth0 authentication()) func login () { auth0 webauth() scope("openid profile") audience("https //enter your scope url here auth0 com/userinfo") start { result in switch result { case success(let credentials) print("obtained credentials \\(credentials)") self credentialsmanager store(credentials credentials) self startditto() case failure(let error) print("failed with \\(error)") // handle error } } } } struct contentview view { @observedobject var viewmodel profileviewmodel = profileviewmodel() var body some view { button("login", action viewmodel login) padding() } } 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 ) class profileviewmodel observableobject { @published var ditto ditto? @published var docs \[dittodocument] = \[] func startditto () { // 1 get the access token from auth0 credentialsmanager credentials { error, credentials in guard error == nil, let credentials = credentials else { // handle error return } guard let accesstoken = credentials accesstoken else { // handle error return } self authdelegate = authdelegate(token accesstoken) // 2 start the ditto instance let identity = dittoidentity onlinewithauthentication( appid "your app id here", authenticationdelegate self authdelegate ) let ditto = ditto(identity identity) try! ditto startsync() // 3 create a livequery self ditto = ditto let subscription = ditto store collection("cars") find("state == 'for sale'") subscribe() let livequery = ditto store collection("cars") find("name == 'toyota'") observelocal { docs, event in self docs = docs } } } } 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 class authdelegate dittoauthenticationdelegate { var token string init (token string) { self token = token } func authenticationrequired(authenticator dittoauthenticator) { authenticator login(self token, provider "replit auth") { clientinfo, err in print("login request completed error? \\(err)") } } func authenticationexpiringsoon(authenticator dittoauthenticator, secondsremaining int64) { print("auth token expiring in \\(secondsremaining)") authenticator login(self token, provider "replit auth") { clientinfo, err in print("login request completed error? \\(err)") } } } our contentview can now display the number of cars, and you can add a button for adding an item to the database struct contentview view { @observedobject var viewmodel profileviewmodel = profileviewmodel() var body some view { button("login", action viewmodel login) padding() text("cars " + string(viewmodel docs count)) // bonus points implement additem button using ditto's `upsert` button("+1", viewmodel additem) } } log out class profileviewmodel observableobject { @published private(set) var state = state isloading enum state { case isloading case loaded(userinfo) } } struct contentview view { @observedobject var viewmodel profileviewmodel = profileviewmodel() var body some view { switch viewmodel state { case isloading button("login", action viewmodel login) case loaded(let user) text(user name ?? "anonymous ditto user") button("logout", action viewmodel logout) } text("cars " + string(viewmodel docs count)) } } 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 class profileviewmodel observableobject { func logout () { auth0 webauth() clearsession(federated false) { result in if result { if (self ditto != nil) { // clean up the cars collection after logout self ditto! auth? logout(cleanup { ditto in ditto store collection("cars") findall() evict() }) } self state = state isloading } } } } 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