mirror of
https://github.com/ordinary-dev/phoenix
synced 2024-09-19 19:30:28 +05:00
Bring back the registration page
This commit is contained in:
parent
37e8663ac2
commit
5cb00632e6
|
@ -12,7 +12,12 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
func ShowRegistrationForm(c *gin.Context) {
|
||||
func ShowRegistrationForm(c *gin.Context, db *gorm.DB) {
|
||||
if database.CountAdmins(db) > 0 {
|
||||
ShowError(c, errors.New("At least 1 user already exists"))
|
||||
return
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "auth.html.tmpl", gin.H{
|
||||
"title": "Create an account",
|
||||
"description": "To prevent other people from seeing your links, create an account.",
|
||||
|
@ -61,10 +66,14 @@ func RequireAuth(c *gin.Context, cfg *config.Config) (*jwt.RegisteredClaims, err
|
|||
return claims, nil
|
||||
}
|
||||
|
||||
func AuthMiddleware(c *gin.Context, cfg *config.Config) {
|
||||
func AuthMiddleware(c *gin.Context, db *gorm.DB, cfg *config.Config) {
|
||||
claims, err := RequireAuth(c, cfg)
|
||||
if err != nil {
|
||||
c.Redirect(http.StatusFound, "/signin")
|
||||
if database.CountAdmins(db) < 1 {
|
||||
c.Redirect(http.StatusFound, "/registration")
|
||||
} else {
|
||||
c.Redirect(http.StatusFound, "/signin")
|
||||
}
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
@ -89,6 +98,11 @@ func GetJWTToken(cfg *config.Config) (string, error) {
|
|||
}
|
||||
|
||||
func CreateUser(c *gin.Context, db *gorm.DB, cfg *config.Config) {
|
||||
if database.CountAdmins(db) > 0 {
|
||||
ShowError(c, errors.New("At least 1 user already exists"))
|
||||
return
|
||||
}
|
||||
|
||||
// Try to create a user.
|
||||
username := c.PostForm("username")
|
||||
password := c.PostForm("password")
|
||||
|
|
|
@ -29,9 +29,17 @@ func GetGinEngine(cfg *config.Config, db *gorm.DB) *gin.Engine {
|
|||
AuthorizeUser(c, db, cfg)
|
||||
})
|
||||
|
||||
engine.GET("/registration", func(c *gin.Context) {
|
||||
ShowRegistrationForm(c, db)
|
||||
})
|
||||
engine.POST("/api/users", func(c *gin.Context) {
|
||||
CreateUser(c, db, cfg)
|
||||
})
|
||||
|
||||
// This group requires authorization before viewing.
|
||||
protected := engine.Group("/")
|
||||
protected.Use(func(c *gin.Context) {
|
||||
AuthMiddleware(c, cfg)
|
||||
AuthMiddleware(c, db, cfg)
|
||||
})
|
||||
|
||||
// Main page
|
||||
|
@ -43,11 +51,6 @@ func GetGinEngine(cfg *config.Config, db *gorm.DB) *gin.Engine {
|
|||
ShowSettings(c, db)
|
||||
})
|
||||
|
||||
// Create new user
|
||||
protected.POST("/api/users", func(c *gin.Context) {
|
||||
CreateUser(c, db, cfg)
|
||||
})
|
||||
|
||||
// Create new group
|
||||
protected.POST("/api/groups", func(c *gin.Context) {
|
||||
CreateGroup(c, db)
|
||||
|
|
Loading…
Reference in a new issue