Adding authentication to our API
Now that we understand the basic concepts of authentication and authorization, it is time to use them in our application. The first thing that we need to do is to create the new data structures that we are going to need.
We are going to need User and Session structures, so let’s build those data structures:
type User struct {
Role string
Username string
Password string
}
type Session struct {
Expires time.Time
Username string
}
You can see here that we are already defining a way to identify the user of the session and its expiration time. Also, you can see that the user has a Role field to determine the role in the system. That’s all we need for now to handle sessions and users.
Also, we need login request data. In our case, we need a username string and a password string:
type LoginRequest struct {
Username string `json: "username"`
Password string `json: "password"`
}
We have...