commit dashboard authentication
This commit is contained in:
1 parent
81f2943967
commit
2ab169ef6b
2 files changed
+41
-3
No files matched your search
@@ -235,5 +235,5 @@ func Login(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
c.Cookie(&cookie)
|
c.Cookie(&cookie)
|
||||||
|
|
||||||
return c.Status(fiber.StatusNotImplemented).Render("dasboard", fiber.Map{})
|
return c.Status(fiber.StatusNotImplemented).Redirect("/dashboard")
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,12 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/SowinskiBraeden/gfbmb/messageBox"
|
"context"
|
||||||
|
|
||||||
|
"github.com/SowinskiBraeden/BugBeGone/models"
|
||||||
|
"github.com/dgrijalva/jwt-go"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Render Index
|
// Render Index
|
||||||
@@ -18,6 +22,40 @@ func RegisterPage(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
func LoginPage(c *fiber.Ctx) error {
|
func LoginPage(c *fiber.Ctx) error {
|
||||||
return c.Render("login", fiber.Map{
|
return c.Render("login", fiber.Map{
|
||||||
"errorMsg": messageBox.EmptyMessageBox(),
|
"msg": "",
|
||||||
|
"errorMsg": "",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func DashboardPage(c *fiber.Ctx) error {
|
||||||
|
cookie := c.Cookies("jwt")
|
||||||
|
|
||||||
|
token, err := jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||||
|
return []byte(SecretKey), nil
|
||||||
|
})
|
||||||
|
// This returns not authorized for both admin and student
|
||||||
|
if err != nil {
|
||||||
|
return c.Status(fiber.StatusUnauthorized).Render("login", fiber.Map{
|
||||||
|
"msg": "not authorized",
|
||||||
|
"errorMsg": "",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
claims := token.Claims.(*jwt.StandardClaims)
|
||||||
|
uid := claims.Issuer
|
||||||
|
|
||||||
|
var user models.User
|
||||||
|
findErr := userCollection.FindOne(context.TODO(), bson.M{"uid": uid}).Decode(&user)
|
||||||
|
if findErr != nil {
|
||||||
|
return c.Status(fiber.StatusUnauthorized).Render("login", fiber.Map{
|
||||||
|
"msg": "User not found",
|
||||||
|
"errorMsg": "",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusAccepted).Render("dashboard", fiber.Map{
|
||||||
|
"msg": "",
|
||||||
|
"errorMsg": "",
|
||||||
|
"user": user,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user