add profile page
This commit is contained in:
1 parent
e3a811b224
commit
4631fc05d0
4 files changed
+760
-69
No files matched your search
@@ -27,6 +27,29 @@ func Init() {
|
||||
SecretKey = os.Getenv("secret")
|
||||
}
|
||||
|
||||
func IsAuthorized(c *fiber.Ctx) (bool, models.User) {
|
||||
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
|
||||
if err != nil {
|
||||
return false, models.User{}
|
||||
}
|
||||
|
||||
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 false, models.User{}
|
||||
}
|
||||
|
||||
return true, user
|
||||
}
|
||||
|
||||
func Register(c *fiber.Ctx) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
firstname := c.FormValue("firstname")
|
||||
|
||||
@@ -1,102 +1,67 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/SowinskiBraeden/BugBeGone/models"
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
// Render Index
|
||||
func MainPage(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("index", fiber.Map{
|
||||
"loggedIn": false,
|
||||
authorized, _ := IsAuthorized(c)
|
||||
// If the user is already authorized, redirect to dashboard
|
||||
if authorized {
|
||||
return c.Status(fiber.StatusAccepted).Render("index", fiber.Map{
|
||||
"loggedIn": true,
|
||||
})
|
||||
}
|
||||
|
||||
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("index", fiber.Map{
|
||||
"loggedIn": false,
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusAccepted).Render("index", fiber.Map{
|
||||
"loggedIn": true,
|
||||
"loggedIn": false,
|
||||
})
|
||||
}
|
||||
|
||||
func RegisterPage(c *fiber.Ctx) error {
|
||||
authorized, _ := IsAuthorized(c)
|
||||
// If the user is already authorized, redirect to dashboard
|
||||
if authorized {
|
||||
return c.Redirect("/dashboard")
|
||||
}
|
||||
return c.Render("register", fiber.Map{
|
||||
"errorMsg": "",
|
||||
})
|
||||
}
|
||||
|
||||
func LoginPage(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
|
||||
authorized, user := IsAuthorized(c)
|
||||
// If the user is already authorized, redirect to dashboard
|
||||
if authorized {
|
||||
return c.Status(fiber.StatusAccepted).Redirect("/dashboard")
|
||||
}
|
||||
return c.Status(fiber.StatusUnauthorized).Render("login", fiber.Map{
|
||||
"errorMsg": "",
|
||||
"msg": "",
|
||||
"user": user,
|
||||
})
|
||||
// This returns not authorized
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusUnauthorized).Render("login", fiber.Map{
|
||||
"msg": "",
|
||||
"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": "",
|
||||
"errorMsg": "",
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusAccepted).Redirect("/dashboard")
|
||||
}
|
||||
|
||||
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
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusUnauthorized).Redirect("login")
|
||||
}
|
||||
|
||||
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 {
|
||||
authorized, user := IsAuthorized(c)
|
||||
if !authorized {
|
||||
return c.Status(fiber.StatusUnauthorized).Redirect("/login")
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusAccepted).Render("dashboard", fiber.Map{
|
||||
"errorMsg": "",
|
||||
"msg": "",
|
||||
"user": user,
|
||||
})
|
||||
}
|
||||
|
||||
func ProfilePage(c *fiber.Ctx) error {
|
||||
authorized, user := IsAuthorized(c)
|
||||
if !authorized {
|
||||
return c.Status(fiber.StatusUnauthorized).Redirect("/login")
|
||||
}
|
||||
return c.Status(fiber.StatusAccepted).Render("profile", fiber.Map{
|
||||
"errorMsg": "",
|
||||
"msg": "",
|
||||
"user": user,
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user