Copy // src/controllers/UserController.go
package controllers
import (
"gofiber/src/helpers"
"gofiber/src/models"
"os"
"github.com/gofiber/fiber/v2"
"golang.org/x/crypto/bcrypt"
)
func RegisterUser (c * fiber . Ctx ) error {
var user models . User
if err := c. BodyParser ( & user); err != nil {
return c. Status (fiber.StatusBadRequest). JSON ( fiber . Map {
"error" : "Failed to parse request body" ,
})
}
hashPassword, _ := bcrypt. GenerateFromPassword ([] byte (user.Password), bcrypt.DefaultCost)
user.Password = string (hashPassword)
models. PostUser ( & user)
return c. Status (fiber.StatusCreated). JSON ( fiber . Map {
"message" : "Register successfully" ,
})
}
func LoginUser (c * fiber . Ctx ) error {
var user models . User
if err := c. BodyParser ( & user); err != nil {
return c. Status (fiber.StatusBadRequest). JSON ( fiber . Map {
"error" : "Failed to parse request body" ,
})
}
validateEmail := models. FindEmail ( & user)
if len (validateEmail) == 0 {
return c. Status (fiber.StatusNotFound). JSON ( fiber . Map {
"message" : "Email not found" ,
})
}
var PasswordSecond string
for _, user := range validateEmail {
PasswordSecond = user.Password
}
if err := bcrypt. CompareHashAndPassword ([] byte (PasswordSecond), [] byte (user.Password)); err != nil || user.Password == "" {
return c. Status (fiber.StatusUnauthorized). JSON ( fiber . Map {
"message" : "Invalid password" ,
})
}
jwtKey := os. Getenv ( "SECRETKEY" )
payload := map [ string ] interface {}{
"email" : user.Email,
}
token, err := helpers. GenerateToken (jwtKey, payload)
if err != nil {
return c. Status (fiber.StatusInternalServerError). JSON ( fiber . Map {
"error" : "Could not generate access token" ,
})
}
refreshToken, err := helpers. GenerateRefreshToken (jwtKey, payload)
if err != nil {
return c. Status (fiber.StatusInternalServerError). JSON ( fiber . Map {
"error" : "Could not generate refresh token" ,
})
}
item := map [ string ] string {
"Email" : user.Email,
"Token" : token,
"RefreshToken" : refreshToken,
}
return c. Status (fiber.StatusCreated). JSON ( fiber . Map {
"message" : "Login successfully" ,
"data" : item,
})
}
func RefreshToken (c * fiber . Ctx ) error {
var input struct {
RefreshToken string `json:"refreshToken"`
}
if err := c. BodyParser ( & input); err != nil {
return c. Status (fiber.StatusBadRequest). JSON ( fiber . Map {
"error" : "Failed to parse request body" ,
})
}
jwtKey := os. Getenv ( "SECRETKEY" )
token, err := helpers. GenerateToken (jwtKey, map [ string ] interface {}{ "refreshToken" : input.RefreshToken})
if err != nil {
return c. Status (fiber.StatusInternalServerError). JSON ( fiber . Map {
"error" : "Could not generate access token" ,
})
}
refreshToken, err := helpers. GenerateRefreshToken (jwtKey, map [ string ] interface {}{ "refreshToken" : input.RefreshToken})
if err != nil {
return c. Status (fiber.StatusInternalServerError). JSON ( fiber . Map {
"error" : "Could not generate refresh token" ,
})
}
item := map [ string ] string {
"Token" : token,
"RefreshToken" : refreshToken,
}
return c. Status (fiber.StatusCreated). JSON ( fiber . Map {
"message" : "Refresh successfully" ,
"data" : item,
})
}