ADD: card component and login behavior
This commit is contained in:
@@ -2,7 +2,9 @@ package auth
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
"studia/internal/logger"
|
||||
"studia/internal/user"
|
||||
"time"
|
||||
|
||||
@@ -11,70 +13,77 @@ import (
|
||||
)
|
||||
|
||||
type LoginRequest struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
Email string
|
||||
Password string
|
||||
}
|
||||
|
||||
type RegisterRequest struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
Username string `json:"username"`
|
||||
Email string
|
||||
Password string
|
||||
Username string
|
||||
}
|
||||
|
||||
const defaultRole = "user"
|
||||
|
||||
var secret = []byte("secret")
|
||||
|
||||
func Login(c *gin.Context, db *sql.DB) {
|
||||
func Login(c *gin.Context, db *sql.DB) error {
|
||||
var req LoginRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
if req.Email == "" || req.Password == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Email and password are required"})
|
||||
return
|
||||
return errors.New("Email and password are required")
|
||||
}
|
||||
|
||||
User, err := user.GetUserByEmail(db, req.Email)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid email or password"})
|
||||
return
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid email "})
|
||||
return err
|
||||
}
|
||||
logger.Log.Info().Msgf("User: %+v", User)
|
||||
|
||||
if !user.CheckPasswordHash(db, User.Email, req.Password) {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid email or password"})
|
||||
return
|
||||
err = user.CheckPasswordHash(db, User.Email, req.Password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
token, err := GenerateJWT(User.ID, User.Email, User.Role)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not generate token"})
|
||||
return
|
||||
return err
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"token": token})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Register(c *gin.Context, db *sql.DB) {
|
||||
func Register(c *gin.Context, db *sql.DB) error {
|
||||
var req RegisterRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
// Log the error for debugging purposes
|
||||
logger.Log.Error().Err(err).Msg("Failed to bind JSON for registration")
|
||||
// Respond with a bad request status and an error message
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
||||
return
|
||||
return errors.New("Invalid request")
|
||||
}
|
||||
logger.Log.Info().Msgf("Register Request: %+v", req)
|
||||
if req.Email == "" || req.Password == "" || req.Username == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Email and password are required"})
|
||||
return
|
||||
return errors.New("Email and password are required")
|
||||
}
|
||||
|
||||
error := user.CreateUser(db, req.Email, req.Username, req.Password, []string{defaultRole})
|
||||
if error != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": error.Error()})
|
||||
return
|
||||
err := user.CreateUser(db, req.Email, req.Username, req.Password, []string{defaultRole})
|
||||
if err != nil {
|
||||
logger.Log.Error().Err(err).Msg("Failed to create user")
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err})
|
||||
return err
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "User created successfully"})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GenerateJWT(uuid string, email string, roles []string) (any, error) {
|
||||
|
||||
Reference in New Issue
Block a user