-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
37 lines (31 loc) · 761 Bytes
/
utils.go
File metadata and controls
37 lines (31 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v4"
"gorm.io/gorm"
)
//GetDatabase returns database from gin context.
func GetDatabase(ctx *gin.Context) *gorm.DB {
return ctx.MustGet("DB").(*gorm.DB)
}
type CustomClaims struct {
Role string `json:"role"`
jwt.RegisteredClaims
}
func GetToken(id uint, role string, time time.Time) (string, error) {
claims := &CustomClaims{
role,
jwt.RegisteredClaims{
Subject: fmt.Sprintf("%d", id),
ExpiresAt: jwt.NewNumericDate(time),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString([]byte("secret_key"))
}
func NewSuccessResponse[T any](ctx *gin.Context, data T) {
ctx.JSON(http.StatusOK, data)
}