Snaplet & Supabase & Flutter | How to create a known user for testing

01kg - Oct 6 - - Dev Community

Snaplet helped a lot in mocking data.

After installed it and learnt how to integrate it with Supabase, I need a "known" user for testing.

Creating a random user, is a piece of cake for Snaplet.

Say for testing or demo purpose, I wanna create a user with email my@user.com and password userpass, and generate a lot data related to the user.

TL;DR

Create a package.json file in your Flutter project with content {} if no such a file exists.

Install packages:

  1. npm install bcrypt
  2. npm i --save-dev @types/bcrypt
// seed.ts
import { createSeedClient } from "@snaplet/seed";
import { v4 as uuidv4 } from "uuid";
import bcrypt from "bcrypt";
async function hashPassword(password: string): Promise<string> {
  const saltRounds = 10; // You can adjust the salt rounds as needed
  const salt = await bcrypt.genSalt(saltRounds);
  const hashedPassword = await bcrypt.hash(password, salt);
  return hashedPassword;
}

const DRY_RUN = false;

const USER_EMAIL = "my@user.com";
const USER_PASSWORD = "userpass";

const main = async () => {
  const USER_ENCRYPTED_PASSWORD = await hashPassword(USER_PASSWORD);
  const USER_ID = uuidv4();

  const seed = await createSeedClient({ dryRun: DRY_RUN });

  // Truncate all tables in the database
  await seed.$resetDatabase();

  // Create the user and its related data
  await seed.auth_users([
    {
      id: USER_ID,
      instance_id: "00000000-0000-0000-0000-000000000000",
      aud: "authenticated",
      role: "authenticated",
      email: USER_EMAIL,
      encrypted_password: USER_ENCRYPTED_PASSWORD,
      // email_confirmed_at: "", // Snaplet will generate this for you
      invited_at: null,
      confirmation_token: "",
      confirmation_sent_at: null,
      recovery_token: "",
      recovery_sent_at: null,
      email_change_token_new: "",
      email_change: "",
      email_change_sent_at: null,
      // last_sign_in_at: "", // Snaplet will generate this for you
      raw_app_meta_data: { "provider": "email", "providers": ["email"] },
      raw_user_meta_data: {
        "sub": USER_ID,
        "email": USER_EMAIL,
        "email_verified": false,
        "phone_verified": false,
      },
      is_super_admin: null,
      // created_at: "", // Snaplet will generate this for you
      // updated_at: "", // Snaplet will generate this for you
      phone: null,
      phone_confirmed_at: null,
      phone_change: "",
      phone_change_token: "",
      phone_change_sent_at: null,
      email_change_token_current: "",
      email_change_confirm_status: 0,
      banned_until: null,
      reauthentication_token: "",
      reauthentication_sent_at: null,
      is_sso_user: false,
      deleted_at: null,
      is_anonymous: false,

      identities: [{
        // id: "", // Snaplet will generate this for you
        identity_data: {
          "sub": USER_ID,
          "email": USER_EMAIL,
          "email_verified": false,
          "phone_verified": false,
        },
        provider: "email",
        provider_id: USER_ID, // If the provider is email or phone, the id is the user's id from the auth.users table. https://supabase.com/docs/guides/auth/identities#the-user-identity-object
        // last_sign_in_at: "", // Snaplet will generate this for you
        // created_at: "", // Snaplet will generate this for you
        // updated_at: "", // Snaplet will generate this for you
      }],
      // data related to user
      // ...
    },
  ]);

  if (!DRY_RUN) {
    console.log("Database seeded successfully!");
  }

  process.exit();
};

main();

Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .