all repos

onasty @ 40bb8b9

a one-time notes service
5 files changed, 59 insertions(+), 0 deletions(-)
feat: /me (#132)

* feat: add /me route

* test: test /me
Author: Smirnov Oleksandr ss2316544@gmail.com
Committed by: GitHub noreply@github.com
Committed at: 2025-06-09 18:12:42 +0300
Parent: e6c5d1c
M e2e/apiv1_auth_test.go
···
        2
        2
         

      
        3
        3
         import (

      
        4
        4
         	"net/http"

      
        
        5
        +	"time"

      
        5
        6
         

      
        6
        7
         	"github.com/gofrs/uuid/v5"

      
        7
        8
         	"github.com/olexsmir/onasty/internal/hasher"

      ···
        435
        436
         	)

      
        436
        437
         

      
        437
        438
         	e.Equal(httpResp.Code, http.StatusBadRequest)

      
        
        439
        +}

      
        
        440
        +

      
        
        441
        +type getMeResponse struct {

      
        
        442
        +	Email     string    `json:"email"`

      
        
        443
        +	CreatedAt time.Time `json:"created_at"`

      
        
        444
        +}

      
        
        445
        +

      
        
        446
        +func (e *AppTestSuite) TestApiV1_getMe() {

      
        
        447
        +	email := e.uuid() + "@test.com"

      
        
        448
        +	_, toks := e.createAndSingIn(email, "password")

      
        
        449
        +

      
        
        450
        +	httpResp := e.httpRequest(http.MethodGet, "/api/v1/me", nil, toks.AccessToken)

      
        
        451
        +

      
        
        452
        +	e.Equal(httpResp.Code, http.StatusOK)

      
        
        453
        +

      
        
        454
        +	var body getMeResponse

      
        
        455
        +	e.readBodyAndUnjsonify(httpResp.Body, &body)

      
        
        456
        +

      
        
        457
        +	e.Equal(email, body.Email)

      
        
        458
        +	e.NotZero(body.CreatedAt)

      
        438
        459
         }

      
        439
        460
         

      
        440
        461
         // createAndSingIn creates an activated user, logs them in,

      
M internal/dtos/user.go
···
        39
        39
         	Access  string

      
        40
        40
         	Refresh string

      
        41
        41
         }

      
        
        42
        +

      
        
        43
        +type UserInfo struct {

      
        
        44
        +	Email     string

      
        
        45
        +	CreatedAt time.Time

      
        
        46
        +}

      
M internal/service/usersrv/usersrv.go
···
        26
        26
         	RefreshTokens(ctx context.Context, refreshToken string) (dtos.Tokens, error)

      
        27
        27
         	Logout(ctx context.Context, userID uuid.UUID, refreshToken string) error

      
        28
        28
         	LogoutAll(ctx context.Context, userID uuid.UUID) error

      
        
        29
        +	GetUserInfo(ctx context.Context, userID uuid.UUID) (dtos.UserInfo, error)

      
        29
        30
         

      
        30
        31
         	ChangePassword(ctx context.Context, userID uuid.UUID, inp dtos.ChangeUserPassword) error

      
        31
        32
         	RequestPasswordReset(ctx context.Context, inp dtos.RequestResetPassword) error

      ···
        165
        166
         

      
        166
        167
         func (u *UserSrv) LogoutAll(ctx context.Context, userID uuid.UUID) error {

      
        167
        168
         	return u.sessionstore.DeleteAllByUserID(ctx, userID)

      
        
        169
        +}

      
        
        170
        +

      
        
        171
        +func (u *UserSrv) GetUserInfo(ctx context.Context, userID uuid.UUID) (dtos.UserInfo, error) {

      
        
        172
        +	user, err := u.userstore.GetByID(ctx, userID)

      
        
        173
        +	if err != nil {

      
        
        174
        +		return dtos.UserInfo{}, err

      
        
        175
        +	}

      
        
        176
        +

      
        
        177
        +	return dtos.UserInfo{

      
        
        178
        +		Email:     user.Email,

      
        
        179
        +		CreatedAt: user.CreatedAt,

      
        
        180
        +	}, nil

      
        168
        181
         }

      
        169
        182
         

      
        170
        183
         func (u *UserSrv) RefreshTokens(ctx context.Context, rtoken string) (dtos.Tokens, error) {

      
M internal/transport/http/apiv1/apiv1.go
···
        54
        54
         		}

      
        55
        55
         	}

      
        56
        56
         

      
        
        57
        +	r.GET("/me", a.authorizedMiddleware, a.getMeHandler)

      
        
        58
        +

      
        57
        59
         	note := r.Group("/note")

      
        58
        60
         	{

      
        59
        61
         		note.GET("/:slug", a.getNoteBySlugHandler)

      
M internal/transport/http/apiv1/auth.go
···
        262
        262
         		RefreshToken: tokens.Refresh,

      
        263
        263
         	})

      
        264
        264
         }

      
        
        265
        +

      
        
        266
        +type getMeResponse struct {

      
        
        267
        +	Email     string    `json:"email"`

      
        
        268
        +	CreatedAt time.Time `json:"created_at"`

      
        
        269
        +}

      
        
        270
        +

      
        
        271
        +func (a *APIV1) getMeHandler(c *gin.Context) {

      
        
        272
        +	uinfo, err := a.usersrv.GetUserInfo(c.Request.Context(), a.getUserID(c))

      
        
        273
        +	if err != nil {

      
        
        274
        +		errorResponse(c, err)

      
        
        275
        +		return

      
        
        276
        +	}

      
        
        277
        +

      
        
        278
        +	c.JSON(http.StatusOK, getMeResponse{

      
        
        279
        +		Email:     uinfo.Email,

      
        
        280
        +		CreatedAt: uinfo.CreatedAt,

      
        
        281
        +	})

      
        
        282
        +}