-
Notifications
You must be signed in to change notification settings - Fork 4
/
auth.go
50 lines (37 loc) · 1.54 KB
/
auth.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
package repository
import (
"context"
"github.com/nikhilnarayanan623/ecommerce-gin-clean-arch/pkg/domain"
"github.com/nikhilnarayanan623/ecommerce-gin-clean-arch/pkg/repository/interfaces"
"gorm.io/gorm"
)
type authDatabase struct {
DB *gorm.DB
}
func NewAuthRepository(db *gorm.DB) interfaces.AuthRepository {
return &authDatabase{
DB: db,
}
}
func (c *authDatabase) SaveRefreshSession(ctx context.Context, refreshSession domain.RefreshSession) error {
query := `INSERT INTO refresh_sessions (token_id, user_id, refresh_token, expire_at)
VALUES ($1, $2, $3, $4)`
err := c.DB.Exec(query, refreshSession.TokenID, refreshSession.UserID, refreshSession.RefreshToken, refreshSession.ExpireAt).Error
return err
}
func (c *authDatabase) FindRefreshSessionByTokenID(ctx context.Context, tokenID string) (refreshSession domain.RefreshSession, err error) {
query := `SELECT * FROM refresh_sessions WHERE token_id = $1`
err = c.DB.Raw(query, tokenID).Scan(&refreshSession).Error
return
}
func (c *authDatabase) SaveOtpSession(ctx context.Context, otpSession domain.OtpSession) error {
query := `INSERT INTO otp_sessions (otp_id, user_id, phone ,expire_at)
VALUES ($1, $2, $3, $4)`
err := c.DB.Exec(query, otpSession.OtpID, otpSession.UserID, otpSession.Phone, otpSession.ExpireAt).Error
return err
}
func (c *authDatabase) FindOtpSession(ctx context.Context, otpID string) (otpSession domain.OtpSession, err error) {
query := `SELECT * FROM otp_sessions WHERE otp_id = $1`
err = c.DB.Raw(query, otpID).Scan(&otpSession).Error
return otpSession, err
}