import type { ZitaldelClient } from './classes.zitadelclient.js'; import type { ZitadelProjectRole } from './classes.zitadelprojectrole.js'; import * as plugins from './plugins.js'; export interface IZitadelUserData { id: string; lastLogin: Date; username: string; } export class ZitaldelUser { // INSTANCE zitadelclientRef: ZitaldelClient; data: IZitadelUserData; constructor(zitadelclientRefArg: ZitaldelClient, dataArg: IZitadelUserData) { this.zitadelclientRef = zitadelclientRefArg; this.data = dataArg; } public async addRole(projectRole: ZitadelProjectRole) { const response = await this.zitadelclientRef.managementClient.addUserGrant({ userId: this.data.id, roleKeys: [projectRole.data.key], projectId: projectRole.data.project.data.id, }); } /** * change email address of user, * optionally supply own url for email verification * @param emailAddress * @param verificationUrl */ public async changeEmail(optionsArg: { emailAddress: string; verificationUrl?: string }) { const response = await this.zitadelclientRef.userClient.setEmail({ userId: this.data.id, email: optionsArg.emailAddress, ...(optionsArg.verificationUrl ? { sendCode: { urlTemplate: optionsArg.verificationUrl, }, } : {}), }); } public async setPassword(optionsArg: { password: string; changeRequired?: boolean }) { const response = await this.zitadelclientRef.userClient.setPassword({ userId: this.data.id, newPassword: { password: optionsArg.password, ...(optionsArg.changeRequired ? { changeRequired: optionsArg.changeRequired } : {}), }, }); } /** * triggers a password reset action for the user */ public async resetPassword() { const response = await this.zitadelclientRef.userClient.passwordReset({ userId: this.data.id, }); } }