From c1e0c6defa3ce83aafe74cc8768b97dcbb91e96b Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 11 Sep 2021 00:57:17 +0200 Subject: [PATCH] feat(sessions): use session --- server.go | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/server.go b/server.go index 4e876bd..c21d5a6 100644 --- a/server.go +++ b/server.go @@ -165,10 +165,28 @@ func getNew(c *fiber.Ctx) error { } func mytoken(c *fiber.Ctx) error { - err := c.BodyParser(&SpotifyAPI) + err := c.BodyParser(&SpotifyAPI) if err != nil { Errors = err.Error() + } else { + sess, err := Session.Get(c) + if err != nil { + Errors = err.Error() + } + sess.Set("token", SpotifyAPI.Token) + sess.Set("expire", SpotifyAPI.ExpiresIn) + sess.Set("tokentype", SpotifyAPI.TokenType) + sess.Set("creation", time.Now().GoString()) + err = sess.Save() + + if err != nil { + fmt.Printf("%v+", err) + } } + + //sess.Set("creation", time.Now()) + + return c.SendStatus(201) } @@ -181,14 +199,30 @@ func spotifyCallback(c *fiber.Ctx) error { } func index(c *fiber.Ctx) error { + sess, err := Session.Get(c) + if err != nil { + Errors = err.Error() + } + if Errors == "" { - return c.Render("index", fiber.Map{"connected": !SpotifyAPI.CheckEmpty(), + tmp := false + if sess.Get("token") != nil { + tmp = true + } + + //return c.Render("index", fiber.Map{"connected": !SpotifyAPI.CheckEmpty(), + return c.Render("index", fiber.Map{"connected": tmp, "url": SpotifyURL}) } else { - tmp := Errors + tmp := false + if sess.Get("token") != nil { + tmp = true + } + + e := Errors Errors = "" - return c.Render("index", fiber.Map{"connected": !SpotifyAPI.CheckEmpty(), - "error": tmp, + return c.Render("index", fiber.Map{"connected": tmp, + "error": e, "url": SpotifyURL}) } } @@ -203,7 +237,6 @@ func main() { app.Post("/feudecamp", getNew) app.Post("/back", formHandler) app.Get("/callback", spotifyCallback) - //app.Post("/mytoken", mytoken) app.Listen(":8080") }