/
session.go
53 lines (37 loc) · 1.06 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package authenticate
import (
"log"
"net/http"
"os"
"github.com/EngineerKamesh/gofullstack/volume2/section6/gopherfaceauth/models"
"github.com/gorilla/sessions"
)
var SessionStore *sessions.FilesystemStore
func CreateUserSession(u *models.User, sessionID string, w http.ResponseWriter, r *http.Request) error {
gfSession, err := SessionStore.Get(r, "gopherface-session")
if err != nil {
log.Print(err)
}
gfSession.Values["sessionID"] = sessionID
gfSession.Values["username"] = u.Username
gfSession.Values["firstName"] = u.FirstName
gfSession.Values["lastName"] = u.LastName
gfSession.Values["email"] = u.Email
err = gfSession.Save(r, w)
if err != nil {
log.Print(err)
return err
}
return nil
}
func ExpireUserSession(w http.ResponseWriter, r *http.Request) {
gfSession, err := SessionStore.Get(r, "gopherface-session")
if err != nil {
log.Print(err)
}
gfSession.Options.MaxAge = -1
gfSession.Save(r, w)
}
func init() {
SessionStore = sessions.NewFilesystemStore("/tmp/gopherface-sessions", []byte(os.Getenv("GOPHERFACE_HASH_KEY")))
}