add logout and better redirects
This commit is contained in:
1 parent
106c24546e
commit
6257cb2bcb
5 files changed
+84
-19
No files matched your search
@@ -2,6 +2,7 @@ package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
@@ -247,3 +248,18 @@ func Login(c *fiber.Ctx) error {
|
||||
|
||||
return c.Status(fiber.StatusNotImplemented).Redirect("/dashboard")
|
||||
}
|
||||
|
||||
func Logout(c *fiber.Ctx) error {
|
||||
fmt.Println("here")
|
||||
cookie := fiber.Cookie{
|
||||
Name: "jwt",
|
||||
Value: "",
|
||||
Expires: time.Now().Add(-time.Hour),
|
||||
HTTPOnly: true,
|
||||
}
|
||||
c.Cookie(&cookie)
|
||||
|
||||
return c.Status(fiber.StatusOK).Render("index", fiber.Map{
|
||||
"loggedIn": false,
|
||||
})
|
||||
}
|
||||
@@ -11,7 +11,32 @@ import (
|
||||
|
||||
// Render Index
|
||||
func MainPage(c *fiber.Ctx) error {
|
||||
return c.Render("index", fiber.Map{})
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
func RegisterPage(c *fiber.Ctx) error {
|
||||
@@ -21,22 +46,15 @@ func RegisterPage(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func LoginPage(c *fiber.Ctx) error {
|
||||
return c.Render("login", fiber.Map{
|
||||
"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
|
||||
// This returns not authorized
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusUnauthorized).Render("login", fiber.Map{
|
||||
"msg": "not authorized",
|
||||
"msg": "",
|
||||
"errorMsg": "",
|
||||
})
|
||||
}
|
||||
@@ -48,7 +66,7 @@ func DashboardPage(c *fiber.Ctx) error {
|
||||
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",
|
||||
"msg": "",
|
||||
"errorMsg": "",
|
||||
})
|
||||
}
|
||||
|
||||
+2
-1
@@ -11,11 +11,12 @@ func Setup(app *fiber.App) {
|
||||
app.Get("/", controllers.MainPage)
|
||||
app.Get("/register", controllers.RegisterPage)
|
||||
app.Get("/login", controllers.LoginPage)
|
||||
app.Get("/dashboard", controllers.DashboardPage)
|
||||
app.Get("/dashboard", controllers.LoginPage)
|
||||
|
||||
// Authentication handlers
|
||||
app.Post("/register", controllers.Register)
|
||||
app.Post("/login", controllers.Login)
|
||||
app.Post("/logout", controllers.Logout)
|
||||
|
||||
// 404 Handler
|
||||
app.Use(func(c *fiber.Ctx) error {
|
||||
|
||||
@@ -6,8 +6,12 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
</head>
|
||||
<body>
|
||||
{{ .username }}
|
||||
{{ .email }}
|
||||
{{ .firstname }} {{ .lastname }}
|
||||
<h3>{{ .username }}</h3>
|
||||
<h3>{{ .email }}</h3>
|
||||
<h3>{{ .firstname }} {{ .lastname }}</h3>
|
||||
|
||||
<form action="/logout" method="POST">
|
||||
<button type="submit">Logout</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
+30
-4
@@ -16,7 +16,7 @@
|
||||
<link rel="stylesheet" href="static/styles/style.css">
|
||||
<script src="https://kit.fontawesome.com/ccff26c93b.js" crossorigin="anonymous"></script>
|
||||
</head>
|
||||
<body>
|
||||
<body onload="verify()">
|
||||
|
||||
<!-- header -->
|
||||
<header class="header" id="intro">
|
||||
@@ -32,7 +32,7 @@
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse">
|
||||
<ul class="navbar-nav">
|
||||
<ul id="list" class="navbar-nav">
|
||||
<li class="nav-item">
|
||||
<a href="#info" class="nav-link">info</a>
|
||||
</li>
|
||||
@@ -48,10 +48,10 @@
|
||||
<li class="nav-item">
|
||||
<a href="#contact" class="nav-link">contact</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<li class="nav-item" id="signup-link">
|
||||
<a href="/register" class="nav-link">sign up</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<li class="nav-item" id="login-link">
|
||||
<a href="/login" class="nav-link">login</a>
|
||||
</li>
|
||||
</ul>
|
||||
@@ -307,5 +307,31 @@
|
||||
<script src="static/resources/OwlCarousel2-2.3.4/dist/owl.carousel.js"></script>
|
||||
<script src="static/resources/WOW-master/dist/wow.js"></script>
|
||||
<script src="static/scripts/script.js" ></script>
|
||||
|
||||
<script>
|
||||
function verify() {
|
||||
let isLoggedIn = {{ .loggedIn }}
|
||||
if (isLoggedIn) {
|
||||
// Add dashboard link
|
||||
let a = document.getElementById("list");
|
||||
let candidate = document.getElementById("candidate");
|
||||
let li = document.createElement("li");
|
||||
li.setAttribute('class', 'nav-item');
|
||||
let link = document.createElement("a");
|
||||
link.setAttribute('href', '/dashboard')
|
||||
link.setAttribute('class', 'nav-link')
|
||||
link.appendChild(document.createTextNode("dashboard"));
|
||||
li.appendChild(link);
|
||||
a.appendChild(li);
|
||||
|
||||
// Remove login/signup links
|
||||
// Declaring a variable to get select element
|
||||
let item = document.getElementById('login-link');
|
||||
a.removeChild(item);
|
||||
item = document.getElementById('signup-link')
|
||||
a.removeChild(item)
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user