Skip to content

Commit 121a3e4

Browse files
committed
feat(repository): add CheckActiveToken, StoreToken, and UnactivateTokenByUser methods for token management
1 parent 823e010 commit 121a3e4

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package repository
2+
3+
import (
4+
"context"
5+
6+
"github.com/hammer-code/lms-be/domain"
7+
)
8+
9+
func (repo *repository) CheckActiveToken(ctx context.Context, token string) (logoutToken domain.LogoutToken, err error) {
10+
if err = repo.db.DB(ctx).Find(&logoutToken, "token = ?", token).Error; err != nil {
11+
return
12+
}
13+
return
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package repository
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/hammer-code/lms-be/domain"
8+
)
9+
10+
func (repo *repository) StoreToken(ctx context.Context, token string, expiredAt time.Time, uid int) error {
11+
err := repo.db.DB(ctx).Create(&domain.LogoutToken{
12+
Token: token,
13+
ExpiredAt: expiredAt,
14+
CreatedAt: time.Now(),
15+
UserId: uid,
16+
Status: 1,
17+
}).Error
18+
19+
if err != nil {
20+
return err
21+
}
22+
23+
return nil
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package repository
2+
3+
import (
4+
"context"
5+
6+
"github.com/hammer-code/lms-be/domain"
7+
)
8+
9+
func (repo *repository) UnactivateTokenByUser(ctx context.Context, uid int) error {
10+
if err := repo.db.DB(ctx).Model(&domain.LogoutToken{}).Where("user_id = ? AND status = 1", uid).Update("status", 0).Error; err != nil {
11+
return err
12+
}
13+
14+
return nil
15+
}

0 commit comments

Comments
 (0)