๐Ÿ—๏ธ
๐Ÿ—๏ธ Build a SaaS with Larz ยท LARZ-601 ยท Session 2 of 6

Accounts, Login & API Keys

Every SaaS starts with users. Larz gives you sign-up, login, sessions, roles and API keys as built-in primitives - so you protect a route with a decorator, not a framework's worth of glue.

โฑ 8 min read๐Ÿ“… Updated 2026-07-27๐Ÿ“Š Tables, charts & examples

A SaaS needs users: people who sign up, log in, and get their own data - and often API keys for programmatic access. Auth is notoriously fiddly to build safely, but larz.auth makes it a few lines: secure password handling, sessions, login-required routes, roles, and API keys are all built in. In this session we add accounts to our app.

Turn on auth

Enabling auth wires up user storage, secure passwords (scrypt), sessions, and the routes and decorators you need.

import larz.auth as auth
auth.enable(app)

@app.login_required           # gate any route behind login
@app.get('/dashboard')
def dashboard(req):
    return 'Hi ' + req.user.email   # req.user is the logged-in user
You getFrom
Sign-up / login / logoutBuilt-in auth routes
Secure passwordsscrypt hashing, handled for you
req.user on every requestThe current logged-in user
@login_requiredProtect any route in one line

You don't implement password hashing, session cookies or a user table by hand - you enable auth and use req.user.

API keys and roles

For programmatic access and permissions, Larz has API keys and role-based access built in too.

@app.api_key_required          # require a valid API key
@app.get('/api/v1/data')
def data(req): return {'ok': True}

@app.require_role('admin')     # role-based access control
@app.get('/admin-only')
def secret(req): return 'admins only'
๐ŸŽฏ
Auth as decorators, hardened underneath. @login_required, @api_key_required and @require_role read like plain English, but underneath they use secure password hashing, signed sessions and proper checks. With the auth extra ([[project_larz_framework]]'s larz[auth]), you can even add scannable 2FA. You get real security without hand-rolling the dangerous parts.

Add accounts to the SaaS

  • Enable auth - users, passwords, sessions in one call.
  • Gate routes - @login_required and req.user everywhere.
  • API keys - @api_key_required for programmatic access.
  • Roles - @require_role for admins and tiers.

๐ŸŽฏ Key takeaways

  • Every SaaS starts with users; larz.auth makes accounts a few lines, not a framework of glue.
  • Enable auth to get sign-up/login, secure scrypt passwords, sessions and req.user.
  • Protect routes with @login_required; add @api_key_required and @require_role for API access and permissions.
  • The decorators read simply but use hardened security underneath (and larz[auth] adds 2FA).

Frequently asked

Is Larz's built-in auth actually secure?

Yes - it uses established, secure practices under the friendly decorators: scrypt password hashing (a strong, slow hash designed for passwords), signed session cookies, and proper access checks, plus CSRF protection and other security middleware in the framework. With the larz[auth] extra it adds RFC-verified crypto and TOTP two-factor via the stack. The value is that you get these correct, hard-to-implement-safely pieces without hand-rolling them - which is exactly where home-grown auth usually goes wrong. You still follow basic good practice (a strong secret, HTTPS in production), but the dangerous cryptographic parts are handled for you correctly.

How do API keys differ from login for a SaaS?

Login (sessions) is for humans using your app in a browser; API keys are for programmatic access - a customer's script or server calling your API without a browser session. In a SaaS you typically want both: @login_required for the web dashboard, and @api_key_required for your public API endpoints, often tied to the customer's plan so you can meter or gate usage. Larz supports both as decorators and lets you associate API keys with users and plans, so the same account can log in to the dashboard and hold keys for API access.

Session 2 of 6 complete

Next up: Plans, Trials & Subscriptions

Continue to Session 3 โ†’

Related lessons

More in this course

Course home → · Browse all courses →