Implement registration
This commit is contained in:
28
http/http.go
28
http/http.go
@ -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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user