propro-utils
v1.5.76
Published
Auth middleware for propro-auth
Downloads
2,214
Maintainers
Readme
propro-utils
propro-utils
is a comprehensive Node.js middleware designed for handling authentication, authorization, and various utility functions for web applications. It provides a robust solution for both server-side and client-side authentication, profile management, and application settings.
Features
- Flexible Authentication: Supports both server-side and client-side authentication.
- Profile Management: Includes routes for updating user profiles, passwords, emails, two-factor authentication, and avatars.
- App Settings: Provides a route for managing application-specific settings.
- Token Refresh: Handles token refreshing for maintaining user sessions.
- Logout Functionality: Implements secure user logout.
- Configurable: Offers a wide range of options for customizing the authentication process.
- Error Handling: Provides robust error management during the authentication process.
Installation
Install the middleware using yarn
:
yarn add propro-utils
Usage
After installing the middleware, you can import and use it in your Express application:
const express = require('express');
const AuthMiddleware = require('propro-utils');
const userSchema = require('./models/user');
const app = express();
const authMiddleware = new AuthMiddleware(
{
authUrl: process.env.AUTH_URL,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
clientUrl: process.env.CLIENT_URL,
redirectUri: process.env.REDIRECT_URI,
appName: process.env.APP_NAME,
appUrl: process.env.APP_URL,
},
userSchema
);
app.use(authMiddleware.middleware());
// Your other routes and middleware
Configuration Options
The AuthMiddleware
constructor accepts an options object with the following properties:
secret
: The secret key used for authentication (default: 'RESTFULAPIs')authUrl
: The authentication URLclientId
: The client IDclientSecret
: The client secretclientUrl
: The client URLredirectUri
: The redirect URIappName
: The application nameappUrl
: The URL of the client application
API Routes
The middleware sets up the following routes:
GET /api/auth
: Initiates the authentication processGET /api/callback
: Handles the callback from the authentication serverPOST /api/refreshToken
: Refreshes the authentication tokenPOST /api/logout
: Logs out the userPATCH /api/profile
: Updates the user profilePATCH /api/profile/password
: Updates the user's passwordPATCH /api/profile/email
: Updates the user's emailPATCH /api/profile/2fa
: Manages two-factor authenticationPATCH /api/profile/avatar
: Updates the user's avatarPATCH /api/app/settings
: Manages application settings
Error Handling
The middleware includes comprehensive error handling for various scenarios, including missing tokens, failed requests, and server errors.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
ISC © 2023 ProPro [email protected]