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

    Class Authentication

    User authentication service using Passport.js.

    Provides static methods for user authentication, logout, and redirect handling. Works with Passport.js local strategy configured in authentication middleware.

    // Basic authentication with JSON response
    import {Router} from 'express';
    import * as expx from 'express-sweet';

    const router = Router();
    router.post('/api/login', async (req, res, next) => {
    const isAuthenticated = await expx.services.Authentication.authenticate(req, res, next);
    res.json({success: isAuthenticated});
    });

    export default router;
    // Authentication with redirect handling
    import {Router} from 'express';
    import * as expx from 'express-sweet';

    const router = Router();
    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);
    });

    export default router;
    Index

    Constructors

    Methods

    • Authenticate user using username and password from request body.

      Uses Passport.js local strategy configured via authentication middleware. Expects req.body.username and req.body.password to be present.

      Parameters

      • req: Request

        HTTP request object containing user credentials in body

      • res: Response

        HTTP response object

      • next: NextFunction

        Next middleware function

      Returns Promise<boolean>

      Returns true if authentication successful, false otherwise

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

      const router = Router();
      router.post('/login', async (req, res, next) => {
      try {
      const isAuthenticated = await expx.services.Authentication.authenticate(req, res, next);
      if (isAuthenticated) {
      res.json({success: true, user: req.user});
      } else {
      res.status(401).json({success: false, message: 'Invalid credentials'});
      }
      } catch (error) {
      next(error);
      }
      });

      export default router;
    • Log out the current user.

      Removes req.user property and clears the login session. This method is synchronous and does not require await.

      Parameters

      • req: Request

        HTTP request object containing the user session

      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('/');
      });

      export default router;
    • Redirect to success page after successful authentication.

      Uses the URL specified in success_redirect option of config/authentication.js. Should be called after successful authentication.

      Parameters

      • res: Response

        HTTP response object

      Returns Promise<void>

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

      const router = Router();
      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);
      }
      });

      export default router;
    • Redirect to failure page after authentication failure.

      Uses the URL specified in failure_redirect option of config/authentication.js. Supports both static URLs and dynamic URL functions. Should be called after failed authentication.

      Parameters

      • req: Request

        HTTP request object

      • res: Response

        HTTP response object

      Returns Promise<void>

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

      const router = Router();
      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);
      }
      });

      export default router;