DEVELOPMENT ENVIRONMENT

~liljamo/tixe

ref: 6124b16e89cc33cf9e3b2df29c709b1f1465ac53 tixe/middlewares/auth.go -rw-r--r-- 713 bytes
6124b16eJonni Liljamo fix: abort if not logged in... 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */
package middlewares

import (
	"net/http"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

func IsAuthenticated(c *gin.Context) {
	if sessions.Default(c).Get("profile") == nil {
		// TODO: This should probably be validated somehow... DB lookup or something.
		c.Redirect(http.StatusSeeOther, "/login")
		c.Abort()
	} else {
		c.Next()
	}
}

func CanLogin(c *gin.Context) {
	if sessions.Default(c).Get("profile") != nil {
		// Don't allow the login page if logged in
		c.Redirect(http.StatusSeeOther, "/")
	} else {
		c.Next()
	}
}