Sessions
Read datapages first for the build loop, hard rules and naming conventions.
Declare the session data and alias once in the app package. Do not declare them when the app has no authentication.
type SessionData struct{ Name string }
type Session = datapages.Session[SessionData]
Use struct{} when the session has no application data. Every handler must use the same data type. Use the shared alias in every handler.
Read
Pages, actions, event handlers and stream hooks may take session Session. It is read-only and provides UserID(), IsGuest(), Token(), IssuedAt(), ExpiresAt() and Data(). Datapages treats an expired session as unauthenticated and deletes its cookie. A stream opened with the session ends when the session closes or reaches ExpiresAt(), and its page reloads after 1s.
Add session Session to every action that must reject a closed or expired session. Without this parameter, Datapages does not read the session store. It checks the CSRF token against the session cookie if the request has one, and accepts a request without a cookie: guests can call the action. Take session Session and check IsGuest() when only signed-in users may call it.
Open and close
Return values, from a GET or an action:
// POSTSignIn is /sign-in
func (*App) POSTSignIn(
r *http.Request,
signals datapages.Signals[struct {
Name string `json:"name"`
}],
) (
newSession datapages.NewSession[SessionData],
redirect datapages.Redirect,
err error,
) {
name := signals.Values.Name
if err := datapages.ValidateUserID(name); err != nil {
return datapages.NewSession[SessionData]{},
datapages.Redirect{}, datapages.ErrBadRequest
}
return datapages.NewSession[SessionData]{
UserID: name,
Data: SessionData{Name: name},
}, datapages.Redirect{URL: href.PageIndex()}, nil
}
NewSession contains UserID, Data and an optional ExpiresAt. Datapages creates the token and sets the issue time. A zero UserID creates no session. To sign out, return closeSession datapages.CloseSession set to true.
ExpiresAt also sets the cookie's Max-Age and Expires attributes. A nonzero value can keep the client signed in after a browser restart. A zero value creates a cookie that the browser deletes when it closes. The session record remains in the store until the application deletes it.
Opening or closing a session cannot be combined with a datapages.SSE parameter. The SSE response headers are sent before the handler runs, so the handler cannot change the cookie. Sign in or out without sse, then return a redirect.
With an offline page cache, sign-in and sign-out actions also take pageCache
and call ClearAll(). A snapshot cached for a guest still shows signed-out
navigation after login. See datapages-offline.
CSRF
CSRF protection is enabled for every app with a session type. Datapages derives the token from the session. Do not set a CSRF header in a template. A normal browser form submission does not include the token. Submit forms through a Datastar action as shown in datapages-templates. Use datapages.WithCSRFProtection(datapages.CSRFConfig{...}) only to replace the token source or disable protection. A QUERYXXX action skips the check. Never change state in it.
Manager
The store is a server option, see datapages-server:
opts = append(opts, datapages.WithSessionManager[app.SessionData](mgr))
Specify the data type in this call. Go cannot infer it here. The compiler checks that the manager stores that type. datapages.NewServer returns an error at startup when it is not the app's session data type.
Use modules/sessions/natskv. Use modules/sessions/inmem only for development. It stores sessions in memory and loses them on restart.
Datapages does not scan the store for expired records. It deletes an expired record only when a client sends that session. Call mgr.DeleteExpired(ctx) on a ticker to delete all other expired records.
<!-- written by datapages sha256:55faea4cf346fc36 -->