-
Notifications
You must be signed in to change notification settings - Fork 0
/
hasher.go
55 lines (40 loc) · 1.55 KB
/
hasher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package middlewares
import (
"fmt"
"github.com/codeflames/cizzors/models"
"github.com/gofiber/fiber/v2"
"golang.org/x/crypto/bcrypt"
)
// Define hashing middleware
func HashingMiddleware(c *fiber.Ctx) error {
// Example: Hashing password from the request body
var user models.User
if err := c.BodyParser(&user); err != nil {
return c.Status(fiber.StatusBadRequest).SendString("Invalid request body")
}
fmt.Println(user)
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Failed to hash password")
}
fmt.Println("hashed password: ", string(hashedPassword))
// Replace the original password with the hashed password in the request context
// c.Context().SetUserValue("hashedPassword", string(hashedPassword))
c.Locals("hashedPassword", string(hashedPassword))
return c.Next()
}
func LoginHashingMiddleware(c *fiber.Ctx) error {
// Example: Hashing password from the request body
// user := models.LoginUserModel{}
user := models.User{}
if err := c.BodyParser(&user); err != nil {
return c.Status(fiber.StatusBadRequest).SendString("Invalid request body")
}
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
// Replace the original password with the hashed password in the request context
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Failed to hash password")
}
c.Context().SetUserValue("hashedPassword", string(hashedPassword))
return c.Next()
}