First pass of authentication
This commit is contained in:
parent
c943864edc
commit
01c6e9de87
15 changed files with 311 additions and 42 deletions
|
|
@ -1,24 +1,27 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gofiber/fiber/v3/middleware/session"
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
"lmika.dev/lmika/weiro/services/auth"
|
||||
)
|
||||
|
||||
func AuthUser() func(c fiber.Ctx) error {
|
||||
func AuthUser(auth *auth.Service) func(c fiber.Ctx) error {
|
||||
return func(c fiber.Ctx) error {
|
||||
// TEMP - Actually do the auth here
|
||||
user := models.User{
|
||||
ID: 1,
|
||||
Username: "testuser",
|
||||
TimeZone: "Australia/Melbourne",
|
||||
sess := session.FromContext(c)
|
||||
userID, _ := sess.Get("user_id").(int64)
|
||||
if userID == 0 {
|
||||
return c.Redirect().To("/login")
|
||||
}
|
||||
|
||||
user, err := auth.GetUser(c.Context(), userID)
|
||||
if err != nil {
|
||||
return c.Redirect().To("/login")
|
||||
}
|
||||
|
||||
c.Locals("user", user)
|
||||
c.SetContext(models.WithUser(c.Context(), user))
|
||||
log.Printf("User %s authenticated", user.Username)
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue