-
Notifications
You must be signed in to change notification settings - Fork 4
/
auth.go
41 lines (30 loc) · 847 Bytes
/
auth.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
package middleware
import (
"net/http"
"strings"
"go-echo-starter/store"
"github.com/labstack/echo/v4"
)
func AuthMiddleware(as store.AuthStore, us store.UserStore) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
reqToken := c.Request().Header.Get(echo.HeaderAuthorization)
splitToken := strings.Split(reqToken, "Bearer: ")
if len(splitToken) < 2 {
return c.JSON(http.StatusUnauthorized, nil)
}
uid, err := as.VerifyIdToken(splitToken[1])
if err != nil {
return c.JSON(http.StatusUnauthorized, nil)
}
user, err := us.FindByUid(uid)
if err != nil {
return c.JSON(http.StatusInternalServerError, nil)
} else if user == nil {
return c.JSON(http.StatusForbidden, nil)
}
c.Set("user", user)
return next(c)
}
}
}