last day (15 days later) » 

4:26 PM
As the comments are getting too long, probs better for us to discuss further in this chat
 
hi matt you are a guy for this
yeah it says undefined
 
np we'll get to the bottom of it. I'm just re-reading your code again quickly
 
thats the error
```
handleSubmit(e) {
e.preventDefault();
if (this.state.email.length < 8 || this.state.password.length < 8) {
alert(`please enter the form correctly `);
} else {
const data = { email: this.state.email, password: this.state.password };

fetch("/login", {
method: "POST", // or 'PUT'
// request.headers['authorization'] = 'Bearer ' + token;,
headers: {
Accept: "application/json, text/plain, */*",
"Content-Type": "application/json"
},
body: JSON.stringify(data)
})
.then(data => {
console.log("Success:", data);
this is my fetch from the login form handle submit
possibly this has somethign to do with it
 
In that code above, you have request.headers['authorization'] = 'Bearer ' + token; commented out. What's the code where you previously assigned token?
We may as well make it work from react
 
That was what I had but it was throwing errors
Im guessing this is mandatory to contain authorisation in header and you cannot just call upon the cookie ?
 
4:39 PM
It's up to you, there are two approaches to this. The first, in react you can get the JWT from the cookie and then send it as part of the auth header in the request. Likewise you can just scap the auth header altogether and get the JWT directly from the cookie in the backend verify function
 
I guewss the cookie option is the best option ?
I really appreciate all this help . However I leave from work soon .
 
I'd say so, there isn't really a need to re-set it in a header. No worries, this should be fairly simple from now. So, in the verify function, where you currently do: const bearerHeader = req.headers["authorization"]; did you say that when you output req.cookies to console it gives you undefined?
If you go to your chrome dev tools -> Application Tab -> Cookies, I assume you can see the JWT in there?
 
yes it said that it was undefined
thats what i got
 
With your code how it was in the question, where you get a 403, just add console.log(JSON.stringify(req.cookies)) in the verify function and then send me a pic of your console
Just want to see what errors in that first pic are related and which aren't
Oh wait, I see at the top it says undefined
Could you go to the application tab of your browser dev tools and see if the cookie exists in there?
if it doesn't, then the cookie is not being set properly
It's probably also worth mentioning that when you sign your JWT, you set the key to be "secretkey" but when verifying it you use "secret" which will also cause it to fail validation
 
4:57 PM
yep i have a cookie
it has sid
and then a token
s%3AxC1a6d5hue3vdjB9UK7BKAL7ayVuWcpe.kRhOS2rpguqwTgvy0XdcEmWMx6vZXbgCbZel5%2BjaSug
Yeah i spotted that. It is already been changed
I have updated the code for it all to be just secret now
I did that before this convo
How would you feel abou tdoign some react tutoing lol I wil lpay you whatever you think you are worth
 
okay, that's good. So, just to confirm what's in my head now, put this in the verify function console.log('Header cookies: ', JSON.stringify(req.headers.cookie))
and post your console output
 
Header cookies: "connect.sid=s%3AxC1a6d5hue3vdjB9UK7BKAL7ayVuWcpe.kRhOS2rpguqwTgvy0XdcEmWMx6vZXbgCbZel5%2BjaSug"
thats what i have recieved
so at least the header cookie is a thing lol
 
So, it looks like you're not using cookieParser, which means cookies are sent as a URL encoded header. In order to make my original code work with req.cookies['auth'] you need to install this via npm and initialise it in your main express file as it shows here: npmjs.com/package/cookie-parser
then, the original line change in the verify function from const bearerHeader = req.headers["authorization"]; to const bearerHeader = req.cookies['auth'] should work
I'd also change res.cookie("auth", token).json({ token }) in the login function to just be res.cookie("auth", token)
let me know if that works
 
I already have that I believe
unless you ment the other way round
unfortyuantely same issue still
Thanks for everythign you have trie dot do
*tried to do
 
5:19 PM
Hmm, interesting. All I would suggest is that you look into that SyntaxError with the promise which was in your console. If you already have cookieParser correctly set up then something is preventing that cookie from being set correctly. I'd check you don't have any options such as "secure" enabled as that will only work over HTTPS. The first thing I recommend is fixing that SyntaxError. You're almost there, but something sketchy is happening to stop the cookie being read.
Once you're able to successfully output the cookies in the verify function, then the code should work as expected
 
Thanks matt I really appreciate all your help ! if you ever want to make a bit of side £ teaching react let me know as id be happy to pay
have a great evening man !
 

last day (15 days later) »