silvermobi/models/accounts.go

51 lines
1.2 KiB
Go
Raw Normal View History

2023-08-08 10:28:43 +00:00
package models
import (
"time"
2024-10-31 19:32:54 +00:00
"github.com/golang-jwt/jwt/v4"
2023-08-08 10:28:43 +00:00
)
type Account struct {
ID string `json:"id"`
2023-12-08 06:35:04 +00:00
Email string `json:"email"`
2023-08-08 10:28:43 +00:00
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
VerifiedIdentity *bool `json:"verifiedIdentity,omitempty"`
BirthDate string `json:"birthdate,omitempty"`
2023-09-19 09:06:20 +00:00
Type string `json:"type,omitempty"`
2023-08-08 10:28:43 +00:00
LocalCredentials
}
2023-12-08 06:35:04 +00:00
type User struct {
ID string `json:"user_id"`
Status string `json:"status"`
BookingID string `json:"booking_id"`
}
2023-08-08 10:28:43 +00:00
type LocalCredentials struct {
Email string
EmailVerified bool
EmailValidationCode string
PhoneNumber string
PhoneNumberVerified bool
PhoneNumberValidationCode string
}
type UserClaims struct {
jwt.RegisteredClaims
}
func (account Account) CreateToken(ttl time.Duration, key string) (token string, err error) {
2024-10-31 19:32:54 +00:00
expiresAt := jwt.NewNumericDate(time.Now().Add(ttl))
2023-08-08 10:28:43 +00:00
claims := UserClaims{
RegisteredClaims: jwt.RegisteredClaims{
2023-09-19 09:06:20 +00:00
Subject: account.ID,
2024-10-31 19:32:54 +00:00
ExpiresAt: expiresAt,
2023-08-08 10:28:43 +00:00
},
}
t := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return t.SignedString([]byte(key))
}