guildgate/session.go

38 lines
790 B
Go

package main
import "net/http"
func setSession(uname string, res http.ResponseWriter) {
value := map[string]string{
"name": uname,
}
if encoded, err := cookieHandler.Encode("session", value); err == nil {
cookie := &http.Cookie{
Name: "session",
Value: encoded,
Path: "/",
}
http.SetCookie(res, cookie)
}
}
func getUserName(req *http.Request) (uname string) {
if cookie, err := req.Cookie("session"); err == nil {
cookieValue := make(map[string]string)
if err = cookieHandler.Decode("session", cookie.Value, &cookieValue); err == nil {
uname = cookieValue["name"]
}
}
return uname
}
func clearSession(res http.ResponseWriter) {
cookie := &http.Cookie{
Name: "session",
Value: "",
Path: "/",
MaxAge: -1,
}
http.SetCookie(res, cookie)
}