feat: redirects

This commit is contained in:
Araozu 2024-10-07 20:02:54 -05:00
parent fe982dd4ca
commit abba2bfd03
8 changed files with 110 additions and 102 deletions

View File

@ -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("<div class='bg-red-500 text-white p-2 m-2 rounded'>Error logging in: %s</div>", 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, "<div _=\"init js window.location.href = '/'\">Logged in, redirecting...</div>")
}

View File

@ -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

View File

@ -4,8 +4,61 @@ import "acide/src/utils"
templ LoginTempl() {
@utils.SkeletonTempl() {
<div class="font-bold text-white py-2 px-4 m-2 bg-cyan-800 rounded underline">
This is the Login template
<nav class="bg-sky-500 text-white text-xl font-bold p-2">
music to go
</nav>
<div hx-ext="response-targets">
Login to Navidrome:
<br/>
<form
hx-post="/auth/f/login"
hx-swap="innerHTML"
hx-target="#login-result"
hx-target-400="#login-result"
hx-target-500="#login-result"
>
<label for="navidrome-url" class="text-sm">
Navidrome URL:
</label>
<input
id="navidrome-url"
class="w-full py-1 px-2 rounded"
placeholder="https://"
name="navidrome-url"
required
/>
<br/>
<label for="username" class="text-sm">
Username:
</label>
<input
id="username"
class="w-full py-1 px-2 rounded"
placeholder="username"
name="username"
required
/>
<br/>
<label for="password" class="text-sm">
Password:
</label>
<input
id="password"
class="w-full py-1 px-2 rounded"
placeholder="password"
name="password"
type="password"
required
/>
<br/>
<button class="py-2 px-4 my-2 bg-sky-500 text-white rounded" type="submit">
Log-in
</button>
<br/>
<div id="login-result">
Result:
</div>
</form>
</div>
}
}

View File

@ -43,7 +43,7 @@ func LoginTempl() templ.Component {
}()
}
ctx = templ.InitializeContext(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div class=\"font-bold text-white py-2 px-4 m-2 bg-cyan-800 rounded underline\">This is the Login template</div>")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<nav class=\"bg-sky-500 text-white text-xl font-bold p-2\">music to go</nav><div hx-ext=\"response-targets\">Login to Navidrome:<br><form hx-post=\"/auth/f/login\" hx-swap=\"innerHTML\" hx-target=\"#login-result\" hx-target-400=\"#login-result\" hx-target-500=\"#login-result\"><label for=\"navidrome-url\" class=\"text-sm\">Navidrome URL:</label> <input id=\"navidrome-url\" class=\"w-full py-1 px-2 rounded\" placeholder=\"https://\" name=\"navidrome-url\" required><br><label for=\"username\" class=\"text-sm\">Username:</label> <input id=\"username\" class=\"w-full py-1 px-2 rounded\" placeholder=\"username\" name=\"username\" required><br><label for=\"password\" class=\"text-sm\">Password:</label> <input id=\"password\" class=\"w-full py-1 px-2 rounded\" placeholder=\"password\" name=\"password\" type=\"password\" required><br><button class=\"py-2 px-4 my-2 bg-sky-500 text-white rounded\" type=\"submit\">Log-in</button><br><div id=\"login-result\">Result:</div></form></div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}

View File

@ -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)
}

View File

@ -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("<div class='bg-red-500 text-white p-2 m-2 rounded'>Error logging in: %s</div>", 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())
}

View File

@ -4,61 +4,8 @@ import "acide/src/utils"
templ IndexTempl() {
@utils.SkeletonTempl() {
<nav class="bg-sky-500 text-white text-xl font-bold p-2">
music to go
</nav>
<div hx-ext="response-targets">
Login to Navidrome:
<br/>
<form
hx-post="/f/login"
hx-swap="innerHTML"
hx-target="#login-result"
hx-target-400="#login-result"
hx-target-500="#login-result"
>
<label for="navidrome-url" class="text-sm">
Navidrome URL:
</label>
<input
id="navidrome-url"
class="w-full py-1 px-2 rounded"
placeholder="https://"
name="navidrome-url"
required
/>
<br/>
<label for="username" class="text-sm">
Username:
</label>
<input
id="username"
class="w-full py-1 px-2 rounded"
placeholder="username"
name="username"
required
/>
<br/>
<label for="password" class="text-sm">
Password:
</label>
<input
id="password"
class="w-full py-1 px-2 rounded"
placeholder="password"
name="password"
type="password"
required
/>
<br/>
<button class="py-2 px-4 my-2 bg-sky-500 text-white rounded" type="submit">
Log-in
</button>
<br/>
<div id="login-result">
Result:
</div>
</form>
<div>
Home page :D
</div>
}
}

View File

@ -43,7 +43,7 @@ func IndexTempl() templ.Component {
}()
}
ctx = templ.InitializeContext(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<nav class=\"bg-sky-500 text-white text-xl font-bold p-2\">music to go</nav><div hx-ext=\"response-targets\">Login to Navidrome:<br><form hx-post=\"/f/login\" hx-swap=\"innerHTML\" hx-target=\"#login-result\" hx-target-400=\"#login-result\" hx-target-500=\"#login-result\"><label for=\"navidrome-url\" class=\"text-sm\">Navidrome URL:</label> <input id=\"navidrome-url\" class=\"w-full py-1 px-2 rounded\" placeholder=\"https://\" name=\"navidrome-url\" required><br><label for=\"username\" class=\"text-sm\">Username:</label> <input id=\"username\" class=\"w-full py-1 px-2 rounded\" placeholder=\"username\" name=\"username\" required><br><label for=\"password\" class=\"text-sm\">Password:</label> <input id=\"password\" class=\"w-full py-1 px-2 rounded\" placeholder=\"password\" name=\"password\" type=\"password\" required><br><button class=\"py-2 px-4 my-2 bg-sky-500 text-white rounded\" type=\"submit\">Log-in</button><br><div id=\"login-result\">Result:</div></form></div>")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div>Home page :D</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}