* Removed Tenanting from base objects as some models may be tenantless * Admins are naturally not restricted by tenants * Users *ARE* the tenants (for now) so they don't require a tenant ID either * User-owned models should all include the Tenanted model as their base * Created .Create and .Save methods attached to base model
45 lines
895 B
Go
45 lines
895 B
Go
package models
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt"
|
|
"github.com/yxzzy-wtf/gin-gonic-prepack/database"
|
|
"github.com/yxzzy-wtf/gin-gonic-prepack/util"
|
|
)
|
|
|
|
type User struct {
|
|
Auth
|
|
Email string `gorm:"unique;index"`
|
|
}
|
|
|
|
const userJwtDuration = time.Hour * 24
|
|
|
|
var UserHmac = util.GenerateHmac()
|
|
|
|
func (u *User) GetJwt() (string, int) {
|
|
j := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
|
"sub": u.Uid.String(),
|
|
"iat": time.Now().Unix(),
|
|
"exp": time.Now().Add(userJwtDuration).Unix(),
|
|
"role": "user",
|
|
})
|
|
|
|
jstr, err := j.SignedString(UserHmac)
|
|
if err != nil {
|
|
// we should ALWAYS be able to build and sign a str
|
|
panic(err)
|
|
}
|
|
|
|
return jstr, int(userJwtDuration.Seconds())
|
|
}
|
|
|
|
func (u *User) ByEmail(email string) error {
|
|
if err := database.Db.Where("email = ?", email).First(&u).Error; err != nil {
|
|
return errors.New("not found")
|
|
}
|
|
|
|
return nil
|
|
}
|