Skip to content

Commit

Permalink
Add script to get access token for user, fix create-user script
Browse files Browse the repository at this point in the history
  • Loading branch information
VictiniX888 committed Apr 3, 2024
1 parent 5ff5877 commit 4c47ad1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
14 changes: 4 additions & 10 deletions tools/scripts/create-user.ts
Expand Up @@ -2,7 +2,6 @@ import dotenv from 'dotenv';
dotenv.config();

import { createClient, SupabaseClient, User } from '@supabase/supabase-js';
import { getEnv } from '../../libs/env/src';

const INSTRUCTIONS = `
Usage:
Expand Down Expand Up @@ -50,20 +49,15 @@ async function createUser(): Promise<User | null> {
}

function createSupabaseServiceClient(): SupabaseClient {
const env = getEnv();
if (
env.Hibiscus.Supabase.apiUrl == null ||
env.Hibiscus.Supabase.serviceKey == null
) {
const apiUrl = process.env.NEXT_PUBLIC_HIBISCUS_SUPABASE_API_URL;
const serviceKey = process.env.HIBISCUS_SUPABASE_SERVICE_KEY;
if (apiUrl == null || serviceKey == null) {
throw new Error(
'Supabase API URL or service key not defined in environment variables'
);
}

return createClient(
getEnv().Hibiscus.Supabase.apiUrl ?? '',
getEnv().Hibiscus.Supabase.serviceKey ?? ''
);
return createClient(apiUrl ?? '', serviceKey ?? '');
}

(async () => {
Expand Down
51 changes: 51 additions & 0 deletions tools/scripts/get-access-token.ts
@@ -0,0 +1,51 @@
import dotenv from 'dotenv';
dotenv.config();

import { createClient, Session, SupabaseClient } from '@supabase/supabase-js';

const INSTRUCTIONS = `
Usage:
npx ts-node path/to/script.ts email password
`;

if (process.argv.length < 4) {
console.log(INSTRUCTIONS);
}

const email = process.argv[2];
const password = process.argv[3];

async function login(): Promise<Session | null> {
const supabase = createSupabaseServiceClient();
const {
data: { session },
} = await supabase.auth.signInWithPassword({
email: email,
password: password,
});

return session;
}

function createSupabaseServiceClient(): SupabaseClient {
const apiUrl = process.env.NEXT_PUBLIC_HIBISCUS_SUPABASE_API_URL;
const serviceKey = process.env.HIBISCUS_SUPABASE_SERVICE_KEY;
if (apiUrl == null || serviceKey == null) {
throw new Error(
'Supabase API URL or service key not defined in environment variables'
);
}

return createClient(apiUrl ?? '', serviceKey ?? '');
}

(async () => {
const session = await login();
if (session != null) {
console.log('User logged in:');
console.log(session);
} else {
console.error('Failed to log in');
}
process.exit();
})();

0 comments on commit 4c47ad1

Please sign in to comment.