feat: redirects
This commit is contained in:
parent
fe982dd4ca
commit
abba2bfd03
@ -2,13 +2,50 @@ package auth
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"acide/src/utils"
|
"acide/src/utils"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Renders the login form
|
// Renders the loginPage form
|
||||||
func login(c echo.Context) error {
|
func loginPage(c echo.Context) error {
|
||||||
|
|
||||||
return utils.RenderTempl(c, http.StatusOK, LoginTempl())
|
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>")
|
||||||
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package index
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"acide/src/utils"
|
"acide/src/utils"
|
||||||
@ -14,7 +14,7 @@ type AuthError struct {
|
|||||||
|
|
||||||
// Attempts to login to a navidrome server with the provided credentials.
|
// Attempts to login to a navidrome server with the provided credentials.
|
||||||
// Returns the session key if succesful, an error otherwise
|
// 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()
|
client := resty.New()
|
||||||
|
|
||||||
var loginData utils.AuthSuccess
|
var loginData utils.AuthSuccess
|
@ -4,8 +4,61 @@ import "acide/src/utils"
|
|||||||
|
|
||||||
templ LoginTempl() {
|
templ LoginTempl() {
|
||||||
@utils.SkeletonTempl() {
|
@utils.SkeletonTempl() {
|
||||||
<div class="font-bold text-white py-2 px-4 m-2 bg-cyan-800 rounded underline">
|
<nav class="bg-sky-500 text-white text-xl font-bold p-2">
|
||||||
This is the Login template
|
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>
|
</div>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ func LoginTempl() templ.Component {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
ctx = templ.InitializeContext(ctx)
|
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 {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
|
@ -26,5 +26,6 @@ func SetupRoutes(g *echo.Group) {
|
|||||||
// g.GET("/login", echo.WrapHandler(templ.Handler(LoginTempl())))
|
// g.GET("/login", echo.WrapHandler(templ.Handler(LoginTempl())))
|
||||||
|
|
||||||
// To include custom rendering logic:
|
// To include custom rendering logic:
|
||||||
g.GET("/login", login)
|
g.GET("/", loginPage)
|
||||||
|
g.POST("/f/login", loginFragment)
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,8 @@ package index
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"acide/src/utils"
|
"acide/src/utils"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
)
|
)
|
||||||
@ -15,45 +13,17 @@ func SetupRoutes(g *echo.Group) {
|
|||||||
log.Print("Setting up the index module")
|
log.Print("Setting up the index module")
|
||||||
|
|
||||||
// To include custom rendering logic:
|
// To include custom rendering logic:
|
||||||
g.GET("/", index)
|
g.GET("/", indexPage)
|
||||||
g.POST("/f/login", loginFragment)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func index(c echo.Context) error {
|
func indexPage(c echo.Context) error {
|
||||||
return utils.RenderTempl(c, http.StatusOK, IndexTempl())
|
// 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 {
|
if err1 != nil || err2 != nil {
|
||||||
|
return c.Redirect(http.StatusFound, "/auth/")
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cookie1 := new(http.Cookie)
|
return utils.RenderTempl(c, http.StatusOK, IndexTempl())
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
|
@ -4,61 +4,8 @@ import "acide/src/utils"
|
|||||||
|
|
||||||
templ IndexTempl() {
|
templ IndexTempl() {
|
||||||
@utils.SkeletonTempl() {
|
@utils.SkeletonTempl() {
|
||||||
<nav class="bg-sky-500 text-white text-xl font-bold p-2">
|
<div>
|
||||||
music to go
|
Home page :D
|
||||||
</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>
|
</div>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ func IndexTempl() templ.Component {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
ctx = templ.InitializeContext(ctx)
|
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 {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user