clndr/api/controllers/session.go

38 lines
860 B
Go

package controllers
import (
"magmise/models"
"net/http"
"os"
"github.com/wader/gormstore/v2"
)
var (
// key must be 16, 24 or 32 bytes long (AES-128, AES-192 or AES-256)
key = []byte("super-secret-key")
store = gormstore.NewOptions(models.DB(), gormstore.Options{}, []byte(os.Getenv("HASHKEY")), []byte("BLOCKKEY"))
)
func Login(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, "session")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
// Authentication goes here
// ...
// Set user as authenticated
session.Values["user"] = "test"
session.Values["authenticated"] = true
session.Save(r, w)
}
func Logout(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "session")
// Revoke users authentication
session.Values["authenticated"] = false
session.Save(r, w)
}