auth-mongo-setup
v1.0.6
Published
Tired of setting up authentication like it's a Rubik's cube? 𧩠Say hello to auth-mongo-setup, your new best friend in the world of user management!
Downloads
8
Readme
π auth-mongo-setup: Your MongoDB Authentication Wingman! π¦ΈββοΈ
Welcome to the world of MongoDB auth made easy! π
Tired of setting up authentication like it's a Rubik's cube? 𧩠Say hello to auth-mongo-setup, your new best friend in the world of user management!
What's this magic potion? π§ͺ
auth-mongo-setup is like having a tiny authentication wizard living in your code. It's got all the MongoDB and Express.js authentication spells you need, pre-packaged and ready to cast!
Features that'll make you dance the authentication tango ππΊ
- π User Registration: Because "Guest12345" is so last season.
- π Login: Keep the baddies out and your users cozy.
- π§ Forgot Password: For when your users' memory fails them (happens to the best of us).
- π‘οΈ JWT Authentication: Like a forcefield, but for your API.
- π§ Email Verification: Make sure your users are real humans, not clever cats with keyboards.
Installation (Easier than installing a new habit) π§
npm install auth-mongo-setup
Configuration (Customize your magic wand) πͺ
Create a .env file in your project root. Fill it with your arcane secrets:
MONGO_URI=mongodb://localhost:27017/myapp
JWT_SECRET=your_secret_key
CLIENT_URL=http://localhost:3000
SMTP_HOST=smtp.example.com
SMTP_PORT=587
[email protected]
SMTP_PASS=your_email_password
FROM_NAME=Your App Name
[email protected]
Usage (No stack overflow required!) π€
First, sprinkle some magic dust in your main file:
require('dotenv').config();
const express = require('express');
const authMongoSetup = require('auth-mongo-setup');
const app = express();
// Start your engines!
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server vrooming on port ${PORT}`));
Now, let's break down the spells... err, functions:
π§ββοΈ Registration Spell
javascriptCopy// In your client-side code
fetch('http://localhost:3000/api/auth/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: '[email protected]', password: 'excalibur123' })
})
.then(res => res.json())
.then(data => console.log('New wizard registered!', data));
π Login Incantation
fetch('http://localhost:3000/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: '[email protected]', password: 'excalibur123' })
})
.then(res => res.json())
.then(data => {
console.log('Wizard authenticated!', data);
localStorage.setItem('authToken', data.token);
});
π§ Forgot Password Potion
fetch('http://localhost:3000/api/auth/forgot-password', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: '[email protected]' })
})
.then(res => res.json())
.then(data => console.log('Password reset potion sent!', data));
π‘οΈ Protected Route Shield
const { auth } = require('auth-mongo-setup');
app.get('/api/secret-spell', auth, (req, res) => {
res.json({ msg: 'The secret spell is Avada Kedavra... oops, wrong franchise!' });
});