Skip to content

Commit

Permalink
Add Content-Type header to response
Browse files Browse the repository at this point in the history
  • Loading branch information
Sowri19 committed Apr 8, 2024
1 parent bc40cfe commit 9d2d862
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions controller/authController.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const signup = async (req, res) => {
const cognitoUser = await user.signup();
const response = {
statusCode: 200,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
success: true,
message: "User signed up successfully",
Expand All @@ -19,6 +20,7 @@ export const signup = async (req, res) => {
} catch (error) {
const response = {
statusCode: 400,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
success: false,
message: "Signup failed",
Expand All @@ -38,15 +40,13 @@ export const confirmSignup = async (req, res) => {
await user.confirmSignup(verificationCode);
const tokens = await user.authenticate();

// Set the JWT token as a cookie in the response
const response = {
statusCode: 200,
headers: {
"Set-Cookie": [
`token=${tokens.idToken}; HttpOnly; Max-Age=${
30 * 24 * 60 * 60
}; Secure; Path=/`,
],
"Content-Type": "application/json",
"Set-Cookie": `token=${tokens.idToken}; HttpOnly; Max-Age=${
30 * 24 * 60 * 60
}; Secure; Path=/`,
},
body: JSON.stringify({
success: true,
Expand All @@ -57,9 +57,9 @@ export const confirmSignup = async (req, res) => {

return response;
} catch (error) {
console.log(error);
const response = {
statusCode: 400,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
success: false,
message: "Failed to confirm signup",
Expand All @@ -78,15 +78,13 @@ export const login = async (req, res) => {
try {
const tokens = await user.authenticate();

// Set the JWT token as a cookie in the response
const response = {
statusCode: 200,
headers: {
"Set-Cookie": [
`token=${tokens.idToken}; HttpOnly; Max-Age=${
30 * 24 * 60 * 60
}; Secure; Path=/`,
],
"Content-Type": "application/json",
"Set-Cookie": `token=${tokens.idToken}; HttpOnly; Max-Age=${
30 * 24 * 60 * 60
}; Secure; Path=/`,
},
body: JSON.stringify({
success: true,
Expand All @@ -99,6 +97,7 @@ export const login = async (req, res) => {
} catch (error) {
const response = {
statusCode: 401,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
success: false,
message: "Authentication failed",
Expand All @@ -109,7 +108,6 @@ export const login = async (req, res) => {
return response;
}
};

export const changePassword = async (req, res) => {
const { email, oldPassword, newPassword } = req.body;
const user = new User(email, oldPassword);
Expand All @@ -118,6 +116,7 @@ export const changePassword = async (req, res) => {
const result = await user.changePassword(oldPassword, newPassword);
const response = {
statusCode: 200,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
success: true,
message: "Password changed successfully",
Expand All @@ -129,6 +128,7 @@ export const changePassword = async (req, res) => {
} catch (error) {
const response = {
statusCode: 400,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
success: false,
message: "Failed to change password",
Expand All @@ -139,7 +139,6 @@ export const changePassword = async (req, res) => {
return response;
}
};

export const updateEmail = async (req, res) => {
const { email, newEmail, password } = req.body;
const user = new User(email, password);
Expand All @@ -148,6 +147,7 @@ export const updateEmail = async (req, res) => {
const result = await user.updateEmail(newEmail);
const response = {
statusCode: 200,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
success: true,
message: "Email updated successfully",
Expand All @@ -159,6 +159,7 @@ export const updateEmail = async (req, res) => {
} catch (error) {
const response = {
statusCode: 400,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
success: false,
message: "Failed to update email",
Expand All @@ -177,11 +178,11 @@ export const logout = async (req, res) => {
try {
user.logout();

// Clear the JWT token cookie in the response
const response = {
statusCode: 200,
headers: {
"Set-Cookie": [`token=; HttpOnly; Max-Age=0; Secure; Path=/`],
"Content-Type": "application/json",
"Set-Cookie": "token=; HttpOnly; Max-Age=0; Secure; Path=/",
},
body: JSON.stringify({
success: true,
Expand All @@ -193,6 +194,7 @@ export const logout = async (req, res) => {
} catch (error) {
const response = {
statusCode: 400,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
success: false,
message: "Failed to log out",
Expand Down

0 comments on commit 9d2d862

Please sign in to comment.