Checkin time data column, checkin module

Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
2025-12-25 02:17:28 +08:00
parent ca08c997c8
commit 396ab10469
6 changed files with 22 additions and 9 deletions

View File

@@ -1,6 +1,10 @@
package data
import "github.com/google/uuid"
import (
"time"
"github.com/google/uuid"
)
type User struct {
Id uint `json:"id" gorm:"primarykey;autoincrement"`
@@ -11,7 +15,7 @@ type User struct {
Nickname string `json:"nickname"`
Subtitle string `json:"subtitle"`
Avatar string `json:"avatar"`
Checkin bool `json:"checkin" gorm:"not null;default:false"`
Checkin time.Time `json:"checkin"`
}
func (self *User) GetByEmail(email string) error {
@@ -28,11 +32,11 @@ func (self *User) GetByUserId(userId string) error {
return nil
}
func (self *User) SetCheckinState(userId uuid.UUID, state bool) error {
func (self *User) SetCheckinState(userId uuid.UUID, time time.Time) error {
if err := Database.Where("user_id = ?", userId).First(&self).Error; err != nil {
return err
}
self.Checkin = state
self.Checkin = time
Database.Save(&self)
return nil
}

View File

@@ -62,7 +62,7 @@ func JWTAuth() gin.HandlerFunc {
}
func GenerateToken(userID uuid.UUID, application string) (string, error) {
var JwtSecret = []byte(viper.GetString("server.jwt_secret"))
var JwtSecret = []byte(viper.GetString("secrets.jwt_secret"))
claims := Claims{
UserID: userID,
RegisteredClaims: jwt.RegisteredClaims{

View File

@@ -2,6 +2,7 @@ package server
import (
"nixcn-cms/service/auth"
"nixcn-cms/service/checkin"
"github.com/gin-gonic/gin"
)
@@ -10,4 +11,5 @@ func Router(e *gin.Engine) {
// API Services
api := e.Group("/api/v1")
auth.Handler(api.Group("/auth"))
checkin.Handler(api.Group("/checkin"))
}

View File

@@ -1,12 +1,13 @@
package auth
import (
"net/http"
"net/url"
"nixcn-cms/data"
"nixcn-cms/internal/crypto/jwt"
"nixcn-cms/pkgs/magiclink"
"nixcn-cms/pkgs/turnstile"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"github.com/gin-gonic/gin"
@@ -68,8 +69,12 @@ func VerifyMagicLink(c *gin.Context) {
}
// Generate jwt
uuid, _ := uuid.NewUUID()
jwtToken, _ := jwt.GenerateToken(uuid, "application")
userInfo := new(data.User)
err := userInfo.GetByEmail(email)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"status": "user not found"})
}
jwtToken, _ := jwt.GenerateToken(userInfo.UserId, "application")
c.JSON(200, gin.H{
"jwt_token": jwtToken,

View File

@@ -3,6 +3,7 @@ package checkin
import (
"net/http"
"nixcn-cms/data"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
@@ -17,7 +18,7 @@ func Checkin(ctx *gin.Context) {
})
return
}
data.SetCheckinState(userId.(uuid.UUID), true)
data.SetCheckinState(userId.(uuid.UUID), time.Now())
ctx.JSON(http.StatusOK, gin.H{
"status": "success",
})

View File

@@ -8,4 +8,5 @@ import (
func Handler(r *gin.RouterGroup) {
r.Use(jwt.JWTAuth())
r.POST("", Checkin)
}