Skip to main content

Overview

The Nurse Handoff Helper requires nurse accounts to be created in both the database and Supabase Auth. This guide explains how to create and manage these accounts using the provided CLI scripts.
The application includes automated scripts to simplify nurse account creation and management. These scripts handle both database records and authentication accounts.

Prerequisites

Before creating nurse accounts, ensure:
  • Supabase database is set up with the nurses table
  • Environment variables are configured, especially SUPABASE_SERVICE_KEY
  • You have the service role key (required for creating auth accounts)
The SUPABASE_SERVICE_KEY is required for creating authentication accounts. Without it, you can only list existing nurses.

Available Commands

The application provides npm scripts for managing nurse accounts:
# List all nurses in the database
npm run nurses:list

# Create authentication accounts for nurses
npm run nurses:create

# List nurse credentials (if applicable)
npm run nurses:credentials

# Check authentication status
npm run nurses:check

# Reset nurse passwords
npm run nurses:reset-passwords

# Test login functionality
npm run test:login

List Existing Nurses

To view all nurses in your database:
npm run nurses:list
This command will display:
  • Nurse name
  • Email address
  • Database ID
  • Authentication account status (linked or not linked)

Example Output

📋 Fetching nurses from database...

Found 3 nurse(s):

1. Emily Chen
   Email: [email protected]
   ID: 550e8400-e29b-41d4-a716-446655440001
   Auth Account: ✅ Linked

2. Michael Rodriguez
   Email: [email protected]
   ID: 550e8400-e29b-41d4-a716-446655440002
   Auth Account: ❌ Not linked

3. Sarah Williams
   Email: [email protected]
   ID: 550e8400-e29b-41d4-a716-446655440003
   Auth Account: ❌ Not linked

📊 Summary:
   ✅ 1 nurse(s) with auth accounts
   ❌ 2 nurse(s) without auth accounts

Create Authentication Accounts

To create Supabase Auth accounts for nurses in your database:
npm run nurses:create
This script (scripts/setup-nurse-accounts.js:76):
  1. Fetches all nurses from the database
  2. Skips nurses who already have auth accounts
  3. Generates email addresses if missing (format: [email protected])
  4. Creates Supabase Auth accounts with temporary passwords
  5. Links the auth account to the nurse record
  6. Displays temporary passwords for each new account

How It Works

1

Fetch Nurses

The script retrieves all nurse records from the nurses table.
2

Check Existing Accounts

For each nurse, it checks if they already have an auth_user_id linked. If yes, the nurse is skipped.
3

Generate Email

If a nurse doesn’t have an email, the script generates one:
// Example: "Emily Chen" becomes "[email protected]"
const emailName = nurse.name
  .toLowerCase()
  .replace(/[^a-z\s]/g, '')
  .split(/\s+/)
  .join('.');
const nurseEmail = `${emailName}@hospital.org`;
4

Create Auth Account

Creates a Supabase Auth user with:
  • Email address
  • Temporary password (Temp{nurse_id}{timestamp})
  • Auto-confirmed email
  • User metadata (name and nurse_id)
const { data: authUser, error } = await supabaseAdmin.auth.admin.createUser({
  email: nurseEmail,
  password: tempPassword,
  email_confirm: true,
  user_metadata: {
    name: nurse.name,
    nurse_id: nurse.id,
  },
});
5

Link Accounts

Updates the nurse record with the auth user ID:
await supabase
  .from('nurses')
  .update({ auth_user_id: authUser.user.id })
  .eq('id', nurse.id);

Example Output

🔐 Creating auth accounts for nurses...

Results:

✅ Created accounts:
   Emily Chen ([email protected])
   Password: Temp550e84001709123456
   
   Michael Rodriguez ([email protected])
   Password: Temp550e84021709123457

🔗 Linked existing accounts:
   Sarah Williams ([email protected])

⏭️  Skipped (already have accounts):
   John Doe ([email protected])

📊 Summary:
   ✅ Created: 2
   🔗 Linked: 1
   ⏭️  Skipped: 1
   ❌ Errors: 0

💡 Nurses can now log in with their email and the temporary password shown above.
   They should change their password after first login.

Adding Nurses to Database

Before creating auth accounts, you need to add nurse records to the database. You can do this via:

Option 1: Supabase Dashboard

  1. Go to your Supabase project
  2. Navigate to Table Editornurses
  3. Click Insert row
  4. Fill in:
    • name: Full name (required)
    • email: Email address (optional, will be auto-generated)
  5. Click Save

Option 2: SQL Insert

INSERT INTO nurses (name, email) VALUES
  ('Emily Chen', '[email protected]'),
  ('Michael Rodriguez', '[email protected]'),
  ('Sarah Williams', '[email protected]');

Option 3: API Endpoint

The application automatically creates nurse records during first login if they don’t exist (server/index.js:35).

Password Management

Temporary Passwords

When accounts are created, they receive temporary passwords in the format:
Temp{nurse_id}{timestamp}
Example: Temp550e84001709123456
Save these passwords securely and provide them to nurses through a secure channel. They are only displayed once during account creation.

First Login Flow

  1. Nurse logs in with email and temporary password
  2. Application authenticates via Supabase Auth
  3. Nurse should be prompted to change password (implement this in your frontend)

Resetting Passwords

To reset nurse passwords:
npm run nurses:reset-passwords
Or use the Supabase dashboard:
  1. Go to AuthenticationUsers
  2. Find the nurse’s auth account
  3. Click Reset password
  4. Send reset email or set new password manually

Account Linking

The script handles three scenarios:

New Account

No auth account exists. Creates new Supabase Auth user and links it.

Existing Account

Auth account exists but not linked. Links existing auth user to nurse record.

Already Linked

Nurse already has auth_user_id. Skips creation.

API Endpoint for Account Creation

You can also create accounts programmatically via the API endpoint (server/index.js:580):
POST /api/nurses/create-accounts

Request

curl -X POST http://localhost:3001/api/nurses/create-accounts

Response

{
  "success": true,
  "message": "Processed 3 nurses",
  "results": [
    {
      "nurse_id": "550e8400-e29b-41d4-a716-446655440001",
      "email": "[email protected]",
      "status": "created",
      "message": "Account created. Temporary password: Temp550e84001709123456",
      "tempPassword": "Temp550e84001709123456"
    }
  ]
}

Troubleshooting

The service role key is required to create auth accounts. Add it to your .env file:
SUPABASE_SERVICE_KEY=your_service_role_key_here
Get it from Project SettingsAPIservice_role in Supabase dashboard.
You need to add nurses to the nurses table first. Use one of the methods described in “Adding Nurses to Database” above.
If you see “User with this email already exists”, the script will attempt to link the existing auth user to the nurse record instead of creating a new one.

Security Best Practices

Change Default Passwords

Ensure nurses change their temporary passwords on first login.

Use Strong Passwords

Implement password strength requirements in your authentication flow.

Secure Credential Sharing

Share temporary passwords through secure channels (not email or Slack).

Regular Audits

Periodically review nurse accounts and remove inactive users.

Next Steps

After creating nurse accounts:
  1. Test login functionality with npm run test:login
  2. Start the application with npm start
  3. Have nurses log in and change their passwords
  4. Assign nurses to rooms and patients

Build docs developers (and LLMs) love