When creating an "initial user", how do I set a username for the seeded user?

I'm working on a small app using NextJS and have been working on a "setup" function/endpoint to seed an initial admin user (for new deployments).

I have zero issue creating a first user with an admin role, but how do I go about setting that user's
username
? (Using the username plugin).

I know that auth.api.updateUser() exists, but it seems like I can't use that because from what I gather, it updates the currently logged in user...which isn't applicable as this is a server-side thing being used to create that user in the first place.

In the docs, I don't see any functions/etc that I can pass the newly created user to in order to update the username/etc, am I missing something?

Thanks in advance!

Here's what I've got so far:

import { user } from '@/auth-schema'
import { auth } from '@/app/utils/auth'
import { db } from '@/app/utils/db'

// Route Handler for setting up our default user.
export async function GET() {
  const adminEmail = process.env.RULE0_INITIAL_EMAIL
  const adminUsername = process.env.RULE0_INITIAL_EMAIL
  const adminPassword = process.env.RULE0_INITIAL_EMAIL

  try {
    if (adminEmail && adminUsername && adminPassword) {
      const existing = await db.select().from(user).limit(1)
      if (existing.length > 0) {
        return new Response(`Unable to make initial user, already exists!`, { status: 400 })
      } else {
        const user = await auth.api.createUser({
          body: {
            name: process.env.RULE0_INITIAL_USERNAME!,
            email: process.env.RULE0_INITIAL_EMAIL!,
            password: process.env.RULE0_INITIAL_PASSWORD!,
            role: 'admin'
          }
        })        
        return new Response('User added successfully!', { status: 200 })
      }
    } else {
      return new Response(
        `Unable to make new user due to lack of env variables! (RULE0_INITIAL_USERNAME/EMAIL/PASSWORD)!`,
        { status: 400 }
      )
    }
  } catch (error) {
    return new Response(`Unable to make new user ${error}`, { status: 400 })
  }
}
Was this page helpful?