2023-12-27 17:31:49 +09:00
|
|
|
package repositories
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
config "learnsteam/cslms-api/configs"
|
|
|
|
"learnsteam/cslms-api/internal/models"
|
|
|
|
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type tokenRepository struct {
|
|
|
|
DB *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTokenRepository(db *gorm.DB) TokenRepository {
|
|
|
|
return &tokenRepository{
|
|
|
|
DB: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type TokenRepository interface {
|
|
|
|
Generate(int64, int64, string) (string, error)
|
|
|
|
Find(int64) (*models.Token, error)
|
|
|
|
FindByRefreshToken(int64, string) (*models.Token, error)
|
|
|
|
Create(*models.Token) (*models.Token, error)
|
|
|
|
Update(*models.Token) (*models.Token, error)
|
|
|
|
Delete(string) error
|
2023-12-29 00:27:12 +09:00
|
|
|
Invalid(int64, string) error
|
2023-12-27 17:31:49 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *tokenRepository) Generate(user_id int64, expire_at int64, role string) (string, error) {
|
|
|
|
fmt.Println(user_id, role)
|
|
|
|
claims := jwt.MapClaims{}
|
|
|
|
claims["authorized"] = true
|
|
|
|
claims["sub"] = user_id
|
|
|
|
claims["exp"] = expire_at
|
|
|
|
claims["role"] = role
|
|
|
|
at := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
|
token, err := at.SignedString([]byte(config.SECRET_KEY))
|
|
|
|
|
|
|
|
fmt.Println(token)
|
|
|
|
return token, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tokenRepository) Find(id int64) (*models.Token, error) {
|
|
|
|
var token *models.Token
|
|
|
|
err := r.DB.Where("id = ?", id).First(&token).Error
|
|
|
|
return token, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tokenRepository) FindByRefreshToken(user_id int64, refreshToken string) (*models.Token, error) {
|
|
|
|
var token *models.Token
|
|
|
|
err := r.DB.Where("user_id = ? AND refresh_token = ?", user_id, refreshToken).First(&token).Error
|
|
|
|
return token, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tokenRepository) Create(token *models.Token) (*models.Token, error) {
|
|
|
|
err := r.DB.Create(&token).Error
|
|
|
|
return token, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tokenRepository) Update(token *models.Token) (*models.Token, error) {
|
|
|
|
var row *models.Token
|
|
|
|
if err := r.DB.Where("id=?", token.ID).First(&row).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err := r.DB.Model(&row).Select("*").Updates(&token).Error
|
|
|
|
return row, err
|
|
|
|
}
|
|
|
|
|
2023-12-29 00:27:12 +09:00
|
|
|
func (r *tokenRepository) Invalid(user_id int64, tokenString string) error {
|
|
|
|
err := r.DB.Model(&models.Token{}).Where("user_id = ? AND token = ?", user_id, tokenString).Update("status", "off").Error
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-27 17:31:49 +09:00
|
|
|
func (r *tokenRepository) Delete(id string) error {
|
|
|
|
var token *models.Token
|
|
|
|
if err := r.DB.Where("id=?", id).First(&token).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err := r.DB.Delete(&token).Error
|
|
|
|
return err
|
|
|
|
}
|