Phase 13, part 3: signing up, and several teams per person

On the multi-tenant edition anyone can set up a team: their account, the
company and its website, whose registered domain the team works on (the
profile keeps to it). A person can belong to several teams and switch
between them from the sidebar, or set up another. Inviting someone who has
an account adds them to the team.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 12:17:15 -07:00
co-authored by Claude Opus 5.5
parent 210b1e57fc
commit d5fb1898e8
16 changed files with 545 additions and 13 deletions
+32 -3
View File
@@ -154,19 +154,48 @@ func (s *Server) toSignIn(w http.ResponseWriter, r *http.Request) {
// startSession signs a user in on this browser.
func (s *Server) startSession(w http.ResponseWriter, r *http.Request, u *users.User) error {
token, _, err := users.StartSession(s.DB, u.ID, clientIP(r), r.UserAgent(), time.Now())
team := s.team()
if s.multi() {
// The team they worked in last, else their first.
list, err := users.TeamsOf(s.platform(), u.ID)
if err != nil {
return err
}
team = 0
for _, t := range list {
if t.Status == users.TeamActive && (team == 0 || t.ID == lastTeam(s, u.ID)) {
team = t.ID
}
}
if team == 0 {
return errors.New("you aren't in any team that's open")
}
}
token, _, err := users.StartSessionIn(s.platform(), u.ID, team, clientIP(r), r.UserAgent(), time.Now())
if err != nil {
return err
}
if err := users.SignedIn(s.DB, u.ID, time.Now()); err != nil {
if err := users.SignedIn(s.platform(), u.ID, time.Now()); err != nil {
return err
}
s.setSessionCookie(w, token)
return nil
}
// lastTeam is the team a person's latest session worked in.
func lastTeam(s *Server, user int64) int64 {
var t int64
_ = s.platform().QueryRow(`SELECT team_id FROM sessions WHERE user_id = ? ORDER BY created_at DESC LIMIT 1`, user).Scan(&t)
return t
}
// setSessionCookie gives the browser its session.
func (s *Server) setSessionCookie(w http.ResponseWriter, token string) {
http.SetCookie(w, &http.Cookie{
Name: s.cookieName(sessionCookie), Value: token, Path: "/",
MaxAge: int(users.SessionMax / time.Second), Secure: s.secureCookies(),
HttpOnly: true, SameSite: http.SameSiteLaxMode,
})
return nil
}
// endSession signs this browser out.