Have got first run working and publishing to Netlify
This commit is contained in:
parent
b7e0269e9d
commit
30d99eeb9e
22 changed files with 472 additions and 47 deletions
77
handlers/index.go
Normal file
77
handlers/index.go
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
|
||||
"emperror.dev/errors"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gofiber/fiber/v3/middleware/session"
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
"lmika.dev/lmika/weiro/services/sites"
|
||||
)
|
||||
|
||||
var sitePath = regexp.MustCompile(`^/sites/([0-9]+)`)
|
||||
|
||||
type IndexHandler struct {
|
||||
SiteService *sites.Service
|
||||
}
|
||||
|
||||
func (h IndexHandler) Index(c fiber.Ctx) error {
|
||||
hasUserAndSites, err := h.SiteService.HasUsersAsSites(c.Context())
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !hasUserAndSites {
|
||||
return c.Redirect().To("/first-run")
|
||||
}
|
||||
|
||||
user, hasUser := models.GetUser(c.Context())
|
||||
if !hasUser {
|
||||
return c.Redirect().To("/login")
|
||||
}
|
||||
|
||||
if refUrl, rerr := url.Parse(c.Get("Referer")); rerr == nil {
|
||||
if parts := sitePath.FindStringSubmatch(refUrl.Path); len(parts) == 2 {
|
||||
return c.Redirect().To(fmt.Sprintf("/sites/%v/posts", parts[1]))
|
||||
}
|
||||
}
|
||||
|
||||
site, err := h.SiteService.BestSite(c.Context(), user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Redirect().To(fmt.Sprintf("/sites/%v/posts", site.ID))
|
||||
}
|
||||
|
||||
func (h IndexHandler) FirstRun(c fiber.Ctx) error {
|
||||
hasUserAndSites, err := h.SiteService.HasUsersAsSites(c.Context())
|
||||
if err != nil {
|
||||
return err
|
||||
} else if hasUserAndSites {
|
||||
return errors.New("you already have a site")
|
||||
}
|
||||
|
||||
return c.Render("index/first-run", fiber.Map{}, "layouts/bare_with_scripts")
|
||||
}
|
||||
|
||||
func (h IndexHandler) FirstRunSubmit(c fiber.Ctx) error {
|
||||
var req sites.FirstRunRequest
|
||||
if err := c.Bind().Body(&req); err != nil {
|
||||
return errors.Wrap(err, "failed to parse first run request")
|
||||
}
|
||||
|
||||
sess := session.FromContext(c)
|
||||
if err := sess.Regenerate(); err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).SendString("Failed to login")
|
||||
}
|
||||
|
||||
user, site, err := h.SiteService.FirstRun(c.Context(), req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sess.Set("user_id", user.ID)
|
||||
return c.Redirect().To(fmt.Sprintf("/sites/%v/posts", site.ID))
|
||||
}
|
||||
|
|
@ -66,6 +66,7 @@ func (lh *LoginHandler) DoLogin(c fiber.Ctx) error {
|
|||
}
|
||||
|
||||
sess.Set("user_id", user.ID)
|
||||
sess.Delete("_login_challenge")
|
||||
|
||||
return c.Redirect().To("/")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,27 @@ import (
|
|||
"lmika.dev/lmika/weiro/services/auth"
|
||||
)
|
||||
|
||||
func AuthUser(auth *auth.Service) func(c fiber.Ctx) error {
|
||||
func OptionalUser(auth *auth.Service) func(c fiber.Ctx) error {
|
||||
return func(c fiber.Ctx) error {
|
||||
sess := session.FromContext(c)
|
||||
userID, _ := sess.Get("user_id").(int64)
|
||||
if userID == 0 {
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
user, err := auth.GetUser(c.Context(), userID)
|
||||
if err != nil {
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
c.Locals("user", user)
|
||||
c.SetContext(models.WithUser(c.Context(), user))
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func RequireUser(auth *auth.Service) func(c fiber.Ctx) error {
|
||||
return func(c fiber.Ctx) error {
|
||||
sess := session.FromContext(c)
|
||||
userID, _ := sess.Get("user_id").(int64)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue