refactor, sketch SSE implementation
This commit is contained in:
67
handlers.go
67
handlers.go
@@ -13,7 +13,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"ytdlp-site/config"
|
||||
@@ -23,6 +22,7 @@ import (
|
||||
"ytdlp-site/originals"
|
||||
"ytdlp-site/playlists"
|
||||
"ytdlp-site/transcodes"
|
||||
"ytdlp-site/users"
|
||||
"ytdlp-site/ytdlp"
|
||||
)
|
||||
|
||||
@@ -37,7 +37,7 @@ func registerPostHandler(c echo.Context) error {
|
||||
username := c.FormValue("username")
|
||||
password := c.FormValue("password")
|
||||
|
||||
err := CreateUser(db, username, password)
|
||||
err := users.Create(db, username, password)
|
||||
|
||||
if err != nil {
|
||||
return c.String(http.StatusInternalServerError, "Error creating user")
|
||||
@@ -47,66 +47,13 @@ func registerPostHandler(c echo.Context) error {
|
||||
}
|
||||
|
||||
func homeHandler(c echo.Context) error {
|
||||
|
||||
// redirect to /videos if logged in
|
||||
session, err := store.Get(c.Request(), "session")
|
||||
if err == nil {
|
||||
_, ok := session.Values["user_id"]
|
||||
if ok {
|
||||
fmt.Println("homeHandler: session contains user_id. Redirect to /video")
|
||||
return c.Redirect(http.StatusSeeOther, "/videos")
|
||||
}
|
||||
}
|
||||
|
||||
return c.Render(http.StatusOK, "home.html",
|
||||
map[string]interface{}{
|
||||
"Footer": handlers.MakeFooter(),
|
||||
})
|
||||
}
|
||||
|
||||
func loginHandler(c echo.Context) error {
|
||||
return c.Render(http.StatusOK, "login.html", nil)
|
||||
}
|
||||
|
||||
func loginPostHandler(c echo.Context) error {
|
||||
username := c.FormValue("username")
|
||||
password := c.FormValue("password")
|
||||
|
||||
var user User
|
||||
if err := db.Where("username = ?", username).First(&user).Error; err != nil {
|
||||
return c.String(http.StatusUnauthorized, "Invalid credentials")
|
||||
}
|
||||
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil {
|
||||
return c.String(http.StatusUnauthorized, "Invalid credentials")
|
||||
}
|
||||
|
||||
session, err := store.Get(c.Request(), "session")
|
||||
_, err := handlers.GetUser(c)
|
||||
if err != nil {
|
||||
return c.String(http.StatusInternalServerError, "Unable to retrieve session")
|
||||
return c.Redirect(http.StatusSeeOther, "/login")
|
||||
} else {
|
||||
fmt.Println("homeHandler: session contains user_id. Redirect to /video")
|
||||
return c.Redirect(http.StatusSeeOther, "/videos")
|
||||
}
|
||||
session.Values["user_id"] = user.ID
|
||||
err = session.Save(c.Request(), c.Response().Writer)
|
||||
|
||||
if err != nil {
|
||||
return c.String(http.StatusInternalServerError, "Unable to save session")
|
||||
}
|
||||
|
||||
session, _ = store.Get(c.Request(), "session")
|
||||
_, ok := session.Values["user_id"]
|
||||
if !ok {
|
||||
return c.String(http.StatusInternalServerError, "user_id was not saved as expected")
|
||||
}
|
||||
|
||||
fmt.Println("loginPostHandler: redirect to /download")
|
||||
return c.Redirect(http.StatusSeeOther, "/download")
|
||||
}
|
||||
|
||||
func logoutHandler(c echo.Context) error {
|
||||
session, _ := store.Get(c.Request(), "session")
|
||||
delete(session.Values, "user_id")
|
||||
session.Save(c.Request(), c.Response().Writer)
|
||||
return c.Redirect(http.StatusSeeOther, "/login")
|
||||
}
|
||||
|
||||
func downloadHandler(c echo.Context) error {
|
||||
|
Reference in New Issue
Block a user