Compare commits
1 commit
dev
...
auth-uploa
Author | SHA1 | Date | |
---|---|---|---|
![]() |
a3312ef6f8 |
4 changed files with 31 additions and 6 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
.env
|
.env
|
||||||
tmp
|
tmp
|
||||||
*.db
|
*.db
|
||||||
|
imgs
|
|
@ -4,22 +4,36 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/golang-jwt/jwt/v5"
|
||||||
|
"stereo.cat/backend/internal/auth"
|
||||||
"stereo.cat/backend/internal/types"
|
"stereo.cat/backend/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RegisterUploadRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
|
func RegisterUploadRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
|
||||||
api.POST("/upload", func(c *gin.Context) {
|
api.POST("/upload", auth.JwtMiddleware(cfg.JWTSecret), func(c *gin.Context) {
|
||||||
|
claims := c.MustGet("claims").(jwt.MapClaims)
|
||||||
|
user := claims["user"].(auth.User)
|
||||||
|
|
||||||
|
uid := user.ID
|
||||||
|
|
||||||
|
if uid == "" {
|
||||||
|
c.JSON(401, gin.H{"error": "unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
file, err := c.FormFile("file")
|
file, err := c.FormFile("file")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, gin.H{"error": "file is required"})
|
c.JSON(400, gin.H{"error": "file is required"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
filePath := filepath.Join(cfg.ImagePath, file.Filename)
|
filePath := filepath.Join(cfg.ImagePath, uid, file.Filename)
|
||||||
if err := c.SaveUploadedFile(file, filePath); err != nil {
|
if err := c.SaveUploadedFile(file, filePath); err != nil {
|
||||||
c.JSON(500, gin.H{"error": "failed to save file"})
|
c.JSON(500, gin.H{"error": "failed to save file"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.JSON(200, gin.H{"message": "file uploaded successfully"})
|
||||||
})
|
})
|
||||||
|
|
||||||
api.GET("/:name", func(c *gin.Context) {
|
api.GET("/:name", func(c *gin.Context) {
|
||||||
|
|
|
@ -11,9 +11,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateJWT(key string, user User, expiryTimestamp uint64) (string, error) {
|
func GenerateJWT(key string, user User, expiryTimestamp uint64) (string, error) {
|
||||||
claims := jwt.MapClaims{
|
claims := Claims{
|
||||||
"user": user,
|
User: user,
|
||||||
"exp": expiryTimestamp,
|
Exp: expiryTimestamp,
|
||||||
}
|
}
|
||||||
|
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
package auth
|
package auth
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/golang-jwt/jwt/v5"
|
||||||
|
)
|
||||||
|
|
||||||
type TokenResponse struct {
|
type TokenResponse struct {
|
||||||
AccessToken string `json:"access_token"`
|
AccessToken string `json:"access_token"`
|
||||||
|
@ -28,3 +32,9 @@ type ExchangeCodeRequest struct {
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
RedirectUri string `json:"redirect_uri"`
|
RedirectUri string `json:"redirect_uri"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Claims struct {
|
||||||
|
User User `json:"user"`
|
||||||
|
Exp uint64 `json:"exp"`
|
||||||
|
jwt.RegisteredClaims
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue