forked from viliproject/vili
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
45 lines (40 loc) · 932 Bytes
/
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
package middleware
import (
"net/http"
"strings"
"github.com/airware/vili/session"
"gopkg.in/labstack/echo.v1"
)
// Session logs the user in using the configured session service
func Session() echo.MiddlewareFunc {
return func(h echo.HandlerFunc) echo.HandlerFunc {
return func(c *echo.Context) error {
if !strings.HasPrefix(c.Request().URL.Path, "/admin/") {
user, err := session.GetUser(c.Request())
if err != nil {
return err
}
if user != nil {
c.Set("user", user)
}
}
if err := h(c); err != nil {
c.Error(err)
}
return nil
}
}
}
// RequireUser redirects to the login page if the user is not logged in
func RequireUser(h echo.HandlerFunc) echo.HandlerFunc {
return func(c *echo.Context) error {
if c.Get("user") == nil {
c.Redirect(http.StatusTemporaryRedirect, "/login")
return nil
}
if err := h(c); err != nil {
c.Error(err)
}
return nil
}
}