2024-12-05 20:36:58 -05:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
|
"github.com/onsonr/sonr/pkg/common/response"
|
|
|
|
|
"github.com/onsonr/sonr/pkg/gateway/internal/pages/index"
|
|
|
|
|
"github.com/onsonr/sonr/pkg/gateway/internal/session"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func HandleIndex(c echo.Context) error {
|
|
|
|
|
if isInitial(c) {
|
|
|
|
|
return response.TemplEcho(c, index.InitialView())
|
|
|
|
|
}
|
|
|
|
|
if isExpired(c) {
|
|
|
|
|
return response.TemplEcho(c, index.ReturningView())
|
|
|
|
|
}
|
|
|
|
|
return c.Render(http.StatusOK, "index.templ", nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ╭─────────────────────────────────────────────────────────╮
|
|
|
|
|
// │ Utility Functions │
|
|
|
|
|
// ╰─────────────────────────────────────────────────────────╯
|
|
|
|
|
|
|
|
|
|
// Initial users have no authorization, user handle, or vault address
|
|
|
|
|
func isInitial(c echo.Context) bool {
|
2024-12-06 21:31:20 -05:00
|
|
|
sess, err := session.Get(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
data := sess.Session()
|
|
|
|
|
return data.UserHandle == "" && data.VaultAddress == ""
|
2024-12-05 20:36:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Expired users have either a user handle or vault address
|
|
|
|
|
func isExpired(c echo.Context) bool {
|
2024-12-06 21:31:20 -05:00
|
|
|
sess, err := session.Get(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
data := sess.Session()
|
|
|
|
|
return data.UserHandle != "" || data.VaultAddress != ""
|
2024-12-05 20:36:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returning users have a valid authorization, and either a user handle or vault address
|
|
|
|
|
func isReturning(c echo.Context) bool {
|
2024-12-06 21:31:20 -05:00
|
|
|
sess, err := session.Get(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
data := sess.Session()
|
|
|
|
|
return data.UserHandle != "" && data.VaultAddress != ""
|
2024-12-05 20:36:58 -05:00
|
|
|
}
|