JWT Authentication Setup

Share this video with your friends

Send Tweet

Setting up a login on the angular app and an authentication endpoint on the server.

Kent C. Dodds
Kent C. Dodds(instructor)
~ 10 years ago

So I'm not certain what trouble you're having, but in my example, the handleError function is not attached to the scope (and certainly not $rootScope). The reason it works is because the second argument to a promise's .then function is a function that is called on an error. Good luck!

adr
adr
~ 10 years ago

Sorry I just deleted the post after finding my error and before your answer :/ It was a noob error from my part ^^ I had like form /form with the labels after... I was going to fast when coding cause you're really fast lol Thanks anyway, really good serie !

Kent C. Dodds
Kent C. Dodds(instructor)
~ 10 years ago

Glad you got it figured out! Thanks!

David Mansy
David Mansy
~ 10 years ago

Hi Small detail. With the illustrated code, I had a node error in the server console: Error: Can't set headers after they are sent.

This happened when you deal with a usern/pwd error case, you trigger a res.end but at the end of the function res.send is anyway executed.

To solve that I've just added 'else if' and 'else' in the code of the 'authenticate' function: ''' if (!body.username || !body.password) { res.status(400).end('Must provide username and password!'); } else if (body.username !== user.username || body.password !== user.password) { res.status(401).end('Username or password is incorrect.'); } else { next(); } '''

A
A
~ 10 years ago

Dear Kent Thank you for your course. You are very good developer and your content is very good but I am very frustrated that I cannot type fast like you and please review Lukas Ruebbelke course format. I was able to finish his 2 courses very fast. Please change something because for this reason I am avoiding Joe Eames classes on pluralsite. You really good in what you do but please change something. I really want to learn from you and there is no speed control. Also at the begging of each lesson explain what are you trying to do.

Kent C. Dodds
Kent C. Dodds(instructor)
~ 10 years ago

Thanks for the compliment and feedback.

A
A
~ 10 years ago

Kent - I want to thank you for your class. It helped me a lot in my last project (last 5 days). I reviewed it even 2 nd time. Keep doing what you doing!

Carlos Andres
Carlos Andres
~ 9 years ago

Which would be a better practice to send username and password? I'm sending it via headers in this way: authorizationHeader = "Basic " + btoa(email + ":" + passphrase); JWT could serve for this purpose ? how?

Kent C. Dodds
Kent C. Dodds(instructor)
~ 9 years ago

That's not how I do it in this series. And I wouldn't recommend doing it that way. You don't want your JWT to contain sensitive information because it can be decoded without the private key.

Robert
Robert
~ 9 years ago

function authenticate(req, res, next) { var body = req.body;

console.log(body.username);

not getting the value says "undefined."

inlightmedia
inlightmedia
~ 8 years ago

In server.js, if the authentication "UTIL function" responds to the request by sending a 400 or 401 it continues to next() and tries to set the response header again at line 23 res.send(user) and results in an error in the console stating that you "Can't set headers after they are sent." How can this be avoided. I could conditionally call next() only if there were no error headers sent but I get the feeling I'm doing more than I should need to and I'm missing something.