Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
returning tokens instead of dealing with Cookies directly
Browse files Browse the repository at this point in the history
  • Loading branch information
MEZ901 committed Mar 31, 2024
1 parent 0d59c4b commit 46e949f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 33 deletions.
25 changes: 4 additions & 21 deletions src/adapters/controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ class AuthController {
confirmPassword,
});

if (result?.jwt) {
res.cookie("access_token", result.jwt.accessToken, { httpOnly: true });
res.cookie("refresh_token", result.jwt.refreshToken, { httpOnly: true });
}

res.status(result.status).json(result.data);
};

Expand All @@ -38,33 +33,21 @@ class AuthController {
password,
});

if (result?.jwt) {
res.cookie("access_token", result.jwt.accessToken, { httpOnly: true });
res.cookie("refresh_token", result.jwt.refreshToken, { httpOnly: true });
}

res.status(result.status).json(result.data);
};

logout = async (req, res) => {
const { refresh_token } = req.cookies;
const { refreshToken } = req.body;

const result = await this.logoutUseCase.execute(refresh_token);

res.clearCookie("access_token");
res.clearCookie("refresh_token");
const result = await this.logoutUseCase.execute(refreshToken);

res.status(result.status).json(result.data);
};

refreshToken = async (req, res) => {
const { refresh_token } = req.cookies;

const result = await this.refreshTokenUseCase.execute(refresh_token);
const { refreshToken } = req.body;

if (result.jwt) {
res.cookie("access_token", result.jwt.accessToken, { httpOnly: true });
}
const result = await this.refreshTokenUseCase.execute(refreshToken);

res.status(result.status).json(result.data);
};
Expand Down
8 changes: 4 additions & 4 deletions src/application/usecases/LoginUseCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class LoginUseCase {

return {
status: 200,
jwt: {
accessToken,
refreshToken,
},
data: {
message: "Login successfully",
jwt: {
accessToken,
refreshToken,
},
user,
},
};
Expand Down
6 changes: 3 additions & 3 deletions src/application/usecases/RefreshTokenUseCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class RefreshTokenUseCase {

return {
status: 200,
jwt: {
accessToken,
},
data: {
message: "Access token has been refreshed successfully!",
jwt: {
accessToken,
},
},
};
};
Expand Down
8 changes: 4 additions & 4 deletions src/application/usecases/RegisterUseCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class RegisterUseCase {

return {
status: 200,
jwt: {
accessToken,
refreshToken,
},
data: {
message: "Register successfully",
jwt: {
accessToken,
refreshToken,
},
user,
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/webserver/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Server {
configureCors = () => {
this.app.use(
cors({
origin: "http://localhost:3000",
origin: "*",
credentials: true,
})
);
Expand Down

0 comments on commit 46e949f

Please sign in to comment.