Persisting client sessions and cookies
Up to now, you had to include the Authorization
header on each request. A better solution is to generate a session cookie. Session cookies allow users to be recognized within an application without having to authenticate every time. Without a cookie, every time you issue an API request, the server will treat you like a completely new visitor.
To generate a session cookie, proceed as follows:
- Install Gin middleware for session management with the following command:
go get github.com/gin-contrib/sessions
- Configure Redis as a store for users' sessions with the following code:
store, _ := redisStore.NewStore(10, "tcp", Â Â Â Â Â Â "localhost:6379", "", []byte("secret")) router.Use(sessions.Sessions("recipes_api", store))
Note
Instead of hardcoding the Redis Uniform Resource Identifier (URI), you can use an environment variable. That way, you can keep configuration...