@newput-newlink/authentication
v1.1.3
Published
The Authentication module is a secure and scalable solution for handling user authentication. It provides robust tools to manage user login, registration, and session handling, allowing developers to focus on building application features without reinvent
Keywords
Readme
Newlink Authentication Module
The Authentication module is a secure and scalable solution for handling user authentication. It provides robust tools to manage user login, registration, and session handling, allowing developers to focus on building application features without reinventing the wheel.
Features
JWT Authentication Secure token-based authentication for users.
Password Encryption Automatically hash and store passwords securely.
User Registration Easy-to-use methods for registering new users.
Session Management Manage user sessions with built-in tools.
Extensible Integrates seamlessly with other Newlink modules, such as Authorization and Notification.
Installation
Installation
Use the module via newlink-cli:
npx @newput-newlink/cliInstallation Process
During installation, the CLI will guide you through two key configuration steps:
-> Architecture Selection: Choose between Monolithic or Microservice architecture based on your project requirements. -> Authentication Setup: You'll be prompted to enable the authentication service. Enter 'Y' to include this feature in your installation.
The CLI tool handles all dependency installation and initial setup automatically, creating a production-ready service configured to your specifications. What Happens Next? After completing these steps, the NewLink CLI will:
Install all required dependencies Generate necessary configuration files Set up your chosen architecture Configure the authentication service (if selected) Initialize your project with the selected settings
Now your NewLink service is ready for development and deployment.
API Endpoints
POST /authentication/login- Authenticate a user and generate a JWT token.POST /authentication/register- Register a new user.
npm install @newput-newlink/authenticationEnvironment Variables
The Authentication module requires several environment variables for proper configuration. Below is a table describing each variable, whether it is required, its purpose, and its default value:
| Environment Variable | Required | Description | Default |
| --------------------- | -------- | ------------------------------------ | ----------- |
| JWT_SECRET | Yes | Secret key used for signing JWTs | None |
| JWT_EXPIRATION | No | JWT expiration time in seconds | 3600 |
| OAUTH_CLIENT_ID | No | Client ID for OAuth integrations | None |
| OAUTH_CLIENT_SECRET | No | Client Secret for OAuth integrations | None |
| DB_TYPE | Yes | Database Type | postgres |
| DB_HOST | Yes | Database host | localhost |
| DB_PORT | No | Database port | 5432 |
| DB_USERNAME | Yes | Database username | None |
| DB_PASSWORD | Yes | Database password | None |
| DB_NAME | Yes | Name of the authentication database | None |
Example .env File
Create a .env file in the root directory of your project and populate it with the necessary environment variables:
JWT_SECRET=your_jwt_secret
JWT_EXPIRATION=3600
OAUTH_CLIENT_ID=your_oauth_client_id
OAUTH_CLIENT_SECRET=your_oauth_client_secret
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=auth_user
DB_PASSWORD=auth_password
DB_NAME=auth_dbUsage
Importing the Module
To use the Authentication module, import and configure it in your application:
import { AuthModule } from '@newput-newlink/authentication';
@Module({
imports: [
AuthModule.register({
jwtSecret: process.env.JWT_SECRET,
jwtExpiration: process.env.JWT_EXPIRATION || '3600',
db: {
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
name: process.env.DB_NAME,
},
}),
],
})
export class AppModule {}Example: User Registration
const user = await authService.register({
username: 'john_doe',
password: 'secure_password',
});
console.log('Registered user:', user);Example: User Login
const token = await authService.login({
username: 'john_doe',
password: 'secure_password',
});
console.log('JWT Token:', token);Testing
Run the tests to verify the module's functionality:
npm testContributing
We welcome contributions! Please follow these steps:
- Fork the repository.
- Create a feature branch.
- Commit your changes.
- Submit a pull request with a detailed description.
License
This module is licensed under the MIT License.
