mv logic into userService
allows reusing it in the views more easy
This commit is contained in:
@@ -1,17 +1,14 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import(
|
import(
|
||||||
"os"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
|
|
||||||
"github.com/golang-jwt/jwt/v5"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
"example.com/gin/test/models"
|
"example.com/gin/test/models"
|
||||||
"example.com/gin/test/repositories"
|
"example.com/gin/test/repositories"
|
||||||
|
"example.com/gin/test/services"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -40,20 +37,7 @@ func (uc *UserController) Register(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//hash pw
|
_, err = services.Users.Register(body.Name, body.Email, body.Password)
|
||||||
hash, err := bcrypt.GenerateFromPassword([]byte(body.Password), 10)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
|
||||||
"error": "Failed to hash password",
|
|
||||||
})
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//create user
|
|
||||||
user := models.User{Name: body.Name, Email: body.Email, Password: string(hash)}
|
|
||||||
_, err = repositories.Users.Create(user)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error: ", err)
|
fmt.Println("Error: ", err)
|
||||||
@@ -81,46 +65,17 @@ func (uc *UserController) Login(c *gin.Context) {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
"error": "Failed to read body",
|
"error": "Login Failed",
|
||||||
})
|
})
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//lookup requested user
|
tokenString, err := services.Users.Login(body.Email, body.Password)
|
||||||
user, err := repositories.Users.GetByEmail(body.Email)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
"error": "Invalid email or password",
|
"error": "Login Failed",
|
||||||
})
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// compare sent with saved pass
|
|
||||||
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(body.Password))
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
|
||||||
"error": "Invalid email or password",
|
|
||||||
})
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//generate jwt token
|
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
|
||||||
"sub": user.ID,
|
|
||||||
"exp": time.Now().Add(time.Hour * 24).Unix(),
|
|
||||||
})
|
|
||||||
|
|
||||||
// Sign and get the complete encoded token as a string using the secret
|
|
||||||
tokenString, err := token.SignedString([]byte(os.Getenv("SECRET")))
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
|
||||||
"error": "Failed to create token",
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -129,7 +84,6 @@ func (uc *UserController) Login(c *gin.Context) {
|
|||||||
// send it back
|
// send it back
|
||||||
c.SetSameSite(http.SameSiteLaxMode)
|
c.SetSameSite(http.SameSiteLaxMode)
|
||||||
c.SetCookie("Authorization", tokenString, 3600 * 24, "", "", false, true)
|
c.SetCookie("Authorization", tokenString, 3600 * 24, "", "", false, true)
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
c.JSON(http.StatusOK, gin.H{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
68
services/userService.go
Normal file
68
services/userService.go
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package services
|
||||||
|
|
||||||
|
import(
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
|
||||||
|
"github.com/golang-jwt/jwt/v5"
|
||||||
|
|
||||||
|
"example.com/gin/test/models"
|
||||||
|
"example.com/gin/test/repositories"
|
||||||
|
)
|
||||||
|
|
||||||
|
var(
|
||||||
|
Users UserService = UserService{}
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserService struct {}
|
||||||
|
|
||||||
|
func (u *UserService) Register(name string, email string, password string) (models.User, error) {
|
||||||
|
//hash pw
|
||||||
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), 10)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return models.User{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
user := models.User{Name: name, Email: email, Password: string(hash)}
|
||||||
|
_, err = repositories.Users.Create(user)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return models.User{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//return jwt tokenstring on success
|
||||||
|
func (u *UserService) Login(email string, password string) (string, error) {
|
||||||
|
//lookup requested user
|
||||||
|
user, err := repositories.Users.GetByEmail(email)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// compare sent with saved pass
|
||||||
|
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
//generate jwt token
|
||||||
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||||
|
"sub": user.ID,
|
||||||
|
"exp": time.Now().Add(time.Hour * 24).Unix(),
|
||||||
|
})
|
||||||
|
|
||||||
|
// Sign and get the complete encoded token as a string using the secret
|
||||||
|
tokenString, err := token.SignedString([]byte(os.Getenv("SECRET")))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tokenString, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user