reset password link

This commit is contained in:
peintnermax
2024-07-15 17:20:38 +02:00
parent bf88446d90
commit 985d22b193
3 changed files with 84 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
import { setEmail, server, listUsers, passwordReset } from "@/lib/zitadel";
import { NextRequest, NextResponse } from "next/server";
export async function POST(request: NextRequest) {
const body = await request.json();
if (body) {
const { loginName, organization } = body;
return listUsers(loginName, organization).then((users) => {
if (
users.details &&
users.details.totalResult == 1 &&
users.result[0].userId
) {
const userId = users.result[0].userId;
return passwordReset(server, userId)
.then((resp) => {
return NextResponse.json(resp);
})
.catch((error) => {
return NextResponse.json(error, { status: 500 });
});
} else {
return NextResponse.json({ error: "User not found" }, { status: 404 });
}
});
}
}

View File

@@ -469,6 +469,28 @@ export async function setEmail(
);
}
/**
*
* @param server
* @param userId the id of the user where the email should be set
* @returns the newly set email
*/
export async function passwordReset(
server: ZitadelServer,
userId: string,
): Promise<any> {
const userservice = user.getUser(server);
return userservice.passwordReset(
{
userId,
sendLink: {
notificationType: 1, // email
},
},
{},
);
}
/**
*
* @param server

View File

@@ -76,6 +76,33 @@ export default function PasswordForm({
return response;
}
async function resetPassword() {
setError("");
setLoading(true);
const res = await fetch("/api/resetpassword", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
loginName,
organization,
authRequestId,
}),
});
const response = await res.json();
setLoading(false);
if (!res.ok) {
console.log(response.details.details);
setError(response.details?.details ?? "Could not verify password");
return Promise.reject(response.details);
}
return response;
}
function submitPasswordAndContinue(value: Inputs): Promise<boolean | void> {
return submitPassword(value).then((resp) => {
// if user has mfa -> /otp/[method] or /u2f
@@ -201,6 +228,13 @@ export default function PasswordForm({
label="Password"
// error={errors.username?.message as string}
/>
<button
className="transition-all text-sm hover:text-primary-light-500 dark:hover:text-primary-dark-500"
onClick={() => resetPassword()}
disabled={loading}
>
Reset Password
</button>
{loginName && (
<input type="hidden" name="loginName" value={loginName} />