Documentation ¶
Index ¶
- Variables
- func GeneratePassword(userPassword string) ([]byte, error)
- func ValidatePassword(userPassword string, hashed []byte) (bool, error)
- type AuthController
- type Controller
- func (c *Controller) AnyLogout()
- func (c *Controller) BeforeActivation(b mvc.BeforeActivation)
- func (c *Controller) GetBy(userID int64) mvc.Result
- func (c *Controller) GetLogin() mvc.Result
- func (c *Controller) GetMe() mvc.Result
- func (c *Controller) GetRegister() mvc.Result
- func (c *Controller) PostLogin(form formValue) mvc.Result
- func (c *Controller) PostRegister(form formValue) mvc.Result
- type DataSource
- type Model
Constants ¶
This section is empty.
Variables ¶
var ( PathLogin = mvc.Response{Path: "/user/login"} PathLogout = mvc.Response{Path: "/user/logout"} )
paths
Functions ¶
func GeneratePassword ¶
GeneratePassword will generate a hashed password for us based on the user's input.
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) 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 ¶
DataSource is our data store example.
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.