Authenticated Express routes with Meteor accounts
Run each use case and copy the matching client and server code.
meteor add accounts-express
auth controls whether an outgoing fetch sends identity.
required controls whether an incoming route rejects anonymous
requests.
Fetch defaults
meteor/accounts-express
auth: true
fetch("/api/me")
Meteor.fetch
auth: false
Meteor.fetch(url, { auth: true })
meteor/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.