Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Toby Yan <me@tobyan.com>
- Loading branch information
Showing
11 changed files
with
420 additions
and
420 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,77 @@ | ||
package controllers | ||
|
||
import ( | ||
"net/http" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/gin-gonic/gin" | ||
|
||
"github.com/totoval/framework/config" | ||
"github.com/totoval/framework/helpers" | ||
"github.com/totoval/framework/helpers/m" | ||
"github.com/totoval/framework/http/controller" | ||
"github.com/totoval/framework/utils/crypt" | ||
"github.com/totoval/framework/utils/jwt" | ||
"totoval/app/http/requests" | ||
"totoval/app/models" | ||
"github.com/totoval/framework/config" | ||
"github.com/totoval/framework/helpers" | ||
"github.com/totoval/framework/helpers/m" | ||
"github.com/totoval/framework/http/controller" | ||
"github.com/totoval/framework/utils/crypt" | ||
"github.com/totoval/framework/utils/jwt" | ||
"totoval/app/http/requests" | ||
"totoval/app/models" | ||
) | ||
|
||
type Register struct { | ||
controller.BaseController | ||
controller.BaseController | ||
} | ||
|
||
func (r *Register) Register(c *gin.Context) { | ||
// validate and assign requestData | ||
var requestData requests.UserRegister | ||
if !r.Validate(c, &requestData, true) { | ||
return | ||
} | ||
// validate and assign requestData | ||
var requestData requests.UserRegister | ||
if !r.Validate(c, &requestData, true) { | ||
return | ||
} | ||
|
||
// determine if exist | ||
user := models.User{ | ||
Email: &requestData.Email, | ||
} | ||
if m.H().Exist(&user, true) { | ||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.register.failed_existed")}) | ||
return | ||
} | ||
// determine if exist | ||
user := models.User{ | ||
Email: &requestData.Email, | ||
} | ||
if m.H().Exist(&user, true) { | ||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.register.failed_existed")}) | ||
return | ||
} | ||
|
||
// create user | ||
// encrypt password //@todo move to model setter later | ||
encryptedPassword := crypt.Bcrypt(requestData.Password) | ||
user.Password = &encryptedPassword | ||
if err := m.H().Create(&user); err != nil { | ||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.register.failed_system_error")}) | ||
return | ||
} | ||
// create user | ||
// encrypt password //@todo move to model setter later | ||
encryptedPassword := crypt.Bcrypt(requestData.Password) | ||
user.Password = &encryptedPassword | ||
if err := m.H().Create(&user); err != nil { | ||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.register.failed_system_error")}) | ||
return | ||
} | ||
|
||
// add user affiliation | ||
if config.GetBool("user_affiliation.enable") { | ||
uaffPtr := &models.UserAffiliation{ | ||
UserID: user.ID, | ||
} | ||
var err error | ||
if requestData.AffiliationFromCode != nil { | ||
err = uaffPtr.InsertNode(&user, *requestData.AffiliationFromCode) | ||
} else { | ||
err = uaffPtr.InsertNode(&user) | ||
} | ||
if err != nil { | ||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.register.failed_system_error")}) | ||
return | ||
} | ||
} | ||
// add user affiliation | ||
if config.GetBool("user_affiliation.enable") { | ||
uaffPtr := &models.UserAffiliation{ | ||
UserID: user.ID, | ||
} | ||
var err error | ||
if requestData.AffiliationFromCode != nil { | ||
err = uaffPtr.InsertNode(&user, *requestData.AffiliationFromCode) | ||
} else { | ||
err = uaffPtr.InsertNode(&user) | ||
} | ||
if err != nil { | ||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.register.failed_system_error")}) | ||
return | ||
} | ||
} | ||
|
||
// create jwt | ||
newJwt := jwt.NewJWT(config.GetString("auth.sign_key")) | ||
username := "" | ||
if user.Name != nil { | ||
username = *user.Name | ||
} | ||
if token, err := newJwt.CreateToken(string(*user.ID), username); err == nil { | ||
c.JSON(http.StatusOK, gin.H{"token": token}) | ||
return | ||
} | ||
// create jwt | ||
newJwt := jwt.NewJWT(config.GetString("auth.sign_key")) | ||
username := "" | ||
if user.Name != nil { | ||
username = *user.Name | ||
} | ||
if token, err := newJwt.CreateToken(string(*user.ID), username); err == nil { | ||
c.JSON(http.StatusOK, gin.H{"token": token}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.register.failed_token_generate_error")}) | ||
return | ||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.register.failed_token_generate_error")}) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
package controllers | ||
|
||
import ( | ||
"net/http" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/gin-gonic/gin" | ||
|
||
"github.com/totoval/framework/http/controller" | ||
"totoval/app/models" | ||
"github.com/totoval/framework/http/controller" | ||
"totoval/app/models" | ||
) | ||
|
||
type UserAffiliation struct { | ||
controller.BaseController | ||
controller.BaseController | ||
} | ||
|
||
func (uaff *UserAffiliation) RenderAll(c *gin.Context) { | ||
var u models.UserAffiliation | ||
c.HTML(http.StatusOK, "user_affiliation.nodes", gin.H{ | ||
"data": u.All(), | ||
}) | ||
var u models.UserAffiliation | ||
c.HTML(http.StatusOK, "user_affiliation.nodes", gin.H{ | ||
"data": u.All(), | ||
}) | ||
|
||
return | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package requests | ||
|
||
type UserRegister struct { | ||
Email string `json:"email" binding:"required,email"` | ||
AffiliationFromCode *string `json:"affiliation_code" binding:"omitempty,len=6"` | ||
Password string `json:"password" binding:"required,min=8,max=24"` | ||
PasswordConfirmation string `json:"password_confirmation" binding:"required,eqfield=Password"` | ||
Email string `json:"email" binding:"required,email"` | ||
AffiliationFromCode *string `json:"affiliation_code" binding:"omitempty,len=6"` | ||
Password string `json:"password" binding:"required,min=8,max=24"` | ||
PasswordConfirmation string `json:"password_confirmation" binding:"required,eqfield=Password"` | ||
} |
Oops, something went wrong.