Skip to content

Commit

Permalink
added middleware and authorizatoin
Browse files Browse the repository at this point in the history
  • Loading branch information
prosenjitjoy committed Oct 6, 2023
1 parent 375edbc commit fb946b5
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '^1.20'

- name: Install golang-migrate
run: |
Expand Down
15 changes: 13 additions & 2 deletions api/account.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package api

import (
"errors"
"fmt"
"main/database/db"
"main/token"
"net/http"

"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5"
)

type createAccountRequest struct {
Owner string `json:"owner" binding:"required"`
Currency string `json:"currency" binding:"required,currency"`
}

Expand All @@ -21,8 +22,10 @@ func (s *Server) createAccount(ctx *gin.Context) {
return
}

authPayload := ctx.MustGet(authorizationPayloadKey).(*token.Payload)

arg := db.CreateAccountParams{
Owner: req.Owner,
Owner: authPayload.Username,
Balance: 0,
Currency: req.Currency,
}
Expand Down Expand Up @@ -62,6 +65,12 @@ func (s *Server) getAcount(ctx *gin.Context) {
return
}

authPayload := ctx.MustGet(authorizationPayloadKey).(*token.Payload)
if account.Owner != authPayload.Username {
err := errors.New("account doesn't belong to the authenticated user")
ctx.JSON(http.StatusUnauthorized, errorResponse(err))
return
}
ctx.JSON(http.StatusOK, account)
}

Expand All @@ -77,7 +86,9 @@ func (s *Server) listAcount(ctx *gin.Context) {
return
}

authPayload := ctx.MustGet(authorizationPayloadKey).(*token.Payload)
arg := &db.ListAccountsParams{
Owner: authPayload.Username,
Limit: req.PageSize,
Offset: (req.PageID - 1) * req.PageSize,
}
Expand Down
Loading

0 comments on commit fb946b5

Please sign in to comment.