code refactor
This commit is contained in:
74
handlers/signup.go
Normal file
74
handlers/signup.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"log"
|
||||
"os"
|
||||
"relay-server/database"
|
||||
"relay-server/helpers"
|
||||
"relay-server/models"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Signup(c *fiber.Ctx) error {
|
||||
db, _ := database.InitDatabase()
|
||||
u := new(models.SignupStruct)
|
||||
if err := c.BodyParser(u); err != nil {
|
||||
return err
|
||||
}
|
||||
// Checks if username or passwords are empty
|
||||
if u.Username == "" {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "username is empty"})
|
||||
} else if u.Password == "" {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "password is empty"})
|
||||
}
|
||||
|
||||
// Checks if passwords have valid length and characters
|
||||
if !helpers.IsValidPassword(u.Password) {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "invalid password"})
|
||||
}
|
||||
// Checks if username have valid length and characters
|
||||
if !helpers.IsValidUsername(u.Username) {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "invalid username"})
|
||||
}
|
||||
|
||||
// Checks if username already exist in database
|
||||
exist, _ := database.CheckUserExists(db, u.Username)
|
||||
if exist {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "user already exists"})
|
||||
}
|
||||
|
||||
// Create password hash
|
||||
passwordHash, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
log.Printf("error hashing password: %v", err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "internal server error"})
|
||||
}
|
||||
|
||||
// Insert username and password hash to database
|
||||
userId, err := database.InsertUser(db, u.Username, string(passwordHash))
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal server error"})
|
||||
}
|
||||
|
||||
// Generate token with user id and username
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
"user_id": userId,
|
||||
"username": u.Username,
|
||||
})
|
||||
// Sign token
|
||||
signedToken, err := token.SignedString([]byte(os.Getenv("JWT_SECRET")))
|
||||
|
||||
// Set token to cookies
|
||||
cookie := new(fiber.Cookie)
|
||||
cookie.Name = "token"
|
||||
cookie.Value = signedToken
|
||||
cookie.Expires = time.Now().Add(30 * 24 * time.Hour)
|
||||
cookie.HTTPOnly = true
|
||||
|
||||
// If everything went well sent username and user_id assigned by database
|
||||
return c.Status(fiber.StatusOK).JSON(fiber.Map{"message": "Successfully signed up", "username": u.Username, "user_id": userId})
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user