How to migrate Netlify GoTrue Users To Appwrite

ionicisere

Bradley Schofield

Posted on September 22, 2022

How to migrate Netlify GoTrue Users To Appwrite

With Appwrite 1.0, we are excited to announce that you can import users from different platforms to Appwrite. One of these platforms is Netlify GoTrue. In this tutorial,
we will show you how to export users from a GoTrue based platform and import them into Appwrite.

First, you might be wondering what exactly Appwrite is. Appwrite is an open-source Backend-as-a-Service (BaaS) packaged as a set of Docker micro-services to give developers of any background the tools necessary to build modern apps quickly and securely.

For this tutorial, you will need NodeJS installed on your machine, as we will be using it to export your GoTrue users and import them into Appwrite. We will also provide a script at the end of this tutorial that you can copy and paste to get started.

TL;DR - GitHub to completed project

Warning: Users with passwords that are less than six characters will not work with Appwrite; we suggest sending an email to your users to reset their password in this case.

Now then, let's move on to our first stage.

Exporting Users from GoTrue

To export users from GoTrue, you need to connect to your Postgres database and dump the auth.users table. This can be done using the node-postgres node module.

Using the node-postgres module

First, create a project folder and install the node-postgres module using npm.

npm install pg
Enter fullscreen mode Exit fullscreen mode

Next, create a file called main.js and add the following code.

const { Client } = require('pg')

// Connect to your Postgres database
const client = new Client({
    user: 'postgres',
    host: 'postgres_url',
    database: 'postgres',
    password: 'yourpasword',
    port: 5432,
  })

client.connect()

// Dump the auth.users table
client.query('SELECT * FROM auth.users', (err, res) => {
    if (err) {
        console.log(err.stack)
        client.end()
    } else {
        console.log('Recieved ' + res.rowCount + ' users')
        client.end()
        return res.rows
    }
})
Enter fullscreen mode Exit fullscreen mode

This code will connect to your GoTrue database and dump the auth.users table. You can then use the res.rows variable to access the users. In the next section, we will add this code to import the users into Appwrite.

Importing Users to Appwrite

Now that we have exported the users from GoTrue, we can import them into Appwrite. To do this, we will be using the Appwrite Server SDK for NodeJS.

First, we are going to install the Appwrite SDK using NPM.

npm install node-appwrite
Enter fullscreen mode Exit fullscreen mode

Once done, we will extend the code we wrote in the previous section to import the users into Appwrite.

Add the following at the top of the file, making sure to update the variables to match your Appwrite instance:

const SDK = require('node-appwrite')

const appwriteClient = new sdk.Client()

appwriteClient
    .setEndpoint('http://localhost/v1') // Your API Endpoint
    .setProject('projectKey') // Your project ID
    .setKey('Server API Key') // Your secret API key
    .setSelfSigned() // Use only on dev mode with a self-signed SSL cert
;
Enter fullscreen mode Exit fullscreen mode

Then overwrite the code in the client.query callback with the following:

console.log('Received ' + res.rowCount + ' users')

for (const user of res.rows) { 
            console.log('Importing user' + user.email)

            await users.createBcryptUser(
                user.id,
                user.email,
                user.encrypted_password
            )
  }

console.log('Successfully Imported ' + res.rowCount + ' users')
client.end()
return res.rows
Enter fullscreen mode Exit fullscreen mode

This code will loop through each user and create a new user in Appwrite using the users.createBcryptUser method. You can find more information about this method in the Appwrite API Reference.

With all that done, you can run the script using node main.js, and your users will be imported into Appwrite. Make sure you have a test account in GoTrue to validate that the import was successful.

Conclusions

This tutorial showed you how to export users from a GoTrue based platform and import them into Appwrite. We also provided a script that you can copy and paste to get started. If you have questions or comments, feel free to join our Discord server.

A complete version of this project can be found in the GitHub repository here.

More about Appwrite

Appwrite is an open-source Backend-as-a-Service (BaaS), packaged as a set of Docker micro-services to give developers of any background the tools necessary to build modern apps quickly and securely.

Chat with us on Discord, or, Learn more about Appwrite:
Check out Appwrite as the backend for your next web, Flutter, or server-side application. Here are some handy links for more information:
Appwrite Contribution Guide
Appwrite Discord
Appwrite Github
Appwrite Documentation

💖 💪 🙅 🚩
ionicisere
Bradley Schofield

Posted on September 22, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related