Skip to content

Commit

Permalink
resset password fix
Browse files Browse the repository at this point in the history
  • Loading branch information
docimin committed Oct 26, 2023
1 parent f7b16ac commit 15af13b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
8 changes: 4 additions & 4 deletions app/api/user/editUser/[uniqueId]/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function PUT(request) {
// Assume the last segment of the URL is the user ID
const userId = request.url.split("/").pop();

const requestBody = await request.json();
const requestBody = await request.text();

if (!userId) {
throw new Error("User ID is required");
Expand All @@ -22,16 +22,16 @@ export async function PUT(request) {
Authorization: `Bearer ${process.env.DOMAIN_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify(requestBody),
body: requestBody,
});

if (!response.ok) {
throw new Error("Failed to update data");
}

const data = await response.json();
return NextResponse.json(data);
return NextResponse.json(data, { status: 200 });
} catch (error) {
return NextResponse.error(500, error.message);
return NextResponse.json(error.message, { status: 500 });
}
}
6 changes: 3 additions & 3 deletions app/api/user/editUserData/[uniqueId]/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ export async function PUT(request) {
Authorization: `Bearer ${process.env.DOMAIN_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify(requestBody),
body: requestBody,
});

if (!response.ok) {
throw new Error("Failed to update data");
}

const data = await response.json();
return NextResponse.json(data);
return NextResponse.json(data, { status: 200 });
} catch (error) {
return NextResponse.error(500, error.message);
return NextResponse.json(error.message, { status: 500 });
}
}
20 changes: 15 additions & 5 deletions app/api/user/editUserPassword/route.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { NextResponse } from "next/server";
import { cookies } from "next/headers";

export const runtime = "edge";

export async function POST(request) {
try {
const requestBody = await request.json();

if (!userId) {
throw new Error("User ID is required");
const cookieStore = cookies();
const jwtCookie = cookieStore.get("jwt");

if (!jwtCookie) {
return NextResponse.json({ error: "No JWT Token" }, { status: 403 });
}

// Construct the URL for the external fetch
Expand All @@ -16,19 +20,25 @@ export async function POST(request) {
const response = await fetch(fetchURL, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.DOMAIN_API_KEY}`,
Authorization: `Bearer ${jwtCookie.value}`,
"Content-Type": "application/json",
},
body: JSON.stringify(requestBody),
});

if (response.status === 400) {
const data = await response.json();
return NextResponse.json(data, { status: 400 });
}

if (!response.ok) {
throw new Error("Failed to update data");
}

const data = await response.json();
return NextResponse.json(data);
return NextResponse.json(data, { status: 200 });
} catch (error) {
return NextResponse.error(500, error.message);
console.log(error);
return NextResponse.json(error.message, { status: 500 });
}
}
4 changes: 2 additions & 2 deletions app/api/user/resetPassword/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export async function POST(request) {
}

const data = await response.json();
return NextResponse.json(data);
return NextResponse.json(data, { status: 200 });
} catch (error) {
return NextResponse.error(500, error.message);
return NextResponse.json(error.message, { status: 500 });
}
}
16 changes: 11 additions & 5 deletions components/account/accountPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ export default function AccountPage() {
const handleSubmit = async (event) => {
event.preventDefault();

const userFormData = new FormData();
let userFormData = {};

if (email.value) {
userFormData.append("email", email.value);
userFormData.email = email.value;
}

if (username_login.value) {
userFormData.append("username", username_login.value);
userFormData.username = username_login.value;
}

try {
Expand All @@ -56,7 +56,10 @@ export default function AccountPage() {

const userResponseUpdate = await fetch(`/api/user/editUser/${userId}`, {
method: "PUT",
body: userFormData,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(userFormData),
});

if (userResponseUpdate.status === 200) {
Expand Down Expand Up @@ -91,7 +94,10 @@ export default function AccountPage() {
}),
});

if (response.ok) {
if (response.status === 400) {
const data = await response.json();
alert(data.error.message);
} else if (response.ok) {
alert("Password reset successful");
} else {
alert("Password reset failed");
Expand Down

0 comments on commit 15af13b

Please sign in to comment.