fix panic when not-logged-in user hits root route
This commit is contained in:
parent
9629bd9d74
commit
82ef4b31d4
@ -2,7 +2,6 @@ package routes
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"dk/internal/actions"
|
"dk/internal/actions"
|
||||||
"dk/internal/middleware"
|
|
||||||
"dk/internal/models/users"
|
"dk/internal/models/users"
|
||||||
"dk/internal/router"
|
"dk/internal/router"
|
||||||
"dk/internal/template/components"
|
"dk/internal/template/components"
|
||||||
@ -10,26 +9,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Index(ctx router.Ctx, _ []string) {
|
func Index(ctx router.Ctx, _ []string) {
|
||||||
user := ctx.UserValue("user").(*users.User)
|
user, ok := ctx.UserValue("user").(*users.User)
|
||||||
if user != nil {
|
if !ok || user == nil {
|
||||||
redirectTo := "/explore"
|
components.RenderPage(ctx, "", "intro.html", nil)
|
||||||
switch user.Currently {
|
|
||||||
case "In Town":
|
|
||||||
redirectTo = "/town"
|
|
||||||
case "Exploring":
|
|
||||||
redirectTo = "/explore"
|
|
||||||
default:
|
|
||||||
redirectTo = "/explore"
|
|
||||||
}
|
|
||||||
ctx.Redirect(redirectTo, 303)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
components.RenderPage(ctx, "", "intro.html", nil)
|
switch user.Currently {
|
||||||
|
case "In Town":
|
||||||
|
ctx.Redirect("/town", 303)
|
||||||
|
case "Exploring":
|
||||||
|
ctx.Redirect("/explore", 303)
|
||||||
|
default:
|
||||||
|
ctx.Redirect("/explore", 303)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Move(ctx router.Ctx, _ []string) {
|
func Move(ctx router.Ctx, _ []string) {
|
||||||
user := middleware.GetCurrentUser(ctx)
|
user := ctx.UserValue("user").(*users.User)
|
||||||
dir, err := strconv.Atoi(string(ctx.PostArgs().Peek("direction")))
|
dir, err := strconv.Atoi(string(ctx.PostArgs().Peek("direction")))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SetContentType("text/plain")
|
ctx.SetContentType("text/plain")
|
||||||
|
6
main.go
6
main.go
@ -10,7 +10,6 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"dk/internal/middleware"
|
"dk/internal/middleware"
|
||||||
"dk/internal/session"
|
|
||||||
"dk/internal/models/babble"
|
"dk/internal/models/babble"
|
||||||
"dk/internal/models/control"
|
"dk/internal/models/control"
|
||||||
"dk/internal/models/drops"
|
"dk/internal/models/drops"
|
||||||
@ -23,6 +22,7 @@ import (
|
|||||||
"dk/internal/models/users"
|
"dk/internal/models/users"
|
||||||
"dk/internal/router"
|
"dk/internal/router"
|
||||||
"dk/internal/routes"
|
"dk/internal/routes"
|
||||||
|
"dk/internal/session"
|
||||||
"dk/internal/template"
|
"dk/internal/template"
|
||||||
|
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
@ -174,8 +174,8 @@ func start(port string) error {
|
|||||||
r.Use(middleware.CSRF())
|
r.Use(middleware.CSRF())
|
||||||
|
|
||||||
r.Get("/", routes.Index)
|
r.Get("/", routes.Index)
|
||||||
r.Get("/explore", routes.Explore)
|
r.Use(middleware.RequireAuth()).Get("/explore", routes.Explore)
|
||||||
r.Post("/move", routes.Move)
|
r.Use(middleware.RequireAuth()).Post("/move", routes.Move)
|
||||||
routes.RegisterAuthRoutes(r)
|
routes.RegisterAuthRoutes(r)
|
||||||
routes.RegisterTownRoutes(r)
|
routes.RegisterTownRoutes(r)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user