Documentation ¶
Index ¶
Constants ¶
const ( ScopeActivation = "activation" ScopeAuthentication = "authentication" )
Define constants for the token scope. For now we just define the scope "activation" but we'll add additional scopes later in the book.
Variables ¶
var AnonymousUser = &User{}
Declare a new AnonymousUser variable.
var ErrInvalidRuntimeFormat = errors.New("invalid runtime format")
Define an error that our UnmarshalJSON() method can return if we're unable to parse or convert the JSON string successfully.
Functions ¶
This section is empty.
Types ¶
type Password ¶
Create a custom password type which is a struct containing the plaintext and hashed versions of the password for a user. The plaintext field is a *pointer* to a string, so that we're able to distinguish between a plaintext password not being present in the struct at all, versus a plaintext password which is the empty string "".
type Permissions ¶
type Permissions []string
func (Permissions) Include ¶
func (p Permissions) Include(code string) bool
Add a helper method to check whether the Permissions slice contains a specific permission code.
type Runtime ¶
type Runtime int32
Declare a custom Runtime type, which has the underlying type int32 (the same as our Movie struct field).
func (Runtime) MarshalJSON ¶
Implement a MarshalJSON() method on the Runtime type so that it satisfies the json.Marshaler interface. This should return the JSON-encoded value for the movie runtime (in our case, it will return a string in the format "<runtime> mins").
func (*Runtime) UnmarshalJSON ¶
Implement a UnmarshalJSON() method on the Runtime type so that it satisfies the json.Unmarshaler interface. IMPORTANT: Because UnmarshalJSON() needs to modify the receiver (our Runtime type), we must use a pointer receiver for this to work correctly. Otherwise, we will only be modifying a copy (which is then discarded when this method returns).
type Token ¶
type Token struct { Plaintext string `json:"token"` Hash []byte `json:"-" db:hash` UserID int64 `json:"-" db:user_id` Expiry time.Time `json:"expiry" db:expiry` Scope string `json:"-" db:scope` }
Define a Token struct to hold the data for an individual token. This includes the plaintext and hashed versions of the token, associated user ID, expiry time and scope.
type User ¶
type User struct { ID int64 `json:"id"` CreatedAt time.Time `json:"created_at"` Name string `json:"name"` Email string `json:"email"` Password `json:"-"` Activated bool `json:"activated"` Version int `json:"-"` }
Define a User struct to represent an individual user. Importantly, notice how we are using the json:"-" struct tag to prevent the Password and Version fields appearing in any output when we encode it to JSON. Also notice that the Password field uses the custom password type defined below.
func (*User) IsAnonymous ¶
Check if a User instance is the AnonymousUser.