Key points
- Keep invitations in their own table, with a random token stored only as a hash.
- On acceptance, check expiry, revocation and the email address inside a transaction.
- Pending invitations count towards seats, and nobody can grant a role higher than their own.
The moment a SaaS moves from individual users to company accounts, “invite a member” becomes a must-have. It is also where we find some of the quietest bugs in the apps we build or take over: invitations that never expire, links that work for anyone, an admin role handed out by accident. This is the design we have settled on.
A separate table, and no plain-text tokens
Creating a “pending” user straight away mixes up two things: a person who exists, and an offer that can expire. A dedicated invitations table keeps the model clean and lets you invite someone who already has an account in another organisation.
CREATE TABLE invitations (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
account_id BIGINT UNSIGNED NOT NULL,
email VARCHAR(255) NOT NULL, -- stored lowercased
role VARCHAR(20) NOT NULL,
token_hash CHAR(64) NOT NULL UNIQUE, -- sha256 of the token, never the token
invited_by BIGINT UNSIGNED NOT NULL,
expires_at DATETIME NOT NULL, -- UTC
accepted_at DATETIME NULL,
revoked_at DATETIME NULL,
KEY (account_id, email)
);
The emailed token is effectively a temporary password, so we keep only its SHA-256 hash. A database leak, or a table export shared with support, then exposes nothing usable.
Sending the invitation
function inviteMember(PDO $pdo, Account $account, User $by, string $email, string $role): string
{
if (!in_array($role, $by->rolesItCanGrant($account), true)) {
throw new DomainException('You cannot grant this role.');
}
$email = mb_strtolower(trim($email));
$token = bin2hex(random_bytes(32));
// Re-inviting the same address cancels the previous link
$pdo->prepare('UPDATE invitations SET revoked_at = UTC_TIMESTAMP()
WHERE account_id = ? AND email = ? AND accepted_at IS NULL AND revoked_at IS NULL')
->execute([$account->id, $email]);
$pdo->prepare('INSERT INTO invitations (account_id, email, role, token_hash, invited_by, expires_at)
VALUES (?, ?, ?, ?, ?, ?)')
->execute([$account->id, $email, $role, hash('sha256', $token), $by->id,
gmdate('Y-m-d H:i:s', time() + 7 * 86400)]);
return 'https://app.example.com/invitations/accept?token=' . $token; // goes in the email only
}
Two rules prevent most incidents. Lowercase the address, or “Marie@…” and “marie@…” become two invitations. And let each new invitation revoke the previous one: when a customer hits “resend” three times, only the latest link works.
Accepting: check everything, exactly once
function acceptInvitation(PDO $pdo, User $user, string $token): int
{
$pdo->beginTransaction();
$stmt = $pdo->prepare('SELECT * FROM invitations
WHERE token_hash = ? AND accepted_at IS NULL AND revoked_at IS NULL
AND expires_at > UTC_TIMESTAMP()
FOR UPDATE');
$stmt->execute([hash('sha256', $token)]);
$inv = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$inv || $inv['email'] !== mb_strtolower($user->email)) {
$pdo->rollBack();
throw new DomainException('This invitation is invalid or was sent to another address.');
}
$pdo->prepare('INSERT IGNORE INTO memberships (account_id, user_id, role) VALUES (?, ?, ?)')
->execute([$inv['account_id'], $user->id, $inv['role']]);
$pdo->prepare('UPDATE invitations SET accepted_at = UTC_TIMESTAMP() WHERE id = ?')
->execute([$inv['id']]);
$pdo->commit();
return (int) $inv['account_id'];
}
FOR UPDATE locks the row, so two quick clicks on the link cannot create two memberships. The email comparison is the step teams argue about most. Leave it out and a forwarded or intercepted link gives whoever holds it access to the company’s account. If someone signs in with a different address, we show a clear message rather than accepting silently.
Business rules that are easy to forget
- Seats: count pending invitations against the plan limit, or a customer on a five-seat plan can send twenty invites and have all of them accepted.
- Roles: members should only grant roles at or below their own. That is what
rolesItCanGrant()stands for above. - Visibility: show admins the pending invitations, with a revoke button.
- Offboarding: removing a member should also revoke their unaccepted invitations and active sessions.
FAQ
Why a seven-day expiry?
It is a trade-off: long enough to survive someone’s holiday, short enough that an old link buried in an inbox stops working. Shorten it if the data is sensitive.
What if the invitee has no account yet?
The link opens sign-up with the address pre-filled and locked. Once the account exists and the email is confirmed, the invitation is accepted automatically.
Moving your product to team accounts? We design and build multi-user SaaS applications, from roles and permissions to per-seat billing.



