Hashing passwords
Hashing passwords in Goffee can be done by using the hashing object from the context, here is how you can do that:
package controllers
import (
"git.smarteching.com/goffee/core/v2"
)
func Login(c *core.Context) *core.Response {
hashedPassword, err := c.GetHashing().HashPassword("my-password")
}
Checking a password against its hash
To verify a plain password against a stored hash use CheckPasswordHash, it returns true
when they match:
package controllers
import (
"git.smarteching.com/goffee/core/v2"
)
func Login(c *core.Context) *core.Response {
// hashedPassword is the value stored in the database
match, err := c.GetHashing().CheckPasswordHash(hashedPassword, "my-password")
if err != nil {
// handle the error
}
if !match {
return c.Response.SetStatusCode(401).Json(`{"message": "invalid credentials"}`)
}
// proceed with the login
}