Docs Menu

Create and Delete Users - Swift SDK

On this page

  • Create a User
  • Delete a User
  • Delete Users with Async/Await
  • Delete Users with Completion Handlers

For most authentication methods, Atlas App Services automatically creates a user object the first time a user authenticates. The only exception is email/password authentication. When you use email/password authentication, you must register and confirm a user before the user can authenticate to an Atlas App Services application.

Tip
Apple Account Deletion Requirements

Apple requires that applications listed through its App Store must give any user who creates an account the option to delete the account. Whether you use an authentication method where you must manually register a user, such as email/password authentication, or one that that automatically creates a user, such as Sign-In with Apple, you must implement user account deletion by June 30, 2022.

New in version 10.23.0.

You can call the delete method on a user object to delete the user object from your App. This deletes the object from the server in addition to clearing local data.

Important

Deleting a user only deletes the user object, which may contain associated metadata. This does not delete custom user data or user-entered data from your application. Apple requires that you disclose data retention and deletion policies to your application customers and give them a way to request user data deletion. If you collect additional user data, you must implement your own methods or processes to delete that data.

If your application uses Apple's async/await syntax:

func testAsyncDeleteUser() async throws {
// Logging in using anonymous authentication creates a user object
let syncUser = try await app.login(credentials: Credentials.anonymous)
// Now we have a user, and the total users in the app = 1
XCTAssertNotNil(syncUser)
XCTAssertEqual(app.allUsers.count, 1)
// Call the `delete` method to delete the user
try await syncUser.delete()
// When you delete the user, the SyncSession is destroyed and
// there is no current user.
XCTAssertNil(app.currentUser)
// Now that we've deleted the user, the app has no users.
XCTAssertEqual(app.allUsers.count, 0)
}

Starting with Realm Swift SDK Versions 10.15.0 and 10.16.0, many of the Realm APIs support the Swift async/await syntax. Projects must meet these requirements:

Swift SDK Version
Swift Version Requirement
Supported OS
10.25.0
Swift 5.6
iOS 13.x
10.15.0 or 10.16.0
Swift 5.5
iOS 15.x

If your application does not use async/await:

// Logging in using anonymous authentication creates a user object
app.login(credentials: Credentials.anonymous) { [self] (result) in
switch result {
case .failure(let error):
fatalError("Login failed: \(error.localizedDescription)")
case .success(let user):
// Assign the user object to a variable to demonstrate user deletion
syncUser = user
}
}
// Later, after the user is loggedd in we have a user,
// and the total users in the app = 1
XCTAssertNotNil(syncUser)
XCTAssertEqual(app.allUsers.count, 1)
// Call the `delete` method to delete the user
syncUser!.delete { (error) in
XCTAssertNil(error)
}
// When you delete the user, the SyncSession is destroyed and
// there is no current user.
XCTAssertNil(app.currentUser)
// Now that we've deleted the user, the app has no users.
XCTAssertEqual(app.allUsers.count, 0)
←  Manage Users - Swift SDKAuthenticate Users - Swift SDK →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.