feat(api): create template
Add template table to the database Create controller function to check if user has template, and create it in the database Made universal jwt.Claims of user data retrieval function
This commit is contained in:
@@ -6,9 +6,16 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt"
|
||||
)
|
||||
|
||||
type UserClaims struct {
|
||||
Id float64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
func GenerateJWT(u *user.User) (string, error) {
|
||||
// Generate JWT token
|
||||
mySigningKey := []byte(config.Env["JWT_SECRET"])
|
||||
@@ -53,3 +60,23 @@ func ParseJWT(tokenString string) (jwt.MapClaims, error) {
|
||||
// Return on invalid token
|
||||
return nil, fmt.Errorf("invalid token")
|
||||
}
|
||||
|
||||
func GetUser(c *gin.Context) (UserClaims, error) {
|
||||
// Get user from context
|
||||
user, ok := c.Get("user")
|
||||
if !ok {
|
||||
return UserClaims{}, fmt.Errorf("no user in middleware context")
|
||||
}
|
||||
|
||||
// Get claims from user
|
||||
mapClaims, ok := user.(jwt.MapClaims)
|
||||
if !ok {
|
||||
return UserClaims{}, fmt.Errorf("invalid token claims")
|
||||
}
|
||||
|
||||
return UserClaims{
|
||||
Id: mapClaims["id"].(float64),
|
||||
Name: mapClaims["name"].(string),
|
||||
Email: mapClaims["email"].(string),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Success(c *gin.Context, data gin.H) {
|
||||
func Success(c *gin.Context, data any) {
|
||||
// Return success to api
|
||||
c.JSON(200, gin.H{
|
||||
"success": true,
|
||||
|
||||
Reference in New Issue
Block a user