user

package
v10.0.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2018 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	PathLogin  = mvc.Response{Path: "/user/login"}
	PathLogout = mvc.Response{Path: "/user/logout"}
)

paths

Functions

func GeneratePassword

func GeneratePassword(userPassword string) ([]byte, error)

GeneratePassword will generate a hashed password for us based on the user's input.

func ValidatePassword

func ValidatePassword(userPassword string, hashed []byte) (bool, error)

ValidatePassword will check if passwords are matched.

Types

type AuthController

type AuthController struct {
	Source  *DataSource
	Session *sessions.Session

	// the whole controller is request-scoped because we already depend on Session, so
	// this will be new for each new incoming request, BeginRequest sets that based on the session.
	UserID int64
}

AuthController is the user authentication controller, a custom shared controller.

func (*AuthController) BeginRequest

func (c *AuthController) BeginRequest(ctx iris.Context)

BeginRequest saves login state to the context, the user id.

func (*AuthController) EndRequest

func (c *AuthController) EndRequest(ctx iris.Context)

EndRequest is here just to complete the BaseController in order to be tell iris to call the `BeginRequest` before the main method.

type Controller

type Controller struct {
	AuthController
}

Controller is responsible to handle the following requests: GET /user/register POST /user/register GET /user/login POST /user/login GET /user/me GET /user/{id:long} | long is a new param type, it's the int64. All HTTP Methods /user/logout

func (*Controller) AnyLogout

func (c *Controller) AnyLogout()

AnyLogout handles any method on path /user/logout.

func (*Controller) BeforeActivation

func (c *Controller) BeforeActivation(b mvc.BeforeActivation)

BeforeActivation called once before the server start and before the controller's registration, here you can add dependencies, to this controller and only, that the main caller may skip.

func (*Controller) GetBy

func (c *Controller) GetBy(userID int64) mvc.Result

GetBy handles GET:/user/{id:long}, i.e http://localhost:8080/user/1

func (*Controller) GetLogin

func (c *Controller) GetLogin() mvc.Result

GetLogin handles GET:/user/login.

func (*Controller) GetMe

func (c *Controller) GetMe() mvc.Result

GetMe handles GET:/user/me.

func (*Controller) GetRegister

func (c *Controller) GetRegister() mvc.Result

GetRegister handles GET:/user/register. mvc.Result can accept any struct which contains a `Dispatch(ctx iris.Context)` method. Both mvc.Response and mvc.View are mvc.Result.

func (*Controller) PostLogin

func (c *Controller) PostLogin(form formValue) mvc.Result

PostLogin handles POST:/user/login.

func (*Controller) PostRegister

func (c *Controller) PostRegister(form formValue) mvc.Result

PostRegister handles POST:/user/register.

type DataSource

type DataSource struct {
	Users map[int64]Model
	// contains filtered or unexported fields
}

DataSource is our data store example.

func NewDataSource

func NewDataSource() *DataSource

NewDataSource returns a new user data source.

func (*DataSource) GetBy

func (d *DataSource) GetBy(query func(Model) bool) (user Model, found bool)

GetBy receives a query function which is fired for every single user model inside our imaginary database. When that function returns true then it stops the iteration.

It returns the query's return last known boolean value and the last known user model to help callers to reduce the loc.

But be carefully, the caller should always check for the "found" because it may be false but the user model has actually real data inside it.

It's actually a simple but very clever prototype function I'm think of and using everywhere since then, hope you find it very useful too.

func (*DataSource) GetByID

func (d *DataSource) GetByID(id int64) (Model, bool)

GetByID returns a user model based on its ID.

func (*DataSource) GetByUsername

func (d *DataSource) GetByUsername(username string) (Model, bool)

GetByUsername returns a user model based on the Username.

func (*DataSource) InsertOrUpdate

func (d *DataSource) InsertOrUpdate(user Model) (Model, error)

InsertOrUpdate adds or updates a user to the (memory) storage.

type Model

type Model struct {
	ID        int64  `json:"id"`
	Firstname string `json:"firstname"`
	Username  string `json:"username"`

	HashedPassword []byte    `json:"-"`
	CreatedAt      time.Time `json:"created_at"`
	// contains filtered or unexported fields
}

Model is our User example model.

func (Model) Dispatch

func (u Model) Dispatch(ctx iris.Context)

Dispatch completes the `mvc.Result` interface in order to be able to return a type of `Model` as mvc.Result. If this function didn't exist then we should explicit set the output result to that Model or to an interface{}.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL