user

package
v8.5.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2017 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PathLogin  = "/user/login"
	PathLogout = "/user/logout"
)

paths

Variables

This section is empty.

Functions

func AllowUser

func AllowUser(ctx iris.Context)

AllowUser will check if this client is a logged user, if not then it will redirect that guest to the login page otherwise it will allow the execution of the next handler.

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 {
	iris.SessionController

	Source *DataSource
	User   Model `iris:"model"`
}

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.

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) GetBy

func (c *Controller) GetBy(userID int64)

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

func (*Controller) GetLogin

func (c *Controller) GetLogin()

GetLogin handles GET:/user/login.

func (*Controller) GetMe

func (c *Controller) GetMe()

GetMe handles GET:/user/me.

func (*Controller) GetRegister

func (c *Controller) GetRegister()

GetRegister handles GET:/user/register.

func (*Controller) PostLogin

func (c *Controller) PostLogin()

PostLogin handles POST:/user/login.

func (*Controller) PostRegister

func (c *Controller) PostRegister()

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.

Jump to

Keyboard shortcuts

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