reset password (test)
This commit is contained in:
parent
54554d140e
commit
25aeef2448
2 changed files with 42 additions and 3 deletions
|
|
@ -4,6 +4,8 @@ const KC_URL = process.env.KC_URL;
|
|||
const KC_REALMS = process.env.KC_REALMS;
|
||||
const KC_CLIENT_ID = process.env.KC_SERVICE_ACCOUNT_CLIENT_ID;
|
||||
const KC_SECRET = process.env.KC_SERVICE_ACCOUNT_SECRET;
|
||||
const AUTH_ACCOUNT_SECRET = process.env.AUTH_ACCOUNT_SECRET
|
||||
const API_KEY = process.env.API_KEY
|
||||
|
||||
let token: string | null = null;
|
||||
let decoded: DecodedJwt | null = null;
|
||||
|
|
@ -765,9 +767,31 @@ export async function changeUserPassword(userId: string, newPassword: string) {
|
|||
// Function to reset password
|
||||
export async function resetPassword(username: string) {
|
||||
try {
|
||||
if (!API_KEY || !AUTH_ACCOUNT_SECRET) {
|
||||
throw new Error("KC_CLIENT_ID and KC_SECRET are required to used this feature.");
|
||||
}
|
||||
const body = new URLSearchParams();
|
||||
body.append("client_id", "gettoken");
|
||||
body.append("client_secret", AUTH_ACCOUNT_SECRET?.toString());
|
||||
body.append("grant_type", "client_credentials");
|
||||
const tokenResponse = await fetch(`${process.env.KC_URL}/realms/${process.env.KC_REALMS}/protocol/openid-connect/token`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
api_key: API_KEY,
|
||||
},
|
||||
body: body
|
||||
});
|
||||
if (!tokenResponse.ok) {
|
||||
throw new Error("Failed to get admin token");
|
||||
}
|
||||
const tokenData = await tokenResponse.json();
|
||||
const adminToken = tokenData.access_token;
|
||||
|
||||
const users = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users?email=${encodeURIComponent(username)}`, {
|
||||
headers: {
|
||||
"authorization": `Bearer ${await getToken()}`,
|
||||
// "authorization": `Bearer ${await getToken()}`,
|
||||
"authorization": `Bearer ${adminToken}`,
|
||||
"content-type": `application/json`,
|
||||
},
|
||||
});
|
||||
|
|
@ -779,7 +803,8 @@ export async function resetPassword(username: string) {
|
|||
const resetResponse = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/execute-actions-email`, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Authorization": `Bearer ${await getToken()}`,
|
||||
// "Authorization": `Bearer ${await getToken()}`,
|
||||
"Authorization": `Bearer ${adminToken}`,
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(["UPDATE_PASSWORD"])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue