mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-11 21:22:14 +00:00
fix: axios, user, zitadel
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Page } from "@playwright/test";
|
||||
import fetch from "node-fetch";
|
||||
import axios from "axios";
|
||||
import { registerWithPasskey } from "./register";
|
||||
import { getUserByUsername, removeUser } from "./zitadel";
|
||||
|
||||
@@ -40,29 +40,31 @@ class User {
|
||||
},
|
||||
};
|
||||
|
||||
const response = await fetch(process.env.ZITADEL_API_URL! + "/v2/users/human", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(body),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: "Bearer " + process.env.ZITADEL_SERVICE_USER_TOKEN!,
|
||||
},
|
||||
});
|
||||
if (response.statusCode >= 400 && response.statusCode != 409) {
|
||||
const error = "HTTP Error: " + response.statusCode + " - " + response.statusMessage;
|
||||
console.error(error);
|
||||
throw new Error(error);
|
||||
try {
|
||||
const response = await axios.post(`${process.env.ZITADEL_API_URL}/v2/users/human`, body, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${process.env.ZITADEL_SERVICE_USER_TOKEN}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (response.status >= 400 && response.status !== 409) {
|
||||
const error = `HTTP Error: ${response.status} - ${response.statusText}`;
|
||||
console.error(error);
|
||||
throw new Error(error);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error making request:", error);
|
||||
throw error;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
async remove() {
|
||||
const resp = await getUserByUsername(this.getUsername());
|
||||
const resp: any = await getUserByUsername(this.getUsername());
|
||||
if (!resp || !resp.result || !resp.result[0]) {
|
||||
return;
|
||||
}
|
||||
await removeUser(resp.result[0].userId);
|
||||
return;
|
||||
}
|
||||
|
||||
public setUserId(userId: string) {
|
||||
@@ -90,7 +92,7 @@ class User {
|
||||
}
|
||||
|
||||
public getFullName() {
|
||||
return this.props.firstName + " " + this.props.lastName;
|
||||
return `${this.props.firstName} ${this.props.lastName}`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,26 +134,36 @@ export class PasswordUserWithOTP extends User {
|
||||
switch (this.type) {
|
||||
case OtpType.sms:
|
||||
url = url + "sms";
|
||||
break;
|
||||
case OtpType.email:
|
||||
url = url + "email";
|
||||
break;
|
||||
}
|
||||
|
||||
const response = await fetch(process.env.ZITADEL_API_URL! + "/v2/users/" + this.getUserId() + "/" + url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: "Bearer " + process.env.ZITADEL_SERVICE_USER_TOKEN!,
|
||||
},
|
||||
});
|
||||
if (response.statusCode >= 400 && response.statusCode != 409) {
|
||||
const error = "HTTP Error: " + response.statusCode + " - " + response.statusMessage;
|
||||
console.error(error);
|
||||
throw new Error(error);
|
||||
}
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`${process.env.ZITADEL_API_URL}/v2/users/${this.getUserId()}/${url}`,
|
||||
{},
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${process.env.ZITADEL_SERVICE_USER_TOKEN}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
// TODO: get code from SMS or Email provider
|
||||
this.code = "";
|
||||
return;
|
||||
if (response.status >= 400 && response.status !== 409) {
|
||||
const error = `HTTP Error: ${response.status} - ${response.statusText}`;
|
||||
console.error(error);
|
||||
throw new Error(error);
|
||||
}
|
||||
|
||||
// TODO: get code from SMS or Email provider
|
||||
this.code = "";
|
||||
} catch (error) {
|
||||
console.error("Error making request:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public getCode() {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import fetch from "node-fetch";
|
||||
import axios from "axios";
|
||||
|
||||
export async function removeUserByUsername(username: string) {
|
||||
const resp = await getUserByUsername(username);
|
||||
@@ -9,18 +9,22 @@ export async function removeUserByUsername(username: string) {
|
||||
}
|
||||
|
||||
export async function removeUser(id: string) {
|
||||
const response = await fetch(process.env.ZITADEL_API_URL! + "/v2/users/" + id, {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
Authorization: "Bearer " + process.env.ZITADEL_SERVICE_USER_TOKEN!,
|
||||
},
|
||||
});
|
||||
if (response.statusCode >= 400 && response.statusCode != 404) {
|
||||
const error = "HTTP Error: " + response.statusCode + " - " + response.statusMessage;
|
||||
console.error(error);
|
||||
throw new Error(error);
|
||||
try {
|
||||
const response = await axios.delete(`${process.env.ZITADEL_API_URL}/v2/users/${id}`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${process.env.ZITADEL_SERVICE_USER_TOKEN}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (response.status >= 400 && response.status !== 404) {
|
||||
const error = `HTTP Error: ${response.status} - ${response.statusText}`;
|
||||
console.error(error);
|
||||
throw new Error(error);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error making request:", error);
|
||||
throw error;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
export async function getUserByUsername(username: string) {
|
||||
@@ -33,20 +37,24 @@ export async function getUserByUsername(username: string) {
|
||||
},
|
||||
],
|
||||
};
|
||||
const jsonBody = JSON.stringify(listUsersBody);
|
||||
const registerResponse = await fetch(process.env.ZITADEL_API_URL! + "/v2/users", {
|
||||
method: "POST",
|
||||
body: jsonBody,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: "Bearer " + process.env.ZITADEL_SERVICE_USER_TOKEN!,
|
||||
},
|
||||
});
|
||||
if (registerResponse.statusCode >= 400) {
|
||||
const error = "HTTP Error: " + registerResponse.statusCode + " - " + registerResponse.statusMessage;
|
||||
console.error(error);
|
||||
throw new Error(error);
|
||||
|
||||
try {
|
||||
const response = await axios.post(`${process.env.ZITADEL_API_URL}/v2/users`, listUsersBody, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${process.env.ZITADEL_SERVICE_USER_TOKEN}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (response.status >= 400) {
|
||||
const error = `HTTP Error: ${response.status} - ${response.statusText}`;
|
||||
console.error(error);
|
||||
throw new Error(error);
|
||||
}
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error making request:", error);
|
||||
throw error;
|
||||
}
|
||||
const respJson = await registerResponse.json();
|
||||
return respJson;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user