From 3e5468f260142881406ecdf5505b81ec27974472 Mon Sep 17 00:00:00 2001 From: kwa0x2 Date: Tue, 28 May 2024 01:43:57 +0300 Subject: [PATCH] Create README.md --- README.md | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..84545b9 --- /dev/null +++ b/README.md @@ -0,0 +1,101 @@ +# Go Redis Session API Example + +This project includes Docker installation for Redis and uses gin-contrib/sessions for Redis session management. The project contains functionality for setting session, clearing session, and authentication middleware. + +## Installation: + +### 1. Start the services using Docker Compose: + +```bash +docker-compose up -d +``` + +### 2. Install the Go dependencies: + +```bash +go mod download +``` + +### 3. Start the project with Air for hot reloading: + +```bash +air +``` + +## Examples: + +### Set Session + +```go +// SetSessionExample sets a user ID and email in the session and saves it. +func SetSessionExample(ctx *gin.Context) { + session := sessions.Default(ctx) + + // Generate a new UUID for the user and set it in the session + session.Set("user_id", uuid.New().String()) + session.Set("user_email", "example@nettasec.com") + err := session.Save() + if err != nil { + // Return an internal server error if the session could not be saved + ctx.JSON(http.StatusInternalServerError, gin.H{ + "error":err.Error(), + }) + return + } + + // Return a success message with the user ID and email from the session + ctx.JSON(http.StatusOK, gin.H{ + "message":"Successfully logged in", + "user_id":session.Get("user_id"), + "user_email":session.Get("user_email"), + }) +} +``` + +### Clear Session + +```go +// ClearSessionExample clears the session and deletes the session cookie. +func ClearSessionExample(ctx *gin.Context) { + session := sessions.Default(ctx) + + // Clear all session data and set the session's max age to -1 (delete it) + session.Clear() + session.Options(sessions.Options{MaxAge: -1}) + session.Save() + + // Delete the session cookie + ctx.SetCookie("connect.sid","",-1,"/","localhost",true,true) + + // Return a success message indicating the session has been cleared + ctx.JSON(http.StatusOK, gin.H{ + "message":"Successfully cleared. Please go to the authentication endpoint for testing.", + }) +} + +``` + +### Auth Middleware + +```go +func SessionAuthMiddleware() gin.HandlerFunc { + return func(ctx *gin.Context) { + // Get the default session for the current context + session := sessions.Default(ctx) + // Get the user ID from the session + sessionUserID := session.Get("user_id") + // If the user ID is nil (meaning no user is logged in), return an unauthorized status and message + if sessionUserID == nil { + ctx.JSON(http.StatusUnauthorized, gin.H{ + "message": "Authorization failed", + }) + // Abort the request processing since the user is not authorized + ctx.Abort() + } + // Set the expiration time for the session to 24 hours from now + session.Set("Expires", time.Now().Add(24*time.Hour)) + // Save the session + session.Save() + } +} +```