Skip to content

Commit

Permalink
Made changes to auth so that people can login with either username or…
Browse files Browse the repository at this point in the history
… password
  • Loading branch information
AriaFantom committed Sep 22, 2024
1 parent bae1705 commit 9c110a4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
9 changes: 5 additions & 4 deletions backend/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ func init() {

func Login(c fiber.Ctx) error {

user := new(models.User)
user := new(models.UserLogin)
if err := c.Bind().Body(user); err != nil {
return c.Status(400).JSON(fiber.Map{
"error": err.Error(),
})
}

name, email, password, err := repository.GetUserByEmailOrName(user.Email)
name, email, password, err := repository.GetUserByEmailOrName(user)
if err != nil {
return c.Status(500).JSON(fiber.Map{
"error": err.Error(),
Expand Down Expand Up @@ -79,7 +79,7 @@ func ParseJWT(cookie string) error {
claims := token.Claims.(jwt.MapClaims)
data := claims["email"].(string)

_, _, _, err2 := repository.GetUserByEmailOrName(data)
_, _, _, err2 := repository.GetUserByEmailOrName(&models.UserLogin{Email: data})
if err2 != nil {
return err
}
Expand All @@ -94,7 +94,8 @@ func Register(c fiber.Ctx) error {
"error": err.Error(),
})
}
name, email, _, _ := repository.GetUserByEmailOrName(user.Email)

name, email, _, _ := repository.GetUserByEmailOrName(&models.UserLogin{Email: user.Email})
if email != "" && name != "" {
return c.Status(400).JSON(fiber.Map{
"error": "User already exists",
Expand Down
2 changes: 2 additions & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"InventoryManagement/routes"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/logger"
"log"
)

Expand All @@ -22,6 +23,7 @@ func main() {
ServerHeader: "Askar",
AppName: "Inventory Management",
})
app.Use(logger.New())

conf := config.LoadConfig()

Expand Down
7 changes: 7 additions & 0 deletions backend/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ type User struct {
Name string `json:"name" validate:"required"`
Password string `json:"password" validate:"required"`
}

type UserLogin struct {
ID int
Email string `json:"email"`
Name string `json:"name"`
Password string `json:"password" validate:"required"`
}
10 changes: 9 additions & 1 deletion backend/repository/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repository

import (
"InventoryManagement/database"
"InventoryManagement/models"
"context"
"time"
)
Expand All @@ -21,12 +22,19 @@ func CreateUser(name string, email string, password string) error {
return nil
}

func GetUserByEmailOrName(data string) (string, string, string, error) {
func GetUserByEmailOrName(user *models.UserLogin) (string, string, string, error) {
conn, err := database.Connect()
if err != nil {
panic(err)
}

var data string
if user.Email != "" {
data = user.Email
} else {
data = user.Name
}

var name, email, password string
err = conn.QueryRow(context.Background(), "SELECT name, email, password FROM users WHERE name = $1 OR email = $1", data).Scan(&name, &email, &password)
if err != nil {
Expand Down

0 comments on commit 9c110a4

Please sign in to comment.