add profile page

This commit is contained in:
SowinskiBraeden committed 2022-04-20 17:35:25 -07:00
1 parent e3a811b224
commit 4631fc05d0
4 files changed
+760 -69

No files matched your search

+34 -69
View File
@@ -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,
})
}