jwt-service/internal/controllers/controllers.go
2025-02-15 13:02:36 +03:00

128 lines
2.9 KiB
Go

package controllers
import (
"jwt_service/internal/database"
"jwt_service/internal/models"
"strconv"
"time"
"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt"
"golang.org/x/crypto/bcrypt"
)
const SecretKey = "secret"
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)
}
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)), //issuer contains the ID of the user.
ExpiresAt: time.Now().Add(time.Hour * 24).Unix(), //Adds time to the token i.e. 24 hours.
})
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,
} //Creates the cookie to be passed.
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 //using the SecretKey which was generated in th Login function
})
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",
})
}