Express Reference - PropelAuth Docs

Express Reference

PropelAuth's Express library provides all the building blocks you need to add authentication to your Express projects.

Installation

npm install @propelauth/express

Initialize

initAuth performs a one-time initialization of the library. It will verify your apiKey is correct and fetch the metadata needed to verify access tokens in requireUser and optionalUser.

In serverless environments, it's beneficial to skip the fetch, in which case you can pass in manualTokenVerificationMetadata instead of having the library fetch it.

import { initAuth } from '@propelauth/express';

const {
    requireUser,
    fetchUserMetadataByUserId,
    // ...
} = initAuth({
    authUrl: "REPLACE_ME",
    apiKey: "REPLACE_ME",
});

Protect API Routes

The @propelauth/express library provides an Express middleware requireUser. This middleware will verify the access token and set req.userClass to the User Class if it's valid. Otherwise, the request is rejected with a 401 Unauthorized. You can also use optionalUser if you want the request to proceed in either case.

import { initAuth } from '@propelauth/express';

const { requireUser } = initAuth({ /* ... */ });

app.get("/api/whoami", requireUser, (req, res) => {
    res.text("Hello user with ID " + req.userClass.userId);
});

Verifying the access token doesn't require an external request.

Authorization / Organizations

You can also verify which organizations the user is in, and which roles and permissions they have in each organization all through the User or OrgMemberInfo Classes.

Check Org Membership

Verify that the request was made by a valid user and that the user is a member of the specified organization.

app.get('/api/org/:orgId', requireUser, async (req, res) => {
    const org = req.userClass.getOrg(req.params.orgId)
    if (!org) {
        // return 403 error
    } else {
        res.json(`You are in org ${org.orgName}`)
    }
})

Check Org Membership and Role

Similar to checking org membership, but will also verify that the user has a specific Role in the organization.

app.get('/api/org/:orgId', requireUser, async (req, res) => {
    const org = req.userClass.getOrg(req.params.orgId)
    if (!org || !org.isRole('Owner')) {
        // return 403 error
    } else {
        res.json(`You are an Owner in org ${org.orgName}`)
    }
})

Check Org Membership and Permission

Similar to checking org membership, but will also verify that the user has the specified permission in the organization.

app.get('/api/org/:orgId', requireUser, async (req, res) => {
    const org = req.userClass.getOrg(req.params.orgId)
    if (!org || !org.hasPermission('can_view_billing')) {
        // return 403 error
    } else {
        res.json(`You can view billing information for org ${org.orgName}`)
    }
})

User Class

The User Class contains information about the user who made the request. It also contains additional methods such as getOrgs() and hasPermission().

const propelAuth = require('@propelauth/express')

app.get('/api/whoami', requireUser, (req, res) => {
    const userClass = req.userClass
    res.json('Hello user with ID ' + userClass.userId)
})

OrgMemberInfo

The OrgMemberInfo object contains information about the user's membership in an organization. It can be retrieved by first getting the User Class and then using either getOrgs(), getOrg(), or getActiveOrg().

app.get('/api/org/:orgId', requireUser, async (req, res) => {
    const orgMemberInfo = req.userClass.getOrg(req.params.orgId)
    res.json(`You are in org ${org.orgName}`)
})

Calling Backend APIs

You can also use the library to call the PropelAuth APIs directly, allowing you to fetch users, create orgs, and a lot more.

const auth = initAuth({
    authUrl: 'REPLACE_ME',
    apiKey: 'REPLACE_ME',
})

const magicLink = await auth.createMagicLink({
    email: 'user@customer.com',
})

See the API Reference for more information.