ADD: added basic cli tool and updated the login modal
This commit is contained in:
@@ -15,6 +15,14 @@ type LoginRequest struct {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type RegisterRequest struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
const defaultRole = "user"
|
||||
|
||||
var secret = []byte("secret")
|
||||
|
||||
func Login(c *gin.Context, db *sql.DB) {
|
||||
@@ -49,6 +57,26 @@ func Login(c *gin.Context, db *sql.DB) {
|
||||
|
||||
}
|
||||
|
||||
func Register(c *gin.Context, db *sql.DB) {
|
||||
var req RegisterRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
||||
return
|
||||
}
|
||||
if req.Email == "" || req.Password == "" || req.Username == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Email and password are required"})
|
||||
return
|
||||
}
|
||||
|
||||
error := user.CreateUser(db, req.Email, req.Username, req.Password, []string{defaultRole})
|
||||
if error != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": error.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "User created successfully"})
|
||||
|
||||
}
|
||||
|
||||
func GenerateJWT(uuid string, email string, roles []string) (any, error) {
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
"user": uuid,
|
||||
|
||||
Reference in New Issue
Block a user