Compare commits

...

3 Commits

4 changed files with 20 additions and 2 deletions

View File

@ -287,9 +287,11 @@ func (a AccountAPI) passwordResetConfirm(jc jape.Context) {
}
func (a AccountAPI) ping(jc jape.Context) {
token := middleware.GetAuthTokenFromContext(jc.Request.Context())
account.EchoAuthCookie(jc, a.Name())
jc.Encode(&PongResponse{
Ping: "pong",
Ping: "pong",
Token: token,
})
}

View File

@ -45,7 +45,8 @@ type PasswordResetVerifyRequest struct {
}
type PongResponse struct {
Ping string `json:"ping"`
Ping string `json:"ping"`
Token string `json:"token"`
}
type AccountInfoResponse struct {
ID uint `json:"id"`

View File

@ -253,6 +253,8 @@ components:
properties:
ping:
type: string
token:
type: string
AccountInfoResponse:
type: object
properties:

View File

@ -16,6 +16,7 @@ import (
)
const DEFAULT_AUTH_CONTEXT_KEY = "user_id"
const AUTH_TOKEN_CONTEXT_KEY = "auth_token"
type JapeMiddlewareFunc func(jape.Handler) jape.Handler
type HttpMiddlewareFunc func(http.Handler) http.Handler
@ -154,6 +155,7 @@ func AuthMiddleware(options AuthMiddlewareOptions) func(http.Handler) http.Handl
}
ctx := context.WithValue(r.Context(), options.AuthContextKey, uint(userId))
ctx = context.WithValue(r.Context(), AUTH_TOKEN_CONTEXT_KEY, authToken)
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
@ -192,6 +194,17 @@ func GetUserFromContext(ctx context.Context, key ...string) uint {
return userId
}
func GetAuthTokenFromContext(ctx context.Context) string {
authToken, ok := ctx.Value(AUTH_TOKEN_CONTEXT_KEY).(string)
if !ok {
panic("auth token stored in context is not of type string")
}
return authToken
}
func CtxAborted(ctx context.Context) bool {
select {
case <-ctx.Done():