mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-12 05:06:55 +00:00
reset password link
This commit is contained in:
28
apps/login/src/app/api/resetpassword/route.ts
Normal file
28
apps/login/src/app/api/resetpassword/route.ts
Normal 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 });
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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} />
|
||||
|
||||
Reference in New Issue
Block a user