-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
main.go
51 lines (37 loc) · 1 KB
/
main.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
package main
import (
"net/http"
"github.com/kataras/go-sessions/v3"
)
var (
cookieNameForSessionID = "mycookiesessionnameid"
sess = sessions.New(sessions.Config{Cookie: cookieNameForSessionID})
)
func secret(w http.ResponseWriter, r *http.Request) {
// Check if user is authenticated
if auth, _ := sess.Start(w, r).GetBoolean("authenticated"); !auth {
w.WriteHeader(http.StatusForbidden)
return
}
// Print secret message
w.Write([]byte("The cake is a lie!"))
}
func login(w http.ResponseWriter, r *http.Request) {
session := sess.Start(w, r)
// Authentication goes here
// ...
// Set user as authenticated
session.Set("authenticated", true)
}
func logout(w http.ResponseWriter, r *http.Request) {
session := sess.Start(w, r)
// Revoke users authentication
session.Set("authenticated", false)
}
func main() {
app := http.NewServeMux()
app.HandleFunc("/secret", secret)
app.HandleFunc("/login", login)
app.HandleFunc("/logout", logout)
http.ListenAndServe(":8080", app)
}