-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
64 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,56 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"time" | ||
|
||
"bkawk/go-echo/api/models" | ||
"bkawk/go-echo/api/utils" | ||
|
||
"github.com/labstack/echo/v4" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
// RegisterEndpoint handles user registration requests | ||
func LoginPost(c echo.Context) error { | ||
// bind the incoming request body to a User struct | ||
// Get database connection from context | ||
db := c.Get("db").(*mongo.Database) | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
// Validate input | ||
u := new(models.User) | ||
if err := c.Bind(u); err != nil { | ||
return err | ||
} | ||
|
||
// validate user input | ||
if u.Username == "" || u.Password == "" || u.Email == "" { | ||
return c.JSON(http.StatusBadRequest, map[string]string{ | ||
"error": "invalid request body", | ||
}) | ||
// Find user by email or username | ||
var user models.User | ||
collection := db.Collection("users") | ||
err := collection.FindOne(ctx, bson.M{ | ||
"$or": []bson.M{ | ||
{"email": u.Email}, | ||
{"username": u.Email}, | ||
}, | ||
}).Decode(&user) | ||
if err != nil { | ||
return c.JSON(http.StatusUnauthorized, echo.Map{"error": "Invalid credentials"}) | ||
} | ||
|
||
// Check if password matches | ||
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(u.Password)) | ||
if err != nil { | ||
return c.JSON(http.StatusUnauthorized, echo.Map{"error": "Invalid credentials"}) | ||
} | ||
|
||
// add the new user to the database | ||
// (this is a dummy implementation and would be replaced in a real application) | ||
// ... | ||
// Generate JWT token | ||
token, err := utils.GenerateJWT(user.Username, user.Email) | ||
if err != nil { | ||
return c.JSON(http.StatusInternalServerError, echo.Map{"error": "Failed to generate token"}) | ||
} | ||
|
||
// return a success response | ||
return c.JSON(http.StatusOK, map[string]string{ | ||
"message": "user registered successfully", | ||
}) | ||
return c.JSON(http.StatusOK, echo.Map{"token": token}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package utils | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
jwt "github.com/dgrijalva/jwt-go" | ||
) | ||
|
||
func GenerateJWT(username, email string) (string, error) { | ||
// Create a new JWT token | ||
token := jwt.New(jwt.SigningMethodHS256) | ||
|
||
// Set claims | ||
claims := token.Claims.(jwt.MapClaims) | ||
claims["username"] = username | ||
claims["email"] = email | ||
claims["exp"] = time.Now().Add(time.Hour * 72).Unix() | ||
|
||
// Generate encoded token and send it as response. | ||
t, err := token.SignedString([]byte(os.Getenv("JWT_SECRET"))) | ||
if err != nil { | ||
return "", err | ||
} | ||
return t, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters