Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
func RegisterFn(c *gin.Context) {
if c.Request.Method == http.MethodGet { // Serve registration page
c.HTML(http.StatusOK, "register", gin.H{
"title": "Hi! GoBlog.",
})
} else if c.Request.Method == http.MethodPost { // Process registration
dbc, exist := c.Get("db")
if !exist {
c.HTML(http.StatusOK, "register", gin.H{
"title": "Hi! GoBlog.",
"error": "We've got an internal error, please try later.",
})
return
}
var register models.RegistrationForm
var user models.User
db := dbc.(*gorm.DB)
if err := c.ShouldBind(&register); err == nil {
if register.CheckPasswords() {
user.Username = register.Username
user.Email = register.Email
user.SetPassword(register.PasswordA)
db.Create(&user)
if db.NewRecord(user) { // New record means user has not a ID set
c.HTML(http.StatusOK, "register", gin.H{
"title": "Hi! GoBlog.",
"error": "The username you have entered is already taken. Please choose another one.",
})
return
}
user.IsAuthenticated = true
c.HTML(http.StatusOK, "index", gin.H{
"title": "Hi! GoBlog.",
"user": user,
})
return
}
c.HTML(http.StatusOK, "register", gin.H{
"title": "Hi! GoBlog.",
"error": "Passwords do not match.",
})
return
}
c.HTML(http.StatusOK, "register", gin.H{
"title": "Hi! GoBlog.",
"error": "Required fields are not provided correctly.",
})
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.