Skip to main content

User Role Update

Assign/Update Role

This guide will help you use the insertUserAndAssignRole function to insert a user and assign or update their role in the database. You can customize the email and role ID based on your requirements. After configuration is complete you will need to run pnpm make:admin command to update your user role .This command will help you manually update your user role in the system .

Function Overview

The insertUserAndAssignRole function performs the following steps:

  1. Checks if a user with the specified email already exists.
  2. If the user does not exist:
    • Creates a new user.
    • Assigns the specified role to the new user.
  3. If the user exists:
    • Checks if the user already has the specified role.
    • If the role does not exist, assigns the new role to the user.
    • If the role exists, updates the role.

Please make sure you have made the necessary changes before runninng the command . For roleId you can either use 1 for super admin and 2 for regular user. Change the values in commented line from the code

Change Email And RoleId

Update the email address in the function call to the desired user email.Please edit the file src/db/seeds/user-role.ts in this location for inserting or updating super admin user role. You will need to use your prefered email and roleId. Modify the commented line from the code bellow

src/db/seeds/user-role.ts
export const insertUserAndAssignRole = async (
db: NodePgDatabase<Record<string, never>>,
) => {
try {
const [adminUser] = await db
.select({id:users.id})
.from(users).where(eq(users.email,'demo@filekit.com')) //CHANGE THE EMAIL ACCORDING TO YOUR NEED
if (!adminUser) {
let userData = {
name: 'FileKit Admin',
email: 'demo@filekit.com', //CHANGE THE EMAIL ACCORDING TO YOUR NEED
email_verified: true,
status: UserStatus.Active,
}
const [user] = await db.insert(users).values(userData).returning();

const userRoleData = {
roleId: 1, // CHANGE THE ROLEID ACCORDING TO YOUR NEED .USE 1 FOR SUPER ADMIN AND 2 FOR REGULAR USER
userId: user.id
}
console.log('🌱 Started Creating Admin \n');
const [userRole] = await db.insert(userRoles).values(userRoleData).returning();
console.log('🚀 Creating......\n');
console.log('✅ Admin Created\n');
console.log(`🌱 Your Super Admin Email '${user.email}' !\n`);
console.log(`✅ Copy this email and login into FileKit dashboard !\n`);
} else {
const userRoleData = {
roleId: 1, // CHANGE THE ROLEID ACCORDING TO YOUR NEED .USE 1 FOR SUPER ADMIN AND 2 FOR REGULAR USER
userId:adminUser.id
}
const [updateRole] = await db
.select()
.from(userRoles).where(eq(userRoles.userId,adminUser.id))
console.log('🌱 Assigning admin role to user\n');
if(!updateRole){
const [userRole] = await db.insert(userRoles).values(userRoleData).returning();
}else{
const userRole = await db.update(userRoles).set(userRoleData).where(eq(userRoles.userId,adminUser.id));
}
console.log('🚀 Updating role......\n');
console.log("✅ Role updated");
}

} catch (error) {
console.error('Error inserting user and assigning role:', error);
}
};