Repository navigation
Expand file tree
/
Copy pathmiddleware.go
More file actions
69 lines (60 loc) · 1.92 KB
/
Copy pathmiddleware.go
File metadata and controls
69 lines (60 loc) · 1.92 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package frank
import (
"context"
"net/http"
"github.com/danielgtaylor/huma/v2"
"github.com/xraph/frank/pkg/errors"
// "github.com/xraph/frank/pkg/utils"
)
func (f *Frank) AuthMiddleware() func(http.Handler) http.Handler {
// authmw := middleware.AuthGoa(f.cfg, f.log, f.Services.Session, f.Services.SessionStore, f.Services.APIKey, f.Services.CookieHandler)
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// ctx, err := authmw.AuthWithOptionsGoa()(r.Context(), "", nil)
// if err != nil {
// utils.RespondError(w, err)
// return
// }
// r = r.WithContext(newCurrenUserID(ctx))
// next.ServeHTTP(w, r)
})
}
}
func (f *Frank) AuthMiddlewareHuma(api huma.API) func(ctx huma.Context, next func(huma.Context)) {
// authmw := middleware.AuthGoa(f.cfg, f.log, f.Services.Session, f.Services.SessionStore, f.Services.APIKey, f.Services.CookieHandler)
return func(ctx huma.Context, next func(huma.Context)) {
// // Wrap the context to add a value.
// octx, err := authmw.AuthWithOptionsGoa()(ctx.Context(), "", nil)
// if err != nil {
// huma.WriteErr(api, ctx, http.StatusUnauthorized,
// "unauthorized", err,
// )
// return
// }
//
// id, ok := middleware.GetUserID(octx)
// if !ok {
// err = errors.New(errors.CodeUnauthorized, "user id not found")
// huma.WriteErr(api, ctx, http.StatusNotFound,
// "user id not found", err,
// )
// }
// ctx = huma.WithValue(ctx, userCtxKey{}, id)
next(ctx)
}
}
type userCtxKey struct{}
func CurrenUserID(ctx context.Context) (string, error) {
id, ok := ctx.Value(userCtxKey{}).(string)
if !ok {
return "", errors.New(errors.CodeUnauthorized, "user id not found")
}
return id, nil
}
func newCurrenUserID(ctx context.Context) context.Context {
// id, ok := middleware.GetUserID(ctx)
// if ok {
// ctx = context.WithValue(ctx, userCtxKey{}, id)
// }
return ctx
}