all repos

onasty @ 943fcdb

a one-time notes service

onasty/internal/models/user.go(view raw)

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
package models

import (
	"errors"
	"net/mail"
	"time"

	"github.com/gofrs/uuid/v5"
)

var (
	ErrUserEmailIsAlreadyInUse = errors.New("user: email is already in use")
	ErrUsernameIsAlreadyInUse  = errors.New("user: username is already in use")
	ErrUserIsAlreeadyVerified  = errors.New("user: user is already verified")

	ErrVerificationTokenNotFound = errors.New("user: verification token not found")
	ErrUserIsNotActivated        = errors.New("user: user is not activated")

	ErrUserNotFound         = errors.New("user: not found")
	ErrUserWrongCredentials = errors.New("user: wrong credentials")
)

type User struct {
	ID          uuid.UUID
	Username    string
	Email       string
	Activated   bool
	Password    string
	CreatedAt   time.Time
	LastLoginAt time.Time
}

func (u User) Validate() error {
	// NOTE: there's probably a better way to validate emails
	_, err := mail.ParseAddress(u.Email)
	if err != nil {
		return errors.New("user: invalid email") //nolint:err113
	}

	if len(u.Password) < 6 {
		return errors.New("user: password too short, minimum 6 chars") //nolint:err113
	}

	if len(u.Username) == 0 {
		return errors.New("user: username is required") //nolint:err113
	}

	return nil
}