Implement registration

This commit is contained in:
2016-12-20 15:26:31 +01:00
parent b9d428d386
commit a71afaf6b9
4 changed files with 42 additions and 1 deletions

View File

@ -36,6 +36,7 @@ func (h *Handler) Serve() {
api.GET("/logout", logout)
api.GET("/login", func(c *gin.Context) { c.Redirect(http.StatusPermanentRedirect, "/login") })
api.POST("/login", h.loginPost)
api.POST("/register", h.registerPost)
// Unauthenticated routes
api.GET("/check", func(c *gin.Context) { c.String(http.StatusOK, "Accessible") })
@ -131,3 +132,30 @@ func (h *Handler) loginPost(c *gin.Context) {
"token": t,
})
}
func (h *Handler) registerPost(c *gin.Context) {
username, _ := c.GetPostForm("username")
password, _ := c.GetPostForm("password")
name, _ := c.GetPostForm("name")
user, err := h.UserService.UserByUsername(username)
if err != nil {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
hash, err := h.CredentialsVerifier.Hash(password)
if err != nil {
c.AbortWithStatus(http.StatusUnauthorized)
}
user = &budgeteer.User{
Name: name,
Password: hash,
Email: username,
}
err = h.UserService.CreateUser(user)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
}
}