Add automatic role assignment for new and existing users

- Implemented auto-assignment of the 'private' role for new users upon first login, ensuring they have access to products.
- Added a safety check to assign the 'private' role to existing users without roles during login.
- Updated relevant documentation to reflect these changes in role management and visibility patterns.
This commit is contained in:
Bastian Masanek
2025-11-02 10:40:52 +01:00
parent 268d91f548
commit cb4810893c
4 changed files with 69 additions and 2 deletions

View File

@@ -63,6 +63,11 @@ export default defineEventHandler(async (event) => {
.returning()
user = newUser
// Auto-assign 'private' role on first login
await assignRoleToUser(newUser.id, 'private', {
adminNotes: 'Auto-assigned on first login',
})
} else {
// Update last login timestamp
await db
@@ -71,6 +76,14 @@ export default defineEventHandler(async (event) => {
updatedAt: new Date(),
})
.where(eq(users.id, user.id))
// Safety check: If existing user has no roles, assign 'private' role
const userRoleCodes = await getUserApprovedRoleCodes(user.id)
if (userRoleCodes.length === 0) {
await assignRoleToUser(user.id, 'private', {
adminNotes: 'Auto-assigned for existing user without roles',
})
}
}
// 6. Create encrypted session