feat: add api to fetch user profile data

This commit is contained in:
Sosokker 2025-02-14 08:03:35 +07:00
parent dd94c24918
commit 8f6488a70d
5 changed files with 103 additions and 1 deletions

View File

@ -82,6 +82,7 @@ func (a *api) Routes() *chi.Mux {
api.UseMiddleware(m.AuthMiddleware(api)) api.UseMiddleware(m.AuthMiddleware(api))
a.registerHelloRoutes(r, api) a.registerHelloRoutes(r, api)
a.registerFarmRoutes(r, api) a.registerFarmRoutes(r, api)
a.registerUserRoutes(r, api)
}) })
return router return router

View File

@ -0,0 +1,62 @@
package api
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/danielgtaylor/huma/v2"
"github.com/forfarm/backend/internal/domain"
"github.com/forfarm/backend/internal/utilities"
"github.com/go-chi/chi/v5"
)
func (a *api) registerUserRoutes(_ chi.Router, api huma.API) {
tags := []string{"user"}
prefix := "/user"
huma.Register(api, huma.Operation{
OperationID: "getSelfData",
Method: http.MethodGet,
Path: prefix + "/me",
Tags: tags,
}, a.getSelfData)
}
type getSelfDataInput struct {
Authorization string `header:"Authorization" required:"true" example:"Bearer token"`
}
type getSelfDataOutput struct {
Body struct {
User domain.User `json:"user"`
}
}
func (a *api) getSelfData(ctx context.Context, input *getSelfDataInput) (*getSelfDataOutput, error) {
resp := &getSelfDataOutput{}
authHeader := input.Authorization
if authHeader == "" {
return nil, fmt.Errorf("no authorization header provided")
}
authToken := strings.TrimPrefix(authHeader, "Bearer ")
if authToken == "" {
return nil, fmt.Errorf("no token provided")
}
uuid, err := utilities.ExtractUUIDFromToken(authToken)
if err != nil {
return nil, err
}
user, err := a.userRepo.GetByUUID(ctx, uuid)
if err != nil {
return nil, err
}
resp.Body.User = user
return resp, nil
}

View File

@ -51,6 +51,7 @@ func (u *User) Validate() error {
type UserRepository interface { type UserRepository interface {
GetByID(context.Context, int64) (User, error) GetByID(context.Context, int64) (User, error)
GetByUUID(context.Context, string) (User, error)
GetByUsername(context.Context, string) (User, error) GetByUsername(context.Context, string) (User, error)
GetByEmail(context.Context, string) (User, error) GetByEmail(context.Context, string) (User, error)
CreateOrUpdate(context.Context, *User) error CreateOrUpdate(context.Context, *User) error

View File

@ -60,6 +60,22 @@ func (p *postgresUserRepository) GetByID(ctx context.Context, id int64) (domain.
return users[0], nil return users[0], nil
} }
func (p *postgresUserRepository) GetByUUID(ctx context.Context, uuid string) (domain.User, error) {
query := `
SELECT id, uuid, username, password, email, created_at, updated_at, is_active
FROM users
WHERE uuid = $1`
users, err := p.fetch(ctx, query, uuid)
if err != nil {
return domain.User{}, err
}
if len(users) == 0 {
return domain.User{}, domain.ErrNotFound
}
return users[0], nil
}
func (p *postgresUserRepository) GetByUsername(ctx context.Context, username string) (domain.User, error) { func (p *postgresUserRepository) GetByUsername(ctx context.Context, username string) (domain.User, error) {
query := ` query := `
SELECT id, uuid, username, password, email, created_at, updated_at, is_active SELECT id, uuid, username, password, email, created_at, updated_at, is_active

View File

@ -8,7 +8,6 @@ import (
"github.com/golang-jwt/jwt/v5" "github.com/golang-jwt/jwt/v5"
) )
// TODO: Change later
var deafultSecretKey = []byte(config.JWT_SECRET_KEY) var deafultSecretKey = []byte(config.JWT_SECRET_KEY)
func CreateJwtToken(uuid string) (string, error) { func CreateJwtToken(uuid string) (string, error) {
@ -52,3 +51,26 @@ func VerifyJwtToken(tokenString string, customKey ...[]byte) error {
return nil return nil
} }
// ExtractUUIDFromToken decodes the JWT token using the default secret key,
// and returns the uuid claim contained within the token.
func ExtractUUIDFromToken(tokenString string) (string, error) {
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, jwt.ErrSignatureInvalid
}
return deafultSecretKey, nil
})
if err != nil {
return "", err
}
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
if uuid, ok := claims["uuid"].(string); ok {
return uuid, nil
}
return "", errors.New("uuid not found in token")
}
return "", errors.New("invalid token claims")
}