add login logic
This commit is contained in:
1 parent
e459e61fba
commit
81f2943967
6 files changed
+148
-5
No files matched your search
@@ -2,21 +2,29 @@ package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/SowinskiBraeden/BugBeGone/database"
|
||||
"github.com/SowinskiBraeden/BugBeGone/models"
|
||||
"github.com/google/uuid"
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
var userCollection *mongo.Collection = database.OpenCollection(database.Client, "users")
|
||||
var SecretKey string
|
||||
|
||||
func toBase64(b []byte) string {
|
||||
return base64.StdEncoding.EncodeToString(b)
|
||||
func Init() {
|
||||
godotenv.Load(".env")
|
||||
SecretKey = os.Getenv("secret")
|
||||
}
|
||||
|
||||
func Register(c *fiber.Ctx) error {
|
||||
@@ -76,12 +84,15 @@ func Register(c *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
user.UID = uuid.New().String()
|
||||
user.Username = username
|
||||
user.Firstname = firstname
|
||||
user.Lastname = lastname
|
||||
user.Email = email
|
||||
user.Password = user.HashPassword(password)
|
||||
user.TempPassword = false
|
||||
user.Attempts = 0
|
||||
user.Disabled = false
|
||||
user.ID = primitive.NewObjectID()
|
||||
user.Created_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
user.Updated_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
@@ -103,5 +114,126 @@ func Register(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func Login(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{})
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
username := c.FormValue("username")
|
||||
password := c.FormValue("password")
|
||||
|
||||
if username == "" && password == "" {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusBadRequest).Render("login", fiber.Map{
|
||||
"msg": "",
|
||||
"errorMsg": "Username and password can't be blank",
|
||||
})
|
||||
}
|
||||
if username == "" {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusBadRequest).Render("login", fiber.Map{
|
||||
"msg": "",
|
||||
"errorMsg": "Username can't be blank",
|
||||
})
|
||||
}
|
||||
if password == "" {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusBadRequest).Render("login", fiber.Map{
|
||||
"msg": "",
|
||||
"errorMsg": "Password can't be blank",
|
||||
})
|
||||
}
|
||||
|
||||
var user models.User
|
||||
err := userCollection.FindOne(ctx, bson.M{"username": username}).Decode(&user)
|
||||
defer cancel()
|
||||
|
||||
if err != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).Render("login", fiber.Map{
|
||||
"msg": "",
|
||||
"errorMsg": "user not found",
|
||||
})
|
||||
}
|
||||
|
||||
var localAccountDisabled = false
|
||||
if user.Attempts >= 5 {
|
||||
localAccountDisabled = true // Catches newly disbaled account before student obj is updated
|
||||
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"disabled": true,
|
||||
"attempts": 0,
|
||||
"updated_at": update_time,
|
||||
},
|
||||
}
|
||||
|
||||
_, updateErr := userCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"username": username},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).Render("login", fiber.Map{
|
||||
"msg": "",
|
||||
"errorMsg": "the user could not be updated",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if localAccountDisabled || user.Disabled {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusForbidden).Render("login", fiber.Map{
|
||||
"msg": "Account is Disabled, contact support",
|
||||
"errorMsg": "Account is Disabled, contact support",
|
||||
})
|
||||
}
|
||||
|
||||
var verified bool = user.ComparePasswords(password)
|
||||
if verified == false {
|
||||
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"Attempts": user.Attempts + 1,
|
||||
"updated_at": update_time,
|
||||
},
|
||||
}
|
||||
|
||||
_, updateErr := userCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"username": username},
|
||||
update,
|
||||
)
|
||||
cancel()
|
||||
if updateErr != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).Render("login", fiber.Map{
|
||||
"msg": "",
|
||||
"errorMsg": "the student could not be updated",
|
||||
})
|
||||
}
|
||||
return c.Status(fiber.StatusBadRequest).Render("login", fiber.Map{
|
||||
"msg": "",
|
||||
"errorMsg": "incorrect password",
|
||||
})
|
||||
}
|
||||
defer cancel()
|
||||
|
||||
claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{
|
||||
Issuer: user.UID,
|
||||
ExpiresAt: time.Now().Add(time.Hour * 24).Unix(), // 1 Day
|
||||
})
|
||||
token, err := claims.SignedString([]byte(SecretKey))
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).Render("login", fiber.Map{
|
||||
"msg": "",
|
||||
"errorMsg": "could not log in",
|
||||
})
|
||||
}
|
||||
|
||||
cookie := fiber.Cookie{
|
||||
Name: "jwt",
|
||||
Value: token,
|
||||
Expires: time.Now().Add(time.Hour * 24),
|
||||
HTTPOnly: true,
|
||||
}
|
||||
c.Cookie(&cookie)
|
||||
|
||||
return c.Status(fiber.StatusNotImplemented).Render("dasboard", fiber.Map{})
|
||||
}
|
||||
Reference in new issue
Block a user