go_winter_work_2025/go/lesson3/controllers/controllers.go
2025-02-15 13:28:49 +03:00

135 lines
2.8 KiB
Go

package controllers
import (
strconv "strconv"
time "time"
fiber "github.com/gofiber/fiber/v2"
jwt "github.com/golang-jwt/jwt"
bcrypt "golang.org/x/crypto/bcrypt"
database "lesson3/database"
models "lesson3/models"
)
func Register(c *fiber.Ctx) error {
var data map[string]string
if err := c.BodyParser(&data); err != nil {
return err
}
password, _ := bcrypt.GenerateFromPassword(
[]byte(data["password"]),
14,
) // GenerateFromPassword returns the bcrypt hash of the password at the given cost i.e. (14 in our case).
user := models.User{
Name: data["name"],
Email: data["email"],
Password: password,
}
database.DB.Create(&user) // Adds the data to the DB
return c.JSON(user)
}
const SecretKey = "secret"
func Login(c *fiber.Ctx) error {
var data map[string]string
if err := c.BodyParser(&data); err != nil {
return err
}
var user models.User
database.DB.Where("email = ?", data["email"]).
First(&user)
// Check the email is present in the DB
if user.ID == 0 { // If the ID return is '0' then there is no such email present in the DB
c.Status(fiber.StatusNotFound)
return c.JSON(fiber.Map{
"message": "user not found",
})
}
if err := bcrypt.CompareHashAndPassword(user.Password, []byte(data["password"])); err != nil {
c.Status(fiber.StatusBadRequest)
return c.JSON(fiber.Map{
"message": "incorrect password",
})
} // If the email is present in the DB then compare the Passwords and if incorrect password then return error.
claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{
Issuer: strconv.Itoa(int(user.ID)),
ExpiresAt: time.Now().Add(time.Hour * 24).Unix(), // 1 day
})
token, err := claims.SignedString([]byte(SecretKey))
if err != nil {
c.Status(fiber.StatusInternalServerError)
return c.JSON(fiber.Map{
"message": "could not login",
})
}
cookie := fiber.Cookie{
Name: "jwt",
Value: token,
Expires: time.Now().Add(time.Hour * 24),
HTTPOnly: true,
}
c.Cookie(&cookie)
return c.JSON(fiber.Map{
"message": "success",
})
}
func User(c *fiber.Ctx) error {
cookie := c.Cookies("jwt")
token, err := jwt.ParseWithClaims(
cookie,
&jwt.StandardClaims{},
func(token *jwt.Token) (interface{}, error) {
return []byte(SecretKey), nil
},
)
if err != nil {
c.Status(fiber.StatusUnauthorized)
return c.JSON(fiber.Map{
"message": "unauthenticated",
})
}
claims := token.Claims.(*jwt.StandardClaims)
var user models.User
database.DB.Where("id = ?", claims.Issuer).First(&user)
return c.JSON(user)
}
func Logout(c *fiber.Ctx) error {
cookie := fiber.Cookie{
Name: "jwt",
Value: "",
Expires: time.Now().
Add(-time.Hour),
// Sets the expiry time an hour ago in the past.
HTTPOnly: true,
}
c.Cookie(&cookie)
return c.JSON(fiber.Map{
"message": "success",
})
}