zitadel/ts/classes.zitadelclient.ts
2024-05-02 14:07:42 +02:00

67 lines
1.8 KiB
TypeScript

import { ZitaldelUser } from './classes.zitadeluser.js';
import * as plugins from './zitadel.plugins.js';
export interface IZitadelContructorOptions {
url: string;
accessToken: string;
}
export class ZitaldelClient {
private options: IZitadelContructorOptions;
public authClient: plugins.zitadel.AuthServiceClient;
public userClient: plugins.zitadel.UserServiceClient;
constructor(optionsArg: IZitadelContructorOptions) {
this.options = optionsArg;
console.log(this.options);
this.authClient = plugins.zitadel.createAuthClient(
this.options.url,
plugins.zitadel.createAccessTokenInterceptor(this.options.accessToken)
);
this.userClient = plugins.zitadel.createUserClient(
this.options.url,
plugins.zitadel.createAccessTokenInterceptor(this.options.accessToken)
);
}
public async listOwnUser() {
const response = await this.authClient.getMyUser({});
const zitadelUser = new ZitaldelUser(this, {
id: response.user.id,
lastLogin: response.lastLogin,
username: response.user.userName,
});
return zitadelUser;
}
public async listUsers() {
const response = await this.userClient.listUsers({});
const returnArray: ZitaldelUser[] = [];
for (const userObject of response.result) {
returnArray.push(new ZitaldelUser(this, {
id: userObject.userId,
username: userObject.username,
lastLogin: null,
}));
}
return returnArray;
}
public async createUser(optionsArg: {
email: string;
firstName: string;
lastName: string;
}) {
const response = await this.userClient.addHumanUser({
email: {
email: optionsArg.email,
},
profile: {
givenName: optionsArg.firstName,
familyName: optionsArg.lastName,
}
});
console.log(response);
}
}