auth

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2019 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LocalHandler = func(c echo.Context) error {
	p := new(localParams)

	if err := c.Bind(p); err != nil {
		return c.JSON(http.StatusBadRequest, err)
	}

	if err := common.Validate(p); err != nil {
		return c.JSON(http.StatusBadRequest, err)
	}

	users, err := models.Users(qm.Where("email = ?", p.Email)).AllG()

	if err != nil {
		return c.JSON(http.StatusInternalServerError, fmt.Sprintln(err))
	}

	if users == nil {
		return c.JSON(http.StatusUnauthorized, "Invalid credentials")
	}

	user := users[0]

	if user.Password.Ptr() == nil {
		return c.JSON(http.StatusUnauthorized, "Password not defined")
	}

	if ok := services.VerifyUserPassword(user, p.Password); !ok {
		return c.JSON(http.StatusUnauthorized, "Invalid credentials")
	}

	if user.Role == models.UserRoleDisable {
		return c.JSON(http.StatusUnauthorized, "Account disabled")
	}

	_, err = SetToken(c, user)

	if err != nil {
		return c.JSON(http.StatusInternalServerError, fmt.Sprintln(err))
	}

	return c.JSON(http.StatusOK, &respJSON{
		ID:          user.ID,
		Email:       user.Email,
		DisplayName: user.DisplayName,
		Picture:     user.Picture,
		Role:        user.Role,
	})
}

LocalHandler auth local

View Source
var LogoutHandler = func(c echo.Context) error {
	RemoveToken(c)

	return c.JSON(http.StatusOK, "ok")
}

LogoutHandler auth logout

View Source
var RegisterHandler = func(c echo.Context) error {
	p := new(registerParams)

	if err := c.Bind(p); err != nil {
		return c.JSON(http.StatusBadRequest, err)
	}

	if err := common.Validate(p); err != nil {
		return c.JSON(http.StatusBadRequest, err)
	}

	users, err := models.Users(qm.Where("email = ?", p.Email)).AllG()

	if err != nil {
		return c.JSON(http.StatusInternalServerError, err)
	}

	if users != nil {
		return c.JSON(http.StatusBadRequest, "Email already exists")
	}

	user := &models.User{
		Email: p.Email,
	}

	if err := services.SetUserPassword(user, p.Password); err != nil {
		return c.JSON(http.StatusInternalServerError, err)
	}

	err = user.InsertG(boil.Whitelist("email", "password", "token_version"))

	if err != nil {
		return c.JSON(http.StatusInternalServerError, err)
	}

	_, err = SetToken(c, user)

	if err != nil {
		return c.JSON(http.StatusInternalServerError, err)
	}

	return c.JSON(http.StatusOK, &respJSON{
		ID:          user.ID,
		Email:       user.Email,
		DisplayName: user.DisplayName,
		Picture:     user.Picture,
		Role:        user.Role,
	})
}

RegisterHandler auth local

Functions

func GetIssuer

func GetIssuer(c echo.Context) string

GetIssuer get issuer from context

func OAuth2CallbackHandler

func OAuth2CallbackHandler(c echo.Context) error

OAuth2CallbackHandler oauth2 callback

func OAuth2Handler

func OAuth2Handler(c echo.Context) error

OAuth2Handler redirect to permission

func RemoveToken

func RemoveToken(c echo.Context)

RemoveToken unset token from cookie

func SetToken

func SetToken(c echo.Context, u *models.User) (token string, err error)

SetToken set cookie token

Types

This section is empty.

Jump to

Keyboard shortcuts

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