Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,23 @@ Once the program is finished:

### Create New Users

- To create new users, ensure you have a service role key in your .env as `SUPABASE_SERVICE_ROLE_KEY`
- Regardless of whether you're creating a new batch of users, or a single custom one ensure you have a service role key in your .env as `SUPABASE_SERVICE_ROLE_KEY`
- This can be found right under the Anon key, see "Run Locally" above **THE VALUE IS SECRET, DO NOT SHARE**
- Run `node scripts/create-users.mjs <numberOfUsers>`
- The script will print out the new users login credentials
*Note: As the app no longer uses emails, resetting a password would need to be done manually in SQL.* **Please ask users to be careful and not lose their creds**

#### Batch Creation

- Run `node scripts/create-users.ts <numberOfUsers>`
- The script will print out the new users login credentials and create all the corresponding users in the DB
- Credentials are formatted as such (both are capitalized, but only password is case-sensitive):
- Username: `<Adjective><Animal><1-999>`
- Password: `<Adjective><Animal>`
*Note: As the app no longer uses emails, resetting a password would need to be done manually in SQL.* **Please ask new users to be careful and not lose their creds**

#### Custom Creation

- Run `node scripts/create-custom-user.ts`
- Follow the script instructions to create a user with a custom username and password
*Note: As the app used to use emails as usernames, all new users are internally referred to as `<username>@coding-cat.internal`, but all previous email accounts will still work.*


### Modify Contract
Expand Down
94 changes: 94 additions & 0 deletions scripts/create-custom-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { createClient } from "@supabase/supabase-js";
import { config } from "dotenv";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
import * as readline from 'readline';

const __dirname = dirname(fileURLToPath(import.meta.url));
config({ path: join(__dirname, "../.env.local") });

const SUPABASE_URL = process.env.REACT_APP_SUPABASE_URL;
const SERVICE_ROLE_KEY = process.env.SUPABASE_SERVICE_ROLE_KEY;
// not a real domain, just to comply with Supabase email field
const EMAIL_DOMAIN = "coding-cat.internal";

if (!SUPABASE_URL || !SERVICE_ROLE_KEY) {
console.error("Missing REACT_APP_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env");
process.exit(1);
}

const supabase = createClient(SUPABASE_URL, SERVICE_ROLE_KEY, {
auth: { autoRefreshToken: false, persistSession: false }
});

async function createUser(username: string, password: string) {
const email = `${username}@${EMAIL_DOMAIN}`;

const { data, error } = await supabase.auth.admin.createUser({
email,
password,
email_confirm: true,
});

if (error) {
console.error(`Auth error for "${username}": ${error.message}`);
return false;
}

const { error: profileError } = await supabase
.from("profiles")
.upsert({
profile_id: data.user.id,
username: username,
created_at: data.user.created_at,
}, { onConflict: "profile_id" });

if (profileError) {
console.error(`Profile error for "${username}": ${profileError.message}`);
return false;
}

return true;
}

/**
* @param input the input to be validated
* @returns boolean whether the input is alphanumerical
*/
function validateInput(input: string){
const regex = /^[a-zA-Z0-9]+$/;
return regex.test(input);
}

function getInput(question: string): Promise<string>{
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

return new Promise((resolve) => {
rl.question(question, (answer: string) => {
rl.close();
resolve(answer);
});
});
}

async function main() {
let username;
let password;
let valid;

do{
username = await getInput("Enter username (only alphanumerical characters): ");
password = await getInput("Enter password: ");

valid = validateInput(username) && validateInput(password);
if(!valid) console.log("Invalid input. Only alphanumerical characters are allowed.");
}while(!valid);

const dbOk = await createUser(username, password);
if(dbOk) console.log(`Successfully created user ${username}!`)
}

main();
2 changes: 1 addition & 1 deletion scripts/create-users.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createClient } from "@supabase/supabase-js";
import { readFileSync } from "fs";
import { config } from "dotenv";import { createRequire } from "module";
import { config } from "dotenv";
import { fileURLToPath } from "url";
import { dirname, join } from "path";

Expand Down
Loading