diff --git a/src/modules/auth/login.go b/src/modules/auth/login.go index 7ae16db..e1651e2 100644 --- a/src/modules/auth/login.go +++ b/src/modules/auth/login.go @@ -2,13 +2,50 @@ package auth import ( "acide/src/utils" + "fmt" "net/http" + "time" "github.com/labstack/echo" ) -// Renders the login form -func login(c echo.Context) error { +// Renders the loginPage form +func loginPage(c echo.Context) error { return utils.RenderTempl(c, http.StatusOK, LoginTempl()) } + +func loginFragment(c echo.Context) error { + + navidromeServer := c.FormValue("navidrome-url") + username := c.FormValue("username") + password := c.FormValue("password") + + // TODO: validation + + sessionToken, err := loginService(navidromeServer, username, password) + if err != nil { + errorMessage := fmt.Sprintf("
Error logging in: %s
", err) + return c.HTML(http.StatusBadRequest, errorMessage) + } + + cookie1 := new(http.Cookie) + cookie1.Name = "session-token" + cookie1.Value = sessionToken + cookie1.Expires = time.Now().Add(24 * time.Hour) + cookie1.Path = "/" + cookie1.HttpOnly = true + cookie1.Secure = true + c.SetCookie(cookie1) + + cookie2 := new(http.Cookie) + cookie2.Name = "navidrome-url" + cookie2.Value = navidromeServer + cookie2.Expires = time.Now().Add(24 * time.Hour) + cookie2.Path = "/" + cookie2.HttpOnly = true + cookie2.Secure = true + c.SetCookie(cookie2) + + return c.HTML(http.StatusOK, "
Logged in, redirecting...
") +} diff --git a/src/modules/index/login.go b/src/modules/auth/login.service.go similarity index 90% rename from src/modules/index/login.go rename to src/modules/auth/login.service.go index 83b5e97..adb9719 100644 --- a/src/modules/index/login.go +++ b/src/modules/auth/login.service.go @@ -1,4 +1,4 @@ -package index +package auth import ( "acide/src/utils" @@ -14,7 +14,7 @@ type AuthError struct { // Attempts to login to a navidrome server with the provided credentials. // Returns the session key if succesful, an error otherwise -func login(server, username, password string) (string, error) { +func loginService(server, username, password string) (string, error) { client := resty.New() var loginData utils.AuthSuccess diff --git a/src/modules/auth/login.templ b/src/modules/auth/login.templ index bae60b4..e74bcc9 100644 --- a/src/modules/auth/login.templ +++ b/src/modules/auth/login.templ @@ -4,8 +4,61 @@ import "acide/src/utils" templ LoginTempl() { @utils.SkeletonTempl() { -
- This is the Login template + +
+ Login to Navidrome: +
+
+ + +
+ + +
+ + +
+ +
+
+ Result: +
+
} } diff --git a/src/modules/auth/login_templ.go b/src/modules/auth/login_templ.go index 2bd13a8..ab86c6d 100644 --- a/src/modules/auth/login_templ.go +++ b/src/modules/auth/login_templ.go @@ -43,7 +43,7 @@ func LoginTempl() templ.Component { }() } ctx = templ.InitializeContext(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
This is the Login template
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Login to Navidrome:




Result:
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/src/modules/auth/setup.go b/src/modules/auth/setup.go index 2c59f9f..46efc1e 100644 --- a/src/modules/auth/setup.go +++ b/src/modules/auth/setup.go @@ -26,5 +26,6 @@ func SetupRoutes(g *echo.Group) { // g.GET("/login", echo.WrapHandler(templ.Handler(LoginTempl()))) // To include custom rendering logic: - g.GET("/login", login) + g.GET("/", loginPage) + g.POST("/f/login", loginFragment) } diff --git a/src/modules/index/index.go b/src/modules/index/index.go index a3ca9d7..2534e8f 100644 --- a/src/modules/index/index.go +++ b/src/modules/index/index.go @@ -2,10 +2,8 @@ package index import ( "acide/src/utils" - "fmt" "log" "net/http" - "time" "github.com/labstack/echo" ) @@ -15,45 +13,17 @@ func SetupRoutes(g *echo.Group) { log.Print("Setting up the index module") // To include custom rendering logic: - g.GET("/", index) - g.POST("/f/login", loginFragment) + g.GET("/", indexPage) } -func index(c echo.Context) error { - return utils.RenderTempl(c, http.StatusOK, IndexTempl()) -} +func indexPage(c echo.Context) error { + // If the required cookies are set, redirect to home + _, err1 := c.Cookie("session-token") + _, err2 := c.Cookie("navidrome-url") -func loginFragment(c echo.Context) error { - - navidromeServer := c.FormValue("navidrome-url") - username := c.FormValue("username") - password := c.FormValue("password") - - // TODO: validation - - sessionToken, err := login(navidromeServer, username, password) - if err != nil { - errorMessage := fmt.Sprintf("
Error logging in: %s
", err) - return c.HTML(http.StatusBadRequest, errorMessage) + if err1 != nil || err2 != nil { + return c.Redirect(http.StatusFound, "/auth/") } - cookie1 := new(http.Cookie) - cookie1.Name = "session-token" - cookie1.Value = sessionToken - cookie1.Expires = time.Now().Add(24 * time.Hour) - cookie1.Path = "/" - cookie1.HttpOnly = true - cookie1.Secure = true - c.SetCookie(cookie1) - - cookie2 := new(http.Cookie) - cookie2.Name = "navidrome-url" - cookie2.Value = navidromeServer - cookie2.Expires = time.Now().Add(24 * time.Hour) - cookie2.Path = "/" - cookie2.HttpOnly = true - cookie2.Secure = true - c.SetCookie(cookie2) - - return c.HTML(http.StatusOK, "wrote some cookies :D") + return utils.RenderTempl(c, http.StatusOK, IndexTempl()) } diff --git a/src/modules/index/index.templ b/src/modules/index/index.templ index 02a6b96..bdad796 100644 --- a/src/modules/index/index.templ +++ b/src/modules/index/index.templ @@ -4,61 +4,8 @@ import "acide/src/utils" templ IndexTempl() { @utils.SkeletonTempl() { - -
- Login to Navidrome: -
-
- - -
- - -
- - -
- -
-
- Result: -
-
+
+ Home page :D
} } diff --git a/src/modules/index/index_templ.go b/src/modules/index/index_templ.go index 4e6d460..1536b36 100644 --- a/src/modules/index/index_templ.go +++ b/src/modules/index/index_templ.go @@ -43,7 +43,7 @@ func IndexTempl() templ.Component { }() } ctx = templ.InitializeContext(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Login to Navidrome:




Result:
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Home page :D
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err }