accounts-express examples Package docs ↗

Authenticated Express routes with Meteor accounts

Run each use case and copy the matching client and server code.

Install meteor add accounts-express
Session Checking session... Reading Meteor account state

auth controls whether an outgoing fetch sends identity. required controls whether an incoming route rejects anonymous requests.

Fetch defaults

CallDefaultAuthenticated usage
Package fetchmeteor/accounts-express auth: true fetch("/api/me")
Meteor.fetchMeteor.fetch auth: false Meteor.fetch(url, { auth: true })
Module fetchmeteor/fetch auth: false fetch(url, { auth: true })

Server only: { token } supplies an explicit login token and implies auth: true. auth: false overrides it.

Use cases

Protected profileRequired authentication GET /api/me

Returns 401 without a token. Compare each fetch entry point.

Invalid credentialsRequired vs optional routes Bearer invalid-token

Shows how the same invalid Bearer token becomes a 401 or an anonymous request.

Public or personalized feedOptional authentication GET /api/feed

Always returns 200. The payload changes when a valid user is present.

Forward request contextServer to server GET /api/proxy/context

Forwards the current request token to a same-origin protected endpoint.

Pass an explicit tokenServer only GET /api/proxy/explicit

Uses { token } when trusted server code already holds a login token.

Protect a routerAuthenticated JSON POST POST /api/workspace/notes

Applies the middleware once to an Express router containing multiple routes.