authmate-js
v1.1.3
Published
Authentication package with MongoDB, JWT, and bcrypt
Maintainers
Readme
🌟 authmate-js 🌟
✨ Secure Authentication Solution for Node.js Applications ✨
A comprehensive and secure authentication package with MongoDB, JWT, and bcrypt for Node.js applications. This package helps you implement user authentication quickly and securely, with tokens managed internally (never exposed to clients)! 🚀
🚀 Installation
Install the package using npm or yarn:
npm install authmate-js
# or
yarn add authmate-js📋 Prerequisites
- MongoDB connection (local or Atlas)
- Node.js environment
- Set up
.envfile with:JWT_SECRET=your_jwt_secret_here MONGO_URI=your_mongodb_connection_string
🌟 Features
- User registration (signup)
- User authentication (login)
- Secure token management (tokens never exposed to clients)
- Password encryption with bcrypt
- User data retrieval
- Logout functionality
- Database connection handling
📖 Usage/Examples
Basic Implementation
import { signup, login, getUser, allusers, logout, connectDb } from 'authmate-js';
// First connect to MongoDB
await connectDb();
// Register a new user
const signupResult = await signup('User Name', '[email protected]', 'password123');
console.log(signupResult);
// { message: "User created successfully", user: { name: "User Name", email: "[email protected]" } }
// Login with credentials
const loginResult = await login('[email protected]', 'password123');
console.log(loginResult);
// { message: "Login successful", token: "jwt_token_here" }
const token = loginResult.token;
// Get user information with token
const userResult = await getUser(token);
console.log(userResult);
// Get all users
const allUsersResult = await allusers();
console.log(allUsersResult);
// { users: [{ _id: "...", name: "User Name", email: "[email protected]" }, ...] }
// Logout user
const logoutResult = await logout(token);
console.log(logoutResult);
// { message: "Logout successful", clearCookie: "token=; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=0" }signup(name, email, password)
Registers a new user.
const result = await signup('User Name', '[email protected]', 'password123');Returns:
- Success:
{ message: "User created successfully", user: { name, email } } - Error:
{ error: "Error message" }
login(email, password)
Authenticates a user and returns a JWT token.
const result = await login('[email protected]', 'password123');Returns:
- Success:
{ message: "Login successful", token: "jwt_token_here" } - Error:
{ error: "Error message" }
Tech Stack
Client: Javascript,Express,Node,Mongodb
Server: Node, Express
🚀 About Me
I'm a full-stack developer passionate about building scalable web applications. I specialize in the MERN stack and Next js And experience integrating AI into web projects. I enjoy solving complex problems, optimizing performance, and working on real-time applications. Besides coding, I actively participate in coding competitions and tech events. Always eager to learn and explore new technologies! 🚀
Appendix
Proficient in MERN Stack, TypeScript,Next js and AI-powered web applications.
Experienced in real-time applications using Socket.IO.
Skilled in database management with MongoDB, Prisma, and Appwrite.
Strong problem-solving abilities with 400+ DSA problems solved.
Active in coding competitions and university tech events. Passionate about building scalable and high-performance applications.
getUser(token)
Retrieves user information based on JWT token.
const result = await getUser('jwt_token_here');Returns:
- Success:
{ user: { _id, name, email } } - Error:
{ error: "Error message" }
allusers()
Gets a list of all users (without password information).
const result = await allusers();Returns:
- Success:
{ users: [{ _id, name, email }, ...] } - Error:
{ error: "Error message" }
logout(token)
Handles user logout.
const result = await logout('jwt_token_here');Returns:
- Success:
{ message: "Logout successful", clearCookie: "..." } - Error:
{ error: "Error message" }
verifytoken(token)
Verifies a JWT token and returns user information.
const result = await verifytoken('jwt_token_here');Returns:
- Success:
{ success: true, user: { _id, name, email } } - Error:
{ message: "Error message" }
🔧 Error Handling
All functions return objects with either success data or error information.
Success response example:
{
message: "Login successful",
token: "jwt_token_here"
}Error response example:
{
error: "Invalid credentials"
}📄 License
ISC
👨💻 Author
Yogesh Tiwari
🤝 Contributing
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.
🌟 Support
Give a ⭐️ if this project helped you!
