package routes import ( "encoding/json" "log" "time" "github.com/aziis98/lupus-lite/auth" "github.com/aziis98/lupus-lite/database" "github.com/aziis98/lupus-lite/util" "github.com/gofiber/fiber/v2" ) func Api(api fiber.Router) { db, err := database.NewInMemoryDB() if err != nil { log.Fatal(err) } auth := auth.NewInMemoryAuthService(db) requireLogged := RequireLoggedMiddleware(auth) api.Get("/status", func(c *fiber.Ctx) error { s, err := json.MarshalIndent(db, "", " ") if err != nil { return err } log.Println(string(s)) return c.SendString("ok") }) api.Post("/login", func(c *fiber.Ctx) error { var loginForm struct { Username string `form:"username"` Password string `form:"password"` } if err := c.BodyParser(&loginForm); err != nil { return err } token, err := auth.Login(loginForm.Username, loginForm.Password) if err != nil { return err } c.Cookie(&fiber.Cookie{ Name: "sid", Value: token, Path: "/", Expires: time.Now().Add(3 * 24 * time.Hour), }) return c.Redirect("/") }) api.Post("/logout", func(c *fiber.Ctx) error { c.Cookie(&fiber.Cookie{ Name: "sid", Value: "", Path: "/", Expires: time.Now(), }) return c.SendString("ok") }) api.Post("/register", func(c *fiber.Ctx) error { var loginForm struct { Username string `form:"username"` Password string `form:"password"` Password2 string `form:"password2"` } if err := c.BodyParser(&loginForm); err != nil { return err } if err := util.ValidateUsername(loginForm.Username); err != nil { return err } if err := util.ValidatePasswords(loginForm.Password, loginForm.Password2); err != nil { return err } if err := auth.Register(loginForm.Username, loginForm.Password); err != nil { return err } return c.Redirect("/login") }) api.Get("/user", requireLogged, func(c *fiber.Ctx) error { return c.JSON(requestUser(c).PublicUser()) }) }