Skip to content

Commit

Permalink
fix: store token in header
Browse files Browse the repository at this point in the history
  • Loading branch information
Mone19 committed Jun 21, 2024
1 parent 1497805 commit 8a78e58
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
3 changes: 1 addition & 2 deletions api/auth/controllers/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ export const signup = async (req, res, next) => {
process.env.JWT_SECRET
);

res.status(201).cookie('access_token', token, {
httpOnly: true}).json({ message: "Anmeldung erfolgreich." });
res.status(201).json({ token, message: "Anmeldung erfolgreich." });
} catch (err) {
if (err.code === 11000) {
if (err.keyPattern.username) {
Expand Down
4 changes: 3 additions & 1 deletion api/post/utils/verifyUser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import jwt from "jsonwebtoken";
import { errorHandler } from "./error.js";
export const verifyToken = (req, res, next) => {
const token = req.cookies.access_token;
if ( req.headers.authorization && req.headers.authorization.startsWith("Bearer")) {
const token = req.headers.authorization.split(" ")[1];
}
if (!token) {
return next(errorHandler(401, "Unauthorized"));
}
Expand Down
1 change: 1 addition & 0 deletions client/src/pages/CreatePost.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default function CreatePost() {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${localStorage.getItem('token')}`
},
body: JSON.stringify(formData),
});
Expand Down
4 changes: 1 addition & 3 deletions client/src/pages/SignIn.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function SignIn() {
const res = await fetch(`${baseUrl}/api/auth/signin`, {
method: "POST",
headers: {
"Content-Type": "application/json"
"Content-Type": "application/json",
},
body: JSON.stringify(formData),
});
Expand All @@ -46,8 +46,6 @@ export default function SignIn() {
dispatch(signInFailure(data.message));
}
if (res.ok) {

localStorage.setItem('token', data.token);
dispatch(signInSuccess(data));
localStorage.setItem('token', data.token);
navigate("/");
Expand Down

0 comments on commit 8a78e58

Please sign in to comment.