If you use eM Client (on desktop or mobile) to sync calendars and contacts via CalDAV and CardDAV against a self-hosted Nextcloud instance, you might open your Nextcloud security settings one day and get a shock: hundreds — or even over a thousand — active sessions listed under Devices & sessions, all named eMClient/... or emclient-mobile/....

Nothing is hacked, and your account isn’t under attack. You’ve just hit a mismatch between how eM Client authenticates DAV requests and how Nextcloud tracks password logins — and the fix takes about two minutes per device.


The Problem: An Ever-Growing List of Sessions

In Nextcloud, navigating to Personal settings → Security → Devices & sessions shows every logged-in browser, sync client, and app token on your account. For well-behaved clients (the official Nextcloud desktop/mobile apps, Thunderbird, Notes apps, etc.), you’ll see one entry per device.

With eM Client connected via CalDAV/CardDAV using your regular Nextcloud username and password, the list looks completely different:

  • Multiple new sessions created in a single hour from the same computer or phone

  • Separate piles of old entries left behind every time eM Client updates to a new version (eMClient/10.4.x, eMClient/11.0.x, emclient-mobile/10.5.0.0, emclient-mobile/11.0.0.0)

  • Over months, the table easily balloons past a thousand rows for a single user

Because eM Client doesn’t reuse the Nextcloud session across sync cycles, every reconnect looks like a brand-new login to Nextcloud.


What the Logs and Code Revealed

Comparing the Nextcloud access logs for eM Client against another DAV client (Thunderbird) made the difference jump right off the screen.

1. eM Client (using a regular account password)

Every sync cycle started with an unauthenticated request (401 Unauthorized), followed by an HTTP Basic Auth retry with the main password:

"PROPFIND /remote.php/dav/principals/users/youruser/ HTTP/1.1"
    401 ... "eMClient/11.0.1577.0"
"PROPFIND /remote.php/dav/principals/users/youruser/ HTTP/1.1"
    207 ... "eMClient/11.0.1577.0"
"OPTIONS /remote.php/dav/calendars/youruser/ HTTP/1.1"
    401 ... "eMClient/11.0.1577.0"
"OPTIONS /remote.php/dav/calendars/youruser/ HTTP/1.1"
    200 ... "eMClient/11.0.1577.0"
"OPTIONS /remote.php/dav/addressbooks/users/youruser/ HTTP/1.1"
    401 ... "eMClient/11.0.1577.0"
"OPTIONS /remote.php/dav/addressbooks/users/youruser/ HTTP/1.1"
    200 ... "eMClient/11.0.1577.0"
"REPORT /remote.php/dav/addressbooks/users/youruser/contacts/
    HTTP/1.1" 401 ... "eMClient/11.0.1577.0"
"REPORT /remote.php/dav/addressbooks/users/youruser/contacts/
    HTTP/1.1" 207 ... "eMClient/11.0.1577.0"

Every time eM Client dropped its cookie and hit that 401 → Basic Auth sequence, Nextcloud inserted a new temporary session token (type = 0, remember = 0) into the oc_authtoken database table. Even a single afternoon of syncing could leave three to five fresh session rows behind.

2. Thunderbird (using a Nextcloud App Password)

On the exact same Nextcloud instance, Thunderbird syncing the exact same calendar and contacts looked completely different in the logs:

"REPORT /remote.php/dav/calendars/youruser/personal/
    HTTP/1.1" 207 ... "Thunderbird/..."
"REPORT /remote.php/dav/addressbooks/users/youruser/contacts/
    HTTP/1.1" 207 ... "Thunderbird/..."

Zero 401s, straight to 207 Multi-Status on every poll — and in oc_authtoken, Thunderbird had one single row (type = 1, a permanent app token) that simply updated its last_activity timestamp on every sync.

3. Why App Passwords Stop the Session Bloat Inside Nextcloud

Checking Nextcloud’s authentication handler (lib/private/User/Session.php → logClientIn()) explains why switching to an App Password fixes this completely — regardless of whether a client caches cookies or triggers an initial 401 challenge:

  • Main password ($isTokenPassword === false): After validating the password, Nextcloud calls createSessionToken(), inserting a new TEMPORARY_TOKEN (type = 0) row into oc_authtoken. If the client doesn’t send that session cookie back next time, the next sync creates another row.

  • App Password ($isTokenPassword === true): Nextcloud recognizes the credential as an existing PERMANENT_TOKEN (type = 1), authenticates directly against that token, and never calls createSessionToken(). No matter how many times eM Client reconnects or re-authenticates, it stays one row in your Devices & sessions list.


The Fix: Switch eM Client to Nextcloud App Passwords

Generate one App Password per device running eM Client (desktop computer, laptop, phone, tablet). Giving each device its own named password makes it easy to tell what’s connected and revoke a single device later without touching the others.

Step 1: Create App Passwords in Nextcloud

  1. Sign in to your Nextcloud instance in a web browser.

  2. Click your profile icon in the top-right corner and open Personal settings.

  3. In the left sidebar, select Security.

  4. Scroll to the bottom to Devices & sessions.

  5. In the App name box, enter a clear label for the first device — for example:

    • eM Client - Work Laptop

    • eM Client - Home Desktop

    • eM Client - Phone

  6. Click Create new app password.

  7. Copy the generated password (Nextcloud only shows it once) and click Done.

  8. Repeat for each device where eM Client syncs with your Nextcloud instance.

Step 2: Update eM Client on Desktop

  1. Open eM Client and go to Menu → Accounts (on macOS: eM Client → Accounts).

  2. Select your Nextcloud CalDAV and/or CardDAV account (depending on how you added it, they may be listed together or as separate tabs/entries).

  3. Replace your main Nextcloud password with the new App Password for that computer.

  4. Click Save & Close, then trigger a refresh to confirm calendars and contacts sync cleanly.

Step 3: Update eM Client on Mobile

  1. Open the eM Client mobile app and go to Settings → Accounts.

  2. Select your Nextcloud CalDAV/CardDAV account.

  3. Replace your main password with the App Password you generated for your phone.

  4. Save and run a manual sync to verify it connects without errors.

Step 4: Clean Up the Old Sessions

Once every device is running on its App Password, you can revoke the stale eMClient/... and emclient-mobile/... entries under Personal settings → Security → Devices & sessions (or let Nextcloud’s background cleanup job prune the expired ones automatically — see the bonus tip below).

Going forward, you’ll see just one clean entry per device, updating its “last activity” timestamp whenever it syncs instead of multiplying endlessly.

Gotcha: Use Your Exact Username, Not Your Email Address

If you previously signed in with your email address (e.g., alice@example.com) and your main password, it worked because Nextcloud supports email-address fallback for main passwords. App Passwords do not allow that fallback — Nextcloud strictly requires the username to match the exact login_name you were logged in with when you created the App Password (usually your short username, e.g., alice).

If you use your email address with an App Password, Nextcloud rejects every sync with 401 Unauthorized and logs:

App token login name does not match

Watch out for two quirks in eM Client when making the switch:

  • Check every tab on the account (CalDAV and CardDAV can hold separate username/password fields — updating only one leaves the other failing).

  • Watch the login popup: if an auth prompt pops up and you type your email address into it, eM Client can overwrite the saved username. Quit and relaunch eM Client after saving the short username + App Password in Accounts.

  • If several failed attempts fire in a row, Nextcloud’s brute-force protection may briefly return 429 Too Many Requests (you can wait out the cooldown or clear it immediately with occ security:bruteforce:reset <client-ip>).


Bonus: Is Nextcloud’s Cron Running?

While digging into why months-old temporary sessions were still sitting in oc_authtoken, we uncovered a second culprit on the server side: Nextcloud’s background job mode.

Out of the box, Nextcloud defaults to AJAX mode, which only runs a single background job whenever someone happens to load a page in the web UI. Meanwhile, Nextcloud’s OC\Authentication\Token\TokenCleanupJob (the job that prunes expired temporary sessions) is marked time_sensitive = 0, meaning it only runs during Nextcloud’s configured maintenance window (typically a 4-hour overnight window).

If nobody opens the Nextcloud web UI between 1:00 AM and 5:00 AM, AJAX mode never runs overnight maintenance jobs at all — so expired temporary sessions sit in the database forever.

To check and fix this on a Docker-hosted Nextcloud instance:

  1. Check your current mode in Administration settings → Basic settings → Background jobs (or via CLI):
    docker exec -u www-data nextcloud \
      php occ config:app:get core backgroundjobs_mode
    
  2. Switch to system cron mode:
    docker exec -u www-data nextcloud php occ background:cron
    
  3. Schedule cron.php to run every 5 minutes — for example, with a systemd timer on the host (or a host cron entry):
    ; /etc/systemd/system/nextcloud-cron.service
    [Unit]
    Description=Run Nextcloud cron.php inside container
    After=docker.service
    
    [Service]
    Type=oneshot
    TimeoutStartSec=240
    ExecStart=/usr/bin/docker exec -u www-data nextcloud \
        php -f /var/www/html/cron.php
    
    ; /etc/systemd/system/nextcloud-cron.timer
    [Unit]
    Description=Run Nextcloud cron.php every 5 minutes
    
    [Timer]
    OnBootSec=2min
    OnUnitActiveSec=5min
    
    [Install]
    WantedBy=timers.target
    
    Enable and start it:
    sudo systemctl daemon-reload
    sudo systemctl enable --now nextcloud-cron.timer
    

Once cron is running every 5 minutes around the clock, Nextcloud’s overnight maintenance jobs — including TokenCleanupJob — will execute on schedule without needing anyone to click around the web interface at 2:00 AM.


Why App Passwords Are Worth Using Anyway

Even apart from keeping your session list tidy, App Passwords give you three extra wins on Nextcloud:

  • Per-device revocation: If a phone is lost or you retire a laptop, you revoke one App Password in Nextcloud without changing your main password or breaking your other devices.

  • 2FA compatibility: If you turn on two-factor authentication on your Nextcloud account, main-password HTTP Basic Auth stops working on CalDAV/CardDAV endpoints — App Passwords keep working seamlessly.

  • Less auth overhead: If your Nextcloud instance authenticates users against an external directory (such as LDAP), App Passwords validate locally against Nextcloud’s token table instead of hitting your identity provider on every DAV reconnect.