Feat: add login
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"gitea.konchin.com/go2025/backend/models"
|
||||
"gitea.konchin.com/go2025/backend/tracing"
|
||||
"gitea.konchin.com/go2025/backend/utils"
|
||||
"github.com/uptrace/bun"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
@@ -19,14 +20,14 @@ func NewBunDatabase(db *bun.DB) *BunDatabase {
|
||||
|
||||
func (self *BunDatabase) GetSession(
|
||||
ctx context.Context,
|
||||
userId string,
|
||||
loginToken string,
|
||||
) (models.Session, error) {
|
||||
ret := models.Session{
|
||||
UserId: userId,
|
||||
LoginToken: loginToken,
|
||||
}
|
||||
err := self.db.NewSelect().
|
||||
Model(&ret).
|
||||
WherePK().
|
||||
Where("login_token = ?", loginToken).
|
||||
Scan(ctx)
|
||||
if err != nil {
|
||||
return models.Session{}, err
|
||||
@@ -67,3 +68,23 @@ func (self *BunDatabase) UpdateRefreshToken(
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (self *BunDatabase) UpdateLoginToken(
|
||||
ctx context.Context,
|
||||
userId string,
|
||||
) (string, error) {
|
||||
token, err := utils.RandomString(24)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, err = self.db.NewUpdate().
|
||||
Model((*models.Session)(nil)).
|
||||
Set("login_token = ?", token).
|
||||
Where("user_id = ?", userId).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user