session

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2017 License: MIT Imports: 2 Imported by: 0

README

gear-session

Build Status Coverage Status License GoDoc

Cookie session middleware for Gear, base on github.com/go-http-utils/cookie-session.

Demo

package main

import (
  "fmt"
  "time"

  "github.com/go-http-utils/cookie-session"
  "github.com/teambition/gear"
  "github.com/teambition/gear-session"
)

// Session is a custom session struct.
type Session struct {
  *sessions.Meta `json:"-"`
  UserID         string `json:"_userId"`
  Name           string `json:"name"`
  Email          string `json:"email"`
  Avatar         string `json:"avatar"`
  Authed         int64  `json:"authed"`
}

// Save is helpful method
func (s *Session) Save() error {
  return s.GetStore().Save(s)
}

var gearSession = session.New("Sess", sessions.New(), func() sessions.Sessions {
  return &Session{Meta: &sessions.Meta{}}
})

// FromCtx is helpful function to read session from gear.Context
func FromCtx(ctx *gear.Context) (*Session, error) {
  val, err := ctx.Any(gearSession)
  return val.(*Session), err
}

func main() {
  app := gear.New()
  app.Set(gear.SetKeys, []string{"some key"})

  app.Use(func(ctx *gear.Context) error {
    sess, err := FromCtx(ctx)
    if err != nil {
      fmt.Println(sess.IsNew()) // true
      sess.UserID = "xxxxID"
      sess.Name = "gear"
      sess.Email = "gear@teambition.com"
      sess.Authed = time.Now().Unix()
      sess.Save()
    } else {
      fmt.Println(sess.IsNew()) // true
      fmt.Println(sess)

      // update session
      sess.Avatar = "avatar.png"
      sess.Authed = time.Now().Unix()
      sess.Save()
    }
    return ctx.JSON(200, sess)
  })
  app.Listen(":3000")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GearSession

type GearSession struct {
	// contains filtered or unexported fields
}

GearSession is a useful wrap of sessions.Store and sessions.Sessions

func New

func New(name string, store sessions.Store, sess func() sessions.Sessions) *GearSession

New return a GearSession instance

func (*GearSession) New

func (gs *GearSession) New(ctx *gear.Context) (interface{}, error)

New implements Gear.Any interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL