Skip to content

Commit

Permalink
Merge branch 'refactor-repos'
Browse files Browse the repository at this point in the history
  • Loading branch information
broneks committed Nov 25, 2024
2 parents d75f92c + a66e864 commit 5f4b448
Show file tree
Hide file tree
Showing 80 changed files with 1,730 additions and 1,412 deletions.
23 changes: 23 additions & 0 deletions api/helper/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package helper

import (
"fmt"
"strings"
)

func ExtractTokenString(authHeader string) (string, error) {
if authHeader == "" {
return "", nil
}

if strings.HasPrefix(authHeader, "Bearer ") {
tokenString := strings.TrimPrefix(authHeader, "Bearer ")

if tokenString == "" {
return "", fmt.Errorf("token is missing")
}

}

return "", fmt.Errorf("invalid authorization header format")
}
100 changes: 0 additions & 100 deletions api/jwtoken/service.go

This file was deleted.

7 changes: 4 additions & 3 deletions api/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package middleware
import (
"log/slog"
"net/http"
"piccolo/api/jwtoken"
"piccolo/api/helper"
"piccolo/api/service/jwtservice"
"piccolo/api/types"

"github.com/labstack/echo/v4"
Expand All @@ -22,7 +23,7 @@ func getAccesssTokenString(c echo.Context) (string, error) {
}

// fallback to using the auth header
tokenString, err := jwtoken.ExtractTokenString(c.Request().Header.Get("Authorization"))
tokenString, err := helper.ExtractTokenString(c.Request().Header.Get("Authorization"))
if err != nil {
slog.Debug(err.Error())
return "", err
Expand All @@ -39,7 +40,7 @@ func Auth() echo.MiddlewareFunc {
slog.Error(err.Error())
}

isAuthenticated := jwtoken.VerifyToken(tokenString)
isAuthenticated := jwtservice.VerifyToken(tokenString)
if !isAuthenticated {
return c.JSON(http.StatusUnauthorized, types.SuccessRes{
Success: false,
Expand Down
5 changes: 3 additions & 2 deletions api/middleware/can_read_shared_album.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"log/slog"
"net/http"
"piccolo/api/helper"
"piccolo/api/repo"
"piccolo/api/repo/sharedalbumrepo"

"github.com/labstack/echo/v4"
)

func CanReadSharedAlbum(sharedAlbumRepo *repo.SharedAlbumRepo) echo.MiddlewareFunc {
func CanReadSharedAlbum(sharedAlbumRepo *sharedalbumrepo.SharedAlbumRepo) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
albumId := helper.GetIdParam(c)
Expand All @@ -19,6 +19,7 @@ func CanReadSharedAlbum(sharedAlbumRepo *repo.SharedAlbumRepo) echo.MiddlewareFu

ctx := c.Request().Context()
readAccessHash := c.QueryParam("share")

canRead, err := sharedAlbumRepo.CanReadSharedAlbum(ctx, albumId, readAccessHash)
if err != nil {
slog.Debug(err.Error())
Expand Down
4 changes: 2 additions & 2 deletions api/middleware/can_reset_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package middleware
import (
"net/http"
"piccolo/api/consts"
"piccolo/api/jwtoken"
"piccolo/api/service/jwtservice"
"piccolo/api/types"

"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -35,7 +35,7 @@ func CanResetPassword(server *types.Server) echo.MiddlewareFunc {
return echo.NewHTTPError(http.StatusForbidden)
}

isAuthenticated := jwtoken.VerifyToken(tokenString)
isAuthenticated := jwtservice.VerifyToken(tokenString)
if !isAuthenticated {
return echo.NewHTTPError(http.StatusForbidden)
}
Expand Down
6 changes: 3 additions & 3 deletions api/middleware/set_user_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package middleware
import (
"fmt"
"log/slog"
"piccolo/api/jwtoken"
"piccolo/api/service/jwtservice"

"github.com/labstack/echo/v4"
)
Expand All @@ -22,8 +22,8 @@ func SetUserData() echo.MiddlewareFunc {
}

if tokenString != "" {
userId := jwtoken.GetUserId(tokenString)
userEmail := jwtoken.GetUserEmail(tokenString)
userId := jwtservice.GetUserId(tokenString)
userEmail := jwtservice.GetUserEmail(tokenString)

c.Set("userId", userId)
c.Set("userEmail", userEmail)
Expand Down
4 changes: 2 additions & 2 deletions api/page/handle_reset_password_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package page
import (
"fmt"
"net/http"
"piccolo/api/service"
"piccolo/api/service/authservice"
"unicode/utf8"

"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -35,7 +35,7 @@ func handleGetResetPasswordPage() echo.HandlerFunc {
}
}

func handlePostResetPasswordPage(authService *service.AuthService) echo.HandlerFunc {
func handlePostResetPasswordPage(authService *authservice.AuthService) echo.HandlerFunc {
return func(c echo.Context) error {
ctx := c.Request().Context()
token := c.FormValue("token")
Expand Down
4 changes: 2 additions & 2 deletions api/page/handle_shared_album_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net/http"
"piccolo/api/helper"
"piccolo/api/model"
"piccolo/api/repo"
"piccolo/api/repo/sharedalbumrepo"
"piccolo/api/types"
"sync"

Expand All @@ -18,7 +18,7 @@ type SharedAlbumPayload struct {
Photos []*model.PhotoWithUrl
}

func handleSharedAlbumPage(server *types.Server, sharedAlbumRepo *repo.SharedAlbumRepo) echo.HandlerFunc {
func handleSharedAlbumPage(server *types.Server, sharedAlbumRepo *sharedalbumrepo.SharedAlbumRepo) echo.HandlerFunc {
return func(c echo.Context) error {
ctx := c.Request().Context()

Expand Down
11 changes: 6 additions & 5 deletions api/page/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package page

import (
"piccolo/api/middleware"
"piccolo/api/repo"
"piccolo/api/service"
"piccolo/api/repo/sharedalbumrepo"
"piccolo/api/repo/userrepo"
"piccolo/api/service/authservice"
"piccolo/api/types"

"github.com/labstack/echo/v4"
)

func Routes(e *echo.Echo, server *types.Server) {
userRepo := repo.NewUserRepo(server.DB)
sharedAlbumRepo := repo.NewSharedAlbumRepo(server.DB)
userRepo := userrepo.New(server.DB)
sharedAlbumRepo := sharedalbumrepo.New(server.DB)

authService := service.NewAuthService(server, userRepo)
authService := authservice.New(server, userRepo)

resetPassword := e.Group("/reset-password", middleware.CanResetPassword(server))

Expand Down
Loading

0 comments on commit 5f4b448

Please sign in to comment.