GithubHelp home page GithubHelp logo

masagatech / echo-session Goto Github PK

View Code? Open in Web Editor NEW

This project forked from piggyman007/echo-session

0.0 0.0 0.0 30 KB

Package echo-session is a middleware that provides session support for echo.

License: Apache License 2.0

Go 100.00%

echo-session's Introduction

echo-session

Go Report Card GoDoc

Middleware echo-session is a session support for echo.

This version is working with echo v3. Please checkout v2 branch if you want use session with echo v2.

Installation

go get github.com/masagatech/echo-session

Example

package main

import (
	"net/http"

	"github.com/labstack/echo"
	"github.com/piggyman007/echo-session"
)

func main() {
	e := echo.New()
	store, _ := session.NewRedisStore(32, "tcp", "localhost:6379", "", []byte("secret")) // set redis store
	opts := session.Options{
		MaxAge:   300,                  // sesstion timeout in seconds
		Secure:   false,                // secure cookie flag
		HttpOnly: true,                 // httponly flag
		SameSite: http.SameSiteLaxMode, // samesite flag
	}
	store.Options(opts)

	e.Use(session.Sessions("EDSESSION", store)) // EDSESSION is the cookie name

	// e.g., http://localhost:8082/login?username=user2&userId=2
	e.GET("/login", func(c echo.Context) error {
		username := c.QueryParam("username")
		userId := c.QueryParam("userId")

		if username != "" && userId != "" {
			session := session.Default(c)
			session.Set("username", username) // save session data
			session.Set("userId", userId)     // save session data
			session.Save()                    // save session
			sessionId := session.GetID()      // get sessionId, need to call GetID() after session.Save()
			return c.JSON(200, map[string]interface{}{
				"msg":       "Login success",
				"sessionId": sessionId,
			})
		}

		return c.JSON(401, map[string]interface{}{
			"msg": "Unauthorized",
		})
	})

	// e.g., http://localhost:8082/profile
	e.GET("/profile", func(c echo.Context) error {
		session := session.Default(c)
		if session.IsNew() {
			return c.JSON(400, map[string]interface{}{
				"msg": "Bad Request",
			})
		}

		username := session.Get("username") // get session data
		userId := session.Get("userId")     // get session data
		session.Save()                      // reset sesstion TTL

		return c.JSON(200, map[string]interface{}{
			"username": username,
			"userId":   userId,
			"sessinId": session.GetID(),
		})
	})

	e.Logger.Fatal(e.Start(":8082"))
}

License

This project is under Apache v2 License. See the LICENSE file for the full license text.

echo-session's People

Contributors

ipfans avatar piggyman007 avatar iannsp avatar gebv avatar jamsmendez avatar etiennera avatar zenwerk avatar mineofcode avatar ijust avatar carynova avatar shogo-ma avatar bbq-all-stars avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.