StaticauthenticateAuthenticate 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.
HTTP request object containing user credentials in body
HTTP response object
Next middleware function
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;
StaticlogoutLog out the current user.
Removes req.user property and clears the login session. This method is synchronous and does not require await.
HTTP request object containing the user session
StaticsuccessRedirect to success page after successful authentication.
Uses the URL specified in success_redirect option of config/authentication.js.
Should be called after successful authentication.
HTTP response object
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;
StaticfailureRedirect 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.
HTTP request object
HTTP response object
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;
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.
See
Passport.js
Example
Example