

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Authentication With Passwords
incomplete
2: Password Review
incomplete
3: Types of Authentication
incomplete
4: JWTs
incomplete
5: Authentication With JWTs
incomplete
6: JWT Review
incomplete
7: Revoking JWTs
incomplete
8: Refresh Tokens
incomplete
9: Cookies
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
There are several different ways to handle authentication. We'll use JWTs in this course. They're a popular choice for APIs that are consumed by web applications and mobile apps.
A JWT is a JSON Web Token. It's a cryptographically signed JSON object that contains information about the user. You'll learn about how the cryptography of JWTs work in our Learn Cryptography course, for now, it's just important to know that once the token is created by the server, the data in the token can't be changed without the server knowing.
When your server issues a JWT to Bob, Bob can use that token to make requests as Bob to your API. Bob won't be able to change the token to make requests as Alice.
The first building blocks you'll write are the functions for creating and validating JWTs, which will be used in the next lesson to authenticate users.
func MakeJWT(userID uuid.UUID, tokenSecret string, expiresIn time.Duration) (string, error)
Create and return a JWT using this JWT library, which you can import into your code by running:
go get -u github.com/golang-jwt/jwt/v5
func ValidateJWT(tokenString, tokenSecret string) (uuid.UUID, error)
If all is well with the token, use the token.Claims interface to get access to the user's id from the claims (which should be stored in the Subject field). Return the id as a uuid.UUID.
Run and submit the CLI tests.