Exemplo n.º 1
0
// AuthorizeUser performs the authentication for the API user
//
// GET /authorize
//
func (c *SessionController) AuthorizeUser(ctx *gin.Context) {
	var json bindings.AuthorizeUser
	err := ctx.Bind(&json)
	if err != nil {
		// Missing authentication credentials
		helpers.JSONResponseValidationFailed(ctx, err)
		return
	}

	user, err := services.Auth.AuthorizeUser(json)
	if err != nil {
		helpers.JSONForbidden(ctx, "Invalid authentication credentials")
		return
	}

	tokenFields, err := services.Auth.GenerateToken(user)
	if err != nil {
		helpers.JSONResponseInternalServerError(ctx, err)
		return
	}

	helpers.JSONResponseOK(ctx, gin.H{
		"user":   presenters.UserPresenter(user),
		"tokens": tokenFields,
	})

}
Exemplo n.º 2
0
// RegisterNewUser creates a new user account
//
// POST /users
//
func (c *UsersController) RegisterNewUser(ctx *gin.Context) {
	var json bindings.RegisterNewUser
	err := ctx.Bind(&json)
	if err != nil {
		helpers.JSONResponseValidationFailed(ctx, err)
		return
	}

	user, err := services.RegisterNewUser(json)
	if err != nil {
		if err == services.ErrNamespaceAlreadyExists {
			helpers.JSONErrorf(ctx, http.StatusBadRequest, "Username already exists")
		} else {
			helpers.JSONResponseInternalServerError(ctx, err)
		}
		return
	}

	helpers.JSONResponseObject(ctx, presenters.UserPresenter(user))
}
Exemplo n.º 3
0
// UpdateMe updates the authenticated user
//
// PATCH /user
//
func (c *UsersController) UpdateMe(ctx *gin.Context) {
	user := getCurrentUser(ctx)
	//TODO: fix me

	helpers.JSONResponseObject(ctx, presenters.UserPresenter(user))
}
Exemplo n.º 4
0
// GetMe gets the authenticated user
//
// GET /user
//
func (c *UsersController) GetMe(ctx *gin.Context) {
	user := getCurrentUser(ctx)
	helpers.JSONResponseObject(ctx, presenters.UserPresenter(user))
}