
Lesson Description
The "Testing the Authentication Routes" Lesson is part of the full, Complete Go for Professional Developers course featured in this preview video. Here's what you'd learn in this lesson:
Melkey tests the authentication routes with two cURL commands. The first request creates a new user. After creating the new user, a second request tests the "/tokens/authentication" route with the username and password. A successful username/password combination returns the newly created token. An invalid combination returns an error. See commit 3.2 in the repo for the current code.
Transcript from the "Testing the Authentication Routes" Lesson
[00:00:00]
>> Melkey: Time for us to check if this works. Let's go back to our running Go server. Run it down and go run main.go. Is there any obvious errors? No? That's good. You can see it's updated to the current version of four, which is great. Okay, if we look at p s q l, select we'll do a /dt.
[00:00:18]
We now see we have our tokens table right here. If we do select all from tokens, we have nothing there. If I quickly double check select all from users, you can see we have one user, John Doe. I'm actually going to create a new user called Jack_, not to be confused with John_Doe, but just to demonstrate that, you just can't create an account and then we can use that account to authenticate.
[00:00:44]
So you go back into the post_notes.txt, you should see under section 3.2, we actually have two sets of code requests. So here we can take this one, which would be the usual, which creates A new user, right? So I'm gonna go ahead and copy this. I'm gonna paste it right here into my terminal.
[00:01:07]
So now I have this new user, id 2. Username, jack_doe, jack.doe@example.com, we can check again, select all from users. There they are, john doe and jack doe, very nice. And if you go back into the POST notes and take the second curl request, which is going to be to /token/authentication.
[00:01:31]
So if you go ahead and grab this and you paste it here, make sure that the username is, in fact, jack_doe. If you don't wanna use that, just make sure matches the username of the user you've created or the other user you have in your database. Make sure the passwords match as well.
[00:01:46]
And if this works, you should see now we are able to generate an authentication token for our user Jack Doe. And if you want, we can be a little bit more let's try, let's see what if this is 124 right? Invalid credentials. So there's ways that we can validate that this does in fact work, but now we can guarantee that our username Jack Doe has a token.
[00:02:08]
And if we double check in PSQL, if we do select all from tokens, you can now see Jackdaw has a hash token Token. His user ID is 2. We don't really know who that is. The expiry is 24 hours from now, and the scope is authentication.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops