Experiment with Authentication and Websockets
Some checks failed
build-docker-imge / Build the docker container (push) Failing after 2m24s
Some checks failed
build-docker-imge / Build the docker container (push) Failing after 2m24s
This commit is contained in:
41
middleware/auth.go
Normal file
41
middleware/auth.go
Normal file
@ -0,0 +1,41 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.kocoder.xyz/kocoded/szuntis-backend/repository"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type AuthMiddleware struct {
|
||||
ar repository.AuthRepository
|
||||
}
|
||||
|
||||
func NewAuthMiddleware(ar repository.AuthRepository) AuthMiddleware {
|
||||
return AuthMiddleware{
|
||||
ar: ar,
|
||||
}
|
||||
}
|
||||
|
||||
func (am AuthMiddleware) IsAuthenticated(ctx *fiber.Ctx) error {
|
||||
val := ctx.Cookies("session-token")
|
||||
if val == "" {
|
||||
fmt.Println(1)
|
||||
return ctx.SendStatus(fiber.StatusUnauthorized)
|
||||
}
|
||||
token, err := uuid.Parse(val)
|
||||
if err != nil {
|
||||
fmt.Println(2)
|
||||
return ctx.SendStatus(fiber.StatusUnauthorized)
|
||||
}
|
||||
|
||||
u, err := am.ar.GetUserByToken(ctx.Context(), token)
|
||||
if err != nil {
|
||||
fmt.Println(3)
|
||||
return ctx.SendStatus(fiber.StatusUnauthorized)
|
||||
}
|
||||
fmt.Println(4)
|
||||
ctx.Locals("user", u)
|
||||
return ctx.Next()
|
||||
}
|
20
middleware/ws.go
Normal file
20
middleware/ws.go
Normal file
@ -0,0 +1,20 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/gofiber/contrib/websocket"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type WsMiddleware struct {
|
||||
}
|
||||
|
||||
func NewWsMiddleware() WsMiddleware {
|
||||
return WsMiddleware{}
|
||||
}
|
||||
|
||||
func (wm WsMiddleware) IsWebscoketUpgrade(ctx *fiber.Ctx) error {
|
||||
if websocket.IsWebSocketUpgrade(ctx) {
|
||||
return ctx.Next()
|
||||
}
|
||||
return ctx.SendStatus(fiber.StatusUpgradeRequired)
|
||||
}
|
Reference in New Issue
Block a user