Function authMiddleware

Middleware to authenticate users with Auth.js

  • Parameters

    • config: AuthConfig

      The Auth.js configuration

    Returns Middleware

    A middleware function that handles authentication by intercepting requests to the Auth.js callbacks

    • Install the @auth/core package to use this middleware. It contains the providers.
    • Be sure to set AUTH_SECRET in your environment variables.
    • The AUTH_BASE_PATH environment variable can be used to set the base path for the Auth.js routes. The default is /api/auth.

    Example usage:

    import { start, router, authMiddleware, text } from "@pulsar-http/core";
    import githubAuthProvider from '@auth/core/providers/github';

    const routes = [
    router.get("/", async () => text("Hello world")),
    ];

    const auth = authMiddleware({
    providers: [
    githubAuthProvider({
    clientId: "YOUR_CLIENT_ID",
    clientSecret: "YOUR_CLIENT_SECRET"
    }),
    ]
    });

    start({
    routes,
    middlewares: [auth],
    });

    In this example, the auth middleware is added to the server, which will handle authentication for the GitHub provider.

    If you access the /api/auth/signin route, the user will be redirected to a page containing a link to sign in with GitHub.