mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-17 03:06:42 -07:00
chore: initial public commit
This commit is contained in:
commit
fc9054b78c
250 changed files with 32809 additions and 0 deletions
41
engine/handlers/lbz_validate.go
Normal file
41
engine/handlers/lbz_validate.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gabehf/koito/engine/middleware"
|
||||
"github.com/gabehf/koito/internal/db"
|
||||
"github.com/gabehf/koito/internal/logger"
|
||||
"github.com/gabehf/koito/internal/utils"
|
||||
)
|
||||
|
||||
type LbzValidateResponse struct {
|
||||
Code int `json:"code"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Valid bool `json:"valid,omitempty"`
|
||||
UserName string `json:"user_name,omitempty"`
|
||||
}
|
||||
|
||||
func LbzValidateTokenHandler(store db.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
l := logger.FromContext(ctx)
|
||||
l.Debug().Msg("Validating user token...")
|
||||
|
||||
u := middleware.GetUserFromContext(ctx)
|
||||
var response LbzValidateResponse
|
||||
if u == nil {
|
||||
response.Code = http.StatusUnauthorized
|
||||
response.Error = "Incorrect Authorization"
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
utils.WriteJSON(w, http.StatusOK, response)
|
||||
} else {
|
||||
response.Code = 200
|
||||
response.Message = "Token valid."
|
||||
response.Valid = true
|
||||
response.UserName = u.Username
|
||||
utils.WriteJSON(w, http.StatusOK, response)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue