Files
FernandoJVideira c4ee0ce4dc chore: rename simple_bank to api_tmpl and clean up for template use
Rename the module, proto service, and all identifiers from simple_bank/
SimpleBank to api_tmpl/ApiTmpl, and regenerate the gRPC/gateway/swagger/
mock code accordingly.

- Scrub real secrets (SMTP password, auth token secret) from api.env/
  api_test.env, untrack them, and add api.env.example/api_test.env.example
- Remove committed build artifacts (tmp/main, tmp/build-errors.log,
  dbml-error.log) and gitignore them going forward
- Fix Dockerfile paths (build target, migrations copy) that referenced a
  stale project layout
- Rewrite README to match the actual cmd/ + internal/ structure and add a
  "using this as a template" section
2026-08-25 03:02:37 +01:00

75 lines
1.9 KiB
Go

package api
import (
"errors"
"net/http"
"strings"
"time"
db "github.com/FernandoJVideira/api_tmpl/internal/db/sqlc"
"github.com/FernandoJVideira/api_tmpl/internal/handlers/http/response"
"github.com/FernandoJVideira/api_tmpl/internal/service"
"github.com/gin-gonic/gin"
)
type renewAccessTokenRequest struct {
RefreshToken string `json:"refresh_token" binding:"required"`
}
type renewAccessTokenResponse struct {
AccessToken string `json:"access_token"`
AccessTokenExpiresAt time.Time `json:"access_token_expires_at"`
}
func (s *Server) renewAccessToken(ctx *gin.Context) {
var req renewAccessTokenRequest
var resp response.JSONResponse
if err := ctx.ShouldBindJSON(&req); err != nil {
resp = response.JSONResponse{
Success: false,
Error: err.Error(),
}
ctx.JSON(http.StatusBadRequest, resp)
return
}
// Call auth service - all business logic moved there
result, err := s.authService.RenewAccessToken(ctx, service.TokenRenewalRequest{
RefreshToken: req.RefreshToken,
})
if err != nil {
statusCode := http.StatusInternalServerError
errMsg := err.Error()
// Map service errors to HTTP codes
if errors.Is(err, db.ErrRecordNotFound) || strings.Contains(errMsg, "session not found") {
statusCode = http.StatusNotFound
} else if strings.Contains(errMsg, "invalid") ||
strings.Contains(errMsg, "blocked") ||
strings.Contains(errMsg, "expired") ||
strings.Contains(errMsg, "mismatched") {
statusCode = http.StatusUnauthorized
}
resp = response.JSONResponse{
Success: false,
Error: errMsg,
}
ctx.JSON(statusCode, resp)
return
}
resp = response.JSONResponse{
Success: true,
Message: "Access token renewed successfully",
Data: renewAccessTokenResponse{
AccessToken: result.AccessToken,
AccessTokenExpiresAt: result.AccessTokenExpiresAt,
},
}
ctx.JSON(http.StatusOK, resp)
}