Skip to content

How to use authentication with Sync feature #1241

Answered by doobe01
doobe01 asked this question in Q&A
Discussion options

You must be logged in to vote

I think I got it.

import { synchronize } from '@nozbe/watermelondb/sync'

async function mySync() {
  await synchronize({
    database,
    pullChanges: async ({ lastPulledAt, schemaVersion, migration }) => {
      const urlParams = `last_pulled_at=${lastPulledAt}&schema_version=${schemaVersion}&migration=${encodeURIComponent(JSON.stringify(migration))}`
      const response = await fetch(`https://my.backend/sync?${urlParams}`, {
        method: 'GET',
        headers: {
             'Authorization': 'Bearer ' + TOKEN,
        body: JSON.stringify(changes)
      })
      if (!response.ok) {
        throw new Error(await response.text())
      }

      const { changes, timestamp } = await …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by radex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant