Login user after Sign in with Apple

I am working on an iOS app developed with Swift. I managed to implement the Sign in with Apple functionality and register a user with MongoDB Realm. Right after I call the login with the identityToken from Apple:

let credentials = Credentials.apple(idToken: "identityToken")
app.login(credentials: credentials)

I get the RealmUser and can sync with the database in Atlas. However, the RealmUser instance won’t be logged in forever (which is expected behaviour I guess, even if I found a post online which suggested otherwise). So maybe I am missing something regarding that process but I don’t get the identityToken without showing the Sign in with Apple button again.
So does anyone know what the Best Practise is regarding Logging in the user with Sign in with Apple after registering successfully?


You can try this. I’m not using signin with apple, but another custom JWT called CosyncJWT.

Anyway, try:

if app.currentUser != nil {
        self.loggedIn = true
        // or some other indicator that the user is already logged in

Once I used this, it checks for the cached realm user and skips the login process. The only reason you would need to show Sign In With  again would be if they actually logged out.

Hi @Kurt_Libby1,

thanks for your answer. I think my question wasn’t clear enough. I was actually wondering what to do in the case the user is logged out. I was looking for a way to login the user again without showing the Sign in With Apple Button like you would do with email and password (Just store it in the keychain), when the user has something like “Remember me” activated. But I guess it’s not possible with Sign in with Apple.

However, I looked at my implementation again where I tried to adopt some concepts from the RChat app example where they have an AppState that handles login and logout. I saw that they logout the user in the init method:

_ = realmApp.currentUser?.logOut()

So every time the app starts the user gets logged out which I don’t want. But I guess in the example they only do it to show the login on purpose every time the app starts.
So can I actually assume that app.currentUser is never nil (logged out) unless I explicitly logout the user?

Hey Tobi,

I think that is correct. When a realm has already been downloaded, there is a cached user, so you can check for that user and then just access the realm.

If you call logOut, that remove the cached user.

At that point you would need to authenticate ( SIW or any other authentication) in order to create a new authenticated realm user. Then, as long as you don’t call logOut, just check for the user.

I am using a similar AppState, but just in my ContentView.swift file at the top level.

@State private var isLoggedIn: Bool = false

Then somewhere in my first screen where people can log in I have

Spacer().onAppear(perform: {
  if app.currentUser != nil {
    self.isLoggedIn = true

That is enough to then route them to another View so that they never see the log in screen unless they log out.

Hope that helps.


Hi Kurt,

thanks so much for your help and your code examples. In the end I solved the problem by just removing the logOut call. But I wasnt aware that there should be always a cached user until I call logOut`.
So thanks for your input