mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
Real JWT login implementation
This commit is contained in:
+15
-5
@@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
jwt "github.com/appleboy/gin-jwt/v2"
|
jwt "github.com/appleboy/gin-jwt/v2"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
@@ -14,7 +15,7 @@ type User struct {
|
|||||||
Password string `json:"password" bson:"password"`
|
Password string `json:"password" bson:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var USERS_LOCATION = Location{Database: "gohcms", Collection: "users"}
|
var UsersLocation = Location{Database: "gohcms", Collection: "users"}
|
||||||
|
|
||||||
var AuthMiddleware, _ = jwt.New(&jwt.GinJWTMiddleware{
|
var AuthMiddleware, _ = jwt.New(&jwt.GinJWTMiddleware{
|
||||||
Realm: "GohCMS",
|
Realm: "GohCMS",
|
||||||
@@ -26,9 +27,18 @@ var AuthMiddleware, _ = jwt.New(&jwt.GinJWTMiddleware{
|
|||||||
|
|
||||||
func JWTAuthenticator(c *gin.Context) (interface{}, error) {
|
func JWTAuthenticator(c *gin.Context) (interface{}, error) {
|
||||||
var user = User{}
|
var user = User{}
|
||||||
c.BindJSON(&user)
|
err := c.BindJSON(&user)
|
||||||
if user.Email == "dddeschamps2022" && user.Password == "1234" {
|
if err != nil {
|
||||||
return gin.H{"email": "dddeschamps2022"}, nil
|
return nil, errors.New("wrong credentials json format.")
|
||||||
}
|
}
|
||||||
return nil, errors.New("can't verify credentials.")
|
|
||||||
|
_, err = getUniqueDocument(UsersLocation, bson.D{
|
||||||
|
{Key: "email", Value: user.Email},
|
||||||
|
{Key: "password", Value: user.Password},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("wrong email or password.")
|
||||||
|
}
|
||||||
|
|
||||||
|
return gin.H{"email": user.Email}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user