Let's build a secure API that gives access to clients only after logging in. In the process, we will define three endpoints:
- /login
- /logout
- /healthcheck
/healthcheck is the data API, but it first has to log in using the /login endpoint. Our API should reject all unauthenticated requests. Create a project directory called simpleAuth, like this:
mkdir -p $GOPATH/src/github.com/git-user/chapter14/simpleAuthtouch $GOPATH/src/github.com/git-user/chapter14/simpleAuth/main.go
In the program, we can see how to enable session-based authentication to API endpoints using the gorilla/ sessions package. Follow these steps:
- We need imports for our program. The main ones are mux and sessions, as seen in the following code ...