github.com/dhax/go-base@v0.0.0-20231004214136-8be7e5c1972b/auth/jwt/token.go (about)

     1  package jwt
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/go-pg/pg/orm"
     7  )
     8  
     9  // Token holds refresh jwt information.
    10  type Token struct {
    11  	ID        int       `json:"id,omitempty"`
    12  	CreatedAt time.Time `json:"created_at,omitempty"`
    13  	UpdatedAt time.Time `json:"updated_at,omitempty"`
    14  	AccountID int       `json:"-"`
    15  
    16  	Token      string    `json:"-"`
    17  	Expiry     time.Time `json:"-"`
    18  	Mobile     bool      `sql:",notnull" json:"mobile"`
    19  	Identifier string    `json:"identifier,omitempty"`
    20  }
    21  
    22  // BeforeInsert hook executed before database insert operation.
    23  func (t *Token) BeforeInsert(db orm.DB) error {
    24  	now := time.Now()
    25  	if t.CreatedAt.IsZero() {
    26  		t.CreatedAt = now
    27  		t.UpdatedAt = now
    28  	}
    29  	return nil
    30  }
    31  
    32  // BeforeUpdate hook executed before database update operation.
    33  func (t *Token) BeforeUpdate(db orm.DB) error {
    34  	t.UpdatedAt = time.Now()
    35  	return nil
    36  }
    37  
    38  // Claims returns the token claims to be signed
    39  func (t *Token) Claims() RefreshClaims {
    40  	return RefreshClaims{
    41  		ID:    t.ID,
    42  		Token: t.Token,
    43  	}
    44  }