zitadel/ts/classes.zitadeluser.ts

77 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-05-02 12:07:42 +00:00
import type { ZitaldelClient } from './classes.zitadelclient.js';
2024-05-02 15:43:31 +00:00
import type { ZitadelProjectRole } from './classes.zitadelprojectrole.js';
import * as plugins from './plugins.js';
2024-05-02 10:52:21 +00:00
2024-05-02 12:07:42 +00:00
export interface IZitadelUserData {
id: string;
lastLogin: Date;
username: string;
2024-05-03 08:39:24 +00:00
}
2024-05-02 10:52:21 +00:00
export class ZitaldelUser {
2024-05-02 15:43:31 +00:00
// INSTANCE
2024-05-02 12:07:42 +00:00
zitadelclientRef: ZitaldelClient;
2024-05-02 15:43:31 +00:00
data: IZitadelUserData;
2024-05-02 12:07:42 +00:00
constructor(zitadelclientRefArg: ZitaldelClient, dataArg: IZitadelUserData) {
this.zitadelclientRef = zitadelclientRefArg;
this.data = dataArg;
2024-05-02 10:52:21 +00:00
}
2024-05-02 15:43:31 +00:00
public async addRole(projectRole: ZitadelProjectRole) {
2024-05-03 08:39:24 +00:00
const response = await this.zitadelclientRef.managementClient.addUserGrant({
2024-05-02 15:43:31 +00:00
userId: this.data.id,
roleKeys: [projectRole.data.key],
projectId: projectRole.data.project.data.id,
});
}
2024-05-03 08:39:24 +00:00
/**
* 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,
});
}
/**
* deletes the user
*/
public async delete() {
const response = await this.zitadelclientRef.userClient.deleteUser({
userId: this.data.id,
});
}
2024-05-02 10:52:21 +00:00
}