accounts

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package accounts is tanGO's first-party, optional, installable reusable app: a ready-made HTML email/password register/login/logout flow, built by composing github.com/angvp/tango/auth's primitives rather than adding to them. auth stays primitive, unopinionated machinery; accounts is the batteries-included convenience layer on top for a project that wants a conventional account system without hand-rolling one. Installing it is optional and purely additive — a project with custom identity needs keeps using auth primitives directly and simply doesn't install this package. See docs/guides/accounts.md.

Index

Constants

View Source
const DefaultSessionCookieName = "tango_account_session"

DefaultSessionCookieName is the session cookie's default name, override via WithSessionCookieName. Exported so a host calling RequireLogin or CurrentAccountID/CurrentAccount can reference it instead of hardcoding the string when accounts.New was installed with no session-cookie-name option.

Variables

This section is empty.

Functions

func CurrentAccountID

func CurrentAccountID(ctx *tango.Context, store *db.Store, cookieName string) (int64, bool, error)

CurrentAccountID returns the requesting Account's ID for a host's own View, or ok=false for a missing, invalid, expired, or now-inactive session — consistent with RequireLogin's strict Active check (ticket 07). Thin sugar over the same accountFromToken RequireLogin uses.

cookieName must match whatever accounts.New was configured with (DefaultSessionCookieName if using the default) — mirroring auth.CurrentUserID's own explicit-cookieName shape, since this helper has no other way to know it.

func New

func New(store *db.Store, opts ...Option) tango.App

New constructs the accounts application. opts configures optional accounts-wide behavior; accounts.New(store) with no options is the baseline this milestone establishes and stays valid as later options are added.

func RequireLogin

func RequireLogin(store *db.Store, cookieName string, loginPath string, next tango.View) tango.View

RequireLogin wraps next so it only runs for a request carrying a valid, unexpired session for an Active Account — resolved fresh on every call via accountFromToken, so a deactivated Account's standing session stops working immediately. A request that fails this check (missing session, invalid/expired token, or a now-inactive Account — all treated identically, since Account has only one gate, unlike AdminUser's separate Active/IsStaff tiers) redirects to loginPath with a "next" query parameter pointing back at the original request.

cookieName must match whatever accounts.New was configured with (DefaultSessionCookieName if using the default).

Types

type Account

type Account struct {
	ID           int64  `tango:"pk"`
	Email        string `tango:"unique"`
	PasswordHash string
	Active       bool
	CreatedAt    time.Time
}

Account is the accounts app's own Application user model — a plain identity, credential, activity state, and timestamp. v0.0.1 deliberately ships no permission-shaped field (no IsStaff, Role, Group, or Permission): an app wanting roles or permissions writes its own View wrapper against its own domain data, including Account itself if this app is installed. See docs/guides/accounts.md.

Active gates both a fresh login and every subsequent request through an already-valid session — not just login, unlike auth.SessionUser, which has no notion of Active at all.

func CurrentAccount

func CurrentAccount(ctx *tango.Context, store *db.Store, cookieName string) (Account, bool, error)

CurrentAccount returns the requesting Account's full row for a host's own View, or ok=false under the same conditions as CurrentAccountID.

type AccountSession

type AccountSession struct {
	ID        int64  `tango:"pk"`
	Token     string `tango:"unique"`
	UserID    int64  `tango:"fk=Account,index"`
	ExpiresAt time.Time
}

AccountSession is one active login for an Account: a session token, which Account it belongs to, and a fixed expiry. Its shape is not a free choice — it satisfies auth.CreateSession's reflection contract (Token string, UserID foreign-keyed to Account, ExpiresAt time.Time), mirroring AdminSession's shape without being built on the same code path admin uses. Deleting a session's row logs out that one login without affecting any other session belonging to the same Account.

type Option

type Option func(*accountsConfig)

Option configures accounts.New.

func WithSessionCookieName

func WithSessionCookieName(name string) Option

WithSessionCookieName overrides the session cookie's name, which defaults to "tango_account_session".

func WithSessionDuration

func WithSessionDuration(d time.Duration) Option

WithSessionDuration sets how long a created AccountSession stays valid, overriding the default (30 days — deliberately longer than admin's fixed 24-hour session, since public-user and operator expectations differ).

func WithSignupDisabled

func WithSignupDisabled() Option

WithSignupDisabled opts an installation out of self-service registration. /accounts/register/ stays mounted and responds with a clear closed-registration message — never a bare 404 — while /accounts/login/ remains fully available. Signup is enabled by default.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL