Express Sweet API Reference - v3.0.0
    Preparing search index...

    Class Authentication

    User authentication service using Passport.js. Provides methods for user authentication, logout, and redirect handling.

    // Using in async request handler
    import * as expx from 'express-sweet';

    router.post('/login', async (req, res, next) => {
    const isAuthenticated = await expx.services.Authentication.authenticate(req, res, next);
    res.json(isAuthenticated);
    });
    // Using in sync request handler
    router.post('/login', async (req, res, next) => {
    const isAuthenticated = await expx.services.Authentication.authenticate(req, res, next);
    if (isAuthenticated)
    await expx.services.Authentication.successRedirect(res);
    else
    await expx.services.Authentication.failureRedirect(req, res);
    });
    Index

    Constructors

    Methods

    • Authenticate the user using username and password from request body. Uses Passport.js local strategy for authentication.

      Parameters

      • req: Request

        The HTTP request object containing user credentials

      • res: Response

        The HTTP response object

      • next: NextFunction

        The next middleware function

      Returns Promise<boolean>

      Returns true if authentication is successful, false otherwise

      // For asynchronous requests
      const router = Router();
      router.post('/login', async (req, res, next) => {
      const isAuthenticated = await Authentication.authenticate(req, res, next);
      res.json(isAuthenticated);
      });
    • Log out the user by removing req.user property and clearing the login session.

      Parameters

      • req: Request

        The HTTP request object

      Returns void

      import {Router} from 'express';
      import * as expx from 'express-sweet';

      const router = Router();
      router.get('/logout', (req, res) => {
      expx.services.Authentication.logout(req);
      res.redirect('/');
      });
    • Redirects to the success page after successful authentication. Uses the URL specified in "success_redirect" of config/authentication.js.

      Parameters

      • res: Response

        The HTTP response object

      Returns Promise<void>

      // For synchronous login handling
      const isAuthenticated = await Authentication.authenticate(req, res, next);
      if (isAuthenticated)
      await Authentication.successRedirect(res);
    • Redirects to the failure page after authentication failure. Uses the URL specified in "failure_redirect" of config/authentication.js. Supports both static URLs and dynamic URL functions.

      Parameters

      • req: Request

        The HTTP request object

      • res: Response

        The HTTP response object

      Returns Promise<void>

      // For synchronous login handling
      const isAuthenticated = await Authentication.authenticate(req, res, next);
      if (!isAuthenticated)
      await Authentication.failureRedirect(req, res);