Function createMiddleware

  • Return an Express authentication middleware.

    Parameters

    Returns ((authenticatedParameters?) => ((req, res, next) => Promise<void>))

    An Express middleware accepting server.AuthenticateRequestParameters parameters.

      • (authenticatedParameters?): ((req, res, next) => Promise<void>)
      • Parameters

        Returns ((req, res, next) => Promise<void>)

          • (req, res, next): Promise<void>
          • Parameters

            • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
            • res: Response<any, Record<string, any>>
            • next: NextFunction

            Returns Promise<void>

    Example: Basic

    const fief = require('@fief/fief');
    const fiefExpress = require('@fief/fief/express');
    const express = require('express');

    const fiefClient = new fief.Fief({
    baseURL: 'https://example.fief.dev',
    clientId: 'YOUR_CLIENT_ID',
    clientSecret: 'YOUR_CLIENT_SECRET',
    });
    const fiefAuthMiddleware = fiefExpress.createMiddleware({
    client: fiefClient,
    tokenGetter: fiefExpress.authorizationSchemeGetter(),
    });

    const app = express();
    app.get('/authenticated', fiefAuthMiddleware(), (req, res) => {
    res.json(req.accessTokenInfo);
    });

    Example: Required scope

    app.get(
    '/authenticated-scope',
    fiefAuthMiddleware({ scope: ['required_scope'] }),
    (req, res) => {
    res.json(req.accessTokenInfo);
    },
    );

    Example: Minimum ACR level

    app.get(
    '/authenticated-acr',
    fiefAuthMiddleware({ acr: FiefACR.LEVEL_ONE }),
    (req, res) => {
    res.json(req.accessTokenInfo);
    },
    );

    Example: Required permissions

    app.get(
    '/authenticated-permission',
    fiefAuthMiddleware({ permissions: ['castles:create'] }),
    (req, res) => {
    res.json(req.accessTokenInfo);
    },
    );