Added some routes for users

And some fixes
This commit is contained in:
cdricms
2025-01-16 10:51:30 +01:00
parent fee9a237c4
commit 9a6e4a7565
7 changed files with 173 additions and 41 deletions

View File

@@ -61,12 +61,18 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
}
var user core.User
err = core.DB.NewSelect().
count, err := core.DB.NewSelect().
Model(&user).
Where("email = ? AND password = ?", login.Email, login.Password).
Limit(1).
Scan(context.Background())
ScanAndCount(context.Background())
if count == 0 {
core.JSONError{
Status: core.Error,
Message: "User not found.",
}.Respond(w, http.StatusNotFound)
return
}
if err != nil {
core.JSONError{
Status: core.Error,
@@ -86,7 +92,7 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
signed, err := token.SignedString([]byte("hello"))
signed, err := token.SignedString(MySigningKey)
if err != nil {
core.JSONError{
Status: core.Error,
@@ -98,14 +104,13 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
core.JSONSuccess{
Status: core.Success,
Message: "JWT Created",
Data: map[string]string{"jwt": signed},
Data: signed,
}.Respond(w, http.StatusCreated)
}
func AuthJWT(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check if the Authorization header is provided
fmt.Println("Coucou")
authHeader := r.Header.Get("Authorization")
if authHeader == "" {
core.JSONError{