2025-01-25 19:49:57 +00:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/1f349/lavender/auth"
|
|
|
|
"github.com/1f349/lavender/auth/authContext"
|
2025-02-24 17:17:28 +00:00
|
|
|
process "github.com/1f349/lavender/auth/process"
|
|
|
|
"github.com/1f349/lavender/database"
|
2025-01-25 19:49:57 +00:00
|
|
|
"github.com/1f349/lavender/logger"
|
2025-02-24 17:17:28 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2025-01-25 19:49:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ auth.Provider = (*InitialLogin)(nil)
|
2025-02-24 17:17:28 +00:00
|
|
|
var _ auth.Form = (*InitialLogin)(nil)
|
2025-01-25 19:49:57 +00:00
|
|
|
|
2025-02-24 17:17:28 +00:00
|
|
|
type InitialLogin struct {
|
|
|
|
DB *database.Queries
|
|
|
|
}
|
2025-01-25 19:49:57 +00:00
|
|
|
|
2025-02-24 17:17:28 +00:00
|
|
|
func (m *InitialLogin) AccessState() process.State { return process.StateUnauthorized }
|
2025-01-25 19:49:57 +00:00
|
|
|
|
|
|
|
func (m *InitialLogin) Name() string { return "base" }
|
|
|
|
|
2025-02-24 17:17:28 +00:00
|
|
|
func (m *InitialLogin) RenderTemplate(ctx authContext.TemplateContext) error {
|
2025-01-25 19:49:57 +00:00
|
|
|
type s struct {
|
|
|
|
UserEmail string
|
|
|
|
Redirect string
|
|
|
|
}
|
|
|
|
|
|
|
|
req := ctx.Request()
|
|
|
|
q := req.URL.Query()
|
|
|
|
cookie, err := req.Cookie("lavender-user-memory")
|
|
|
|
if err == nil && cookie.Valid() == nil {
|
|
|
|
ctx.Render(s{
|
|
|
|
UserEmail: cookie.Value,
|
|
|
|
Redirect: q.Get("redirect"),
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Render(s{
|
|
|
|
UserEmail: "",
|
|
|
|
Redirect: q.Get("redirect"),
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *InitialLogin) AttemptLogin(ctx authContext.FormContext) error {
|
|
|
|
req := ctx.Request()
|
|
|
|
userEmail := req.FormValue("email")
|
|
|
|
rememberMe := req.FormValue("remember-me")
|
|
|
|
logger.Logger.Debug("Hi", "em", userEmail, "rm", rememberMe)
|
2025-02-24 17:17:28 +00:00
|
|
|
|
|
|
|
rw := ctx.ResponseWriter()
|
|
|
|
now := time.Now()
|
|
|
|
future := now.AddDate(1, 0, 0)
|
|
|
|
http.SetCookie(rw, &http.Cookie{
|
|
|
|
Name: "lavender-user-memory",
|
|
|
|
Value: userEmail,
|
|
|
|
Path: "/",
|
|
|
|
Expires: future,
|
|
|
|
MaxAge: int(future.Sub(now).Seconds()),
|
|
|
|
Secure: true,
|
|
|
|
SameSite: http.SameSiteLaxMode,
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx.UpdateSession(process.LoginProcessData{State: process.StateBase})
|
|
|
|
|
2025-01-25 19:49:57 +00:00
|
|
|
return nil
|
|
|
|
}
|