diff --git a/controllers/authController.go b/controllers/authController.go
index 94a61f2..fa42e5e 100644
--- a/controllers/authController.go
+++ b/controllers/authController.go
@@ -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")
diff --git a/controllers/viewsController.go b/controllers/viewsController.go
index edc92c5..1973841 100644
--- a/controllers/viewsController.go
+++ b/controllers/viewsController.go
@@ -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,
+ })
+}
diff --git a/routes/routes.go b/routes/routes.go
index a4174c8..8e9ea8f 100644
--- a/routes/routes.go
+++ b/routes/routes.go
@@ -12,6 +12,7 @@ func Setup(app *fiber.App) {
app.Get("/register", controllers.RegisterPage)
app.Get("/login", controllers.LoginPage)
app.Get("/dashboard", controllers.DashboardPage)
+ app.Get("/profile", controllers.ProfilePage)
// Authentication handlers
app.Post("/register", controllers.Register)
diff --git a/views/profile.html b/views/profile.html
new file mode 100644
index 0000000..84d856a
--- /dev/null
+++ b/views/profile.html
@@ -0,0 +1,702 @@
+
+
+
+
+
+
+
+
+
+ Profile - BugBeGone
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Pages
+ Profile
+
+ Profile
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Richard Davis
+
+
+ CEO / Co-Founder
+
+
+
+
+
+
+
+
+
+
+
+
Account
+
+
Application
+
+
+
+
+
+
+
+
+
+ Hi, I’m Alec Thompson, Decisions: If you can’t decide, the answer is no. If two equally difficult paths, choose the one more painful in the short term (pain avoidance is creating an illusion of equality).
+
+
+
+ Full Name: Alec M. Thompson
+ Mobile: (44) 123 1234 123
+ Email: alecthompson@mail.com
+ Location: USA
+
+ Social:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Sophie B.
+
Hi! I need more information..
+
+ Reply
+
+
+
+
+
+
+
Anne Marie
+
Awesome work, can you..
+
+ Reply
+
+
+
+
+
+
+
Ivanna
+
About files I can..
+
+ Reply
+
+
+
+
+
+
+
Peterson
+
Have a great afternoon..
+
+ Reply
+
+
+
+
+
+
+
Nick Daniel
+
Hi! I need more information..
+
+ Reply
+
+
+
+
+
+
+
+
Projects
+
Architects design houses
+
+
+
+
+
+
+
Project #2
+
+
+ Modern
+
+
+
+ As Uber works through a huge amount of internal management turmoil.
+
+
+
+
+
+
+
+
+
+
Project #1
+
+
+ Scandinavian
+
+
+
+ Music is something that every person has his or her own specific opinion about.
+
+
+
+
+
+
+
+
+
+
Project #3
+
+
+ Minimalist
+
+
+
+ Different people have different taste, and various types of music.
+
+
+
+
+
+
+
+
+
+
Project #4
+
+
+ Gothic
+
+
+
+ Why would anyone pick blue over pink? Pink is obviously a better color.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file