Skip to content

Commit 023f028

Browse files
authored
Merge pull request #171 from skrijwan100/main-616
npm package simple-auth-kit
2 parents dc34645 + c3d955b commit 023f028

File tree

12 files changed

+1181
-0
lines changed

12 files changed

+1181
-0
lines changed

assets/simple-auth-kit.png

1 MB
Loading

projects.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,13 @@ const projects = [
513513
imgSrc:"assets/stackoverflow.png"
514514

515515
},
516+
{
517+
title:"Simple-auth-kit",
518+
url:"https://github.com/ianshulx/React-projects-for-beginners/tree/main-616/simple-auth-kit",
519+
description:"A simple and reusable Node.js authentication package using **MongoDB**, **JWT**, and **bcrypt**. Just plug it into your frontend project and go!",
520+
imgSrc:"assets/Simple-auth-kit.png"
521+
522+
},
516523

517524
];
518525

simple-auth-kit/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.env
3+
*test.js

simple-auth-kit/.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Ignore node_modules and test/dev files
2+
node_modules
3+
.env
4+
*.log
5+
*.test.js
6+
example/
7+
README-template.md

simple-auth-kit/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# simple-auth-kit 🔐
2+
---
3+
## ✨ Provide authentication with write backend code
4+
---
5+
A simple and reusable Node.js authentication package using **MongoDB**, **JWT**, and **bcrypt**. Just plug it into your frontend project and go!
6+
7+
---
8+
## 🚀 Installation
9+
```bash
10+
npm install simple-auth-kit
11+
12+
```
13+
## 🛠️ Usage
14+
15+
```js
16+
const { connectDB, register, login } = require("simple-auth-kit");
17+
18+
// Connect to MongoDB
19+
connectDB("mongodb://localhost:27017/testauth");
20+
21+
// Register a new user
22+
const reg = async () => {
23+
const res = await register({
24+
name: "Rijwan",
25+
26+
address: "India",
27+
gender: "male",
28+
password: "pass123"
29+
});
30+
console.log(res);
31+
};
32+
reg();
33+
34+
// Login the user
35+
const loginuser = async () => {
36+
const res = await login({
37+
38+
password: "pass123"
39+
});
40+
console.log(res);
41+
};
42+
loginuser();
43+
44+
//More sample code are comming
45+
46+
47+

simple-auth-kit/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const {connectDB} = require("./lib/db")
2+
const {register,login,getuser}= require("./lib/auth")
3+
module.exports = {
4+
connectDB,
5+
register,
6+
login,
7+
getuser
8+
};

simple-auth-kit/lib/Usermodle.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const mongoose= require('mongoose')
2+
const {Schema}= mongoose;
3+
const userSchema = new Schema({
4+
username:{
5+
type:String,
6+
require:true
7+
},
8+
email:{
9+
type:String,
10+
require:true,
11+
unique: true,
12+
},
13+
address:{
14+
type:String,
15+
},
16+
gender:{
17+
type:String,
18+
19+
},
20+
password:{
21+
type:String,
22+
require:true
23+
}
24+
},{ timestamps: true })
25+
26+
module.exports=mongoose.model('User',userSchema);

simple-auth-kit/lib/auth.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const User = require("./Usermodle");
2+
const jwt = require("jsonwebtoken");
3+
const JWT_SERECT='fd*x$hT&nB#'
4+
const { hashPassword,comparepassword,fecthuser } = require("./utils");
5+
6+
async function register({ name, email, address, gender, password }) {
7+
try {
8+
9+
10+
const chakeuser = await User.findOne({ email })
11+
console.log(email)
12+
13+
if (chakeuser) {
14+
return { status: false, message: "User already exists" }
15+
}
16+
hashpas = await hashPassword(password)
17+
const newuser = await User({
18+
username: name,
19+
email: email,
20+
address: address,
21+
gender: gender,
22+
password: hashpas
23+
});
24+
await newuser.save()
25+
return { status: true, message: "User registered" };
26+
} catch (error) {
27+
console.log(error)
28+
return { status: false, message: "Some error happend" }
29+
}
30+
}
31+
async function login({email,password}) {
32+
try {
33+
const finduser= await User.findOne({email});
34+
if(!finduser){
35+
return {status:false,message:"Invalid email or password"}
36+
}
37+
const chakepass= await comparepassword(password,finduser.password)
38+
if(!chakepass){
39+
return {status:false,message:"Invalid email or password"}
40+
}
41+
const authtoken= jwt.sign({
42+
user:finduser._id
43+
},JWT_SERECT)
44+
return {status:true,message:"Login Successful",token:authtoken}
45+
46+
} catch (error) {
47+
console.log(error)
48+
return { status: false, message: "Some error happend" }
49+
}
50+
51+
}
52+
async function getuser(authtoken) {
53+
const userdata= await fecthuser(authtoken)
54+
if(!userdata){
55+
return {status:false,message:"Inavlid token"}
56+
}
57+
const userid=userdata.user
58+
const finduser= await User.findById(userid).select("-password")
59+
return finduser
60+
61+
}
62+
module.exports = { register,login ,getuser };

simple-auth-kit/lib/db.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const mongoose = require('mongoose')
2+
3+
async function connectDB(uri) {
4+
try {
5+
await mongoose.connect(uri)
6+
console.log("✅ MongoDB connected");
7+
8+
9+
} catch (error) {
10+
console.error("❌ MongoDB connection error:", err.message);
11+
return {error:'error to connect with your mongodb.'}
12+
}
13+
14+
}
15+
16+
module.exports={connectDB};

simple-auth-kit/lib/utils.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const bcrypt = require("bcrypt")
2+
const jwt = require("jsonwebtoken");
3+
const JWT_SERECT='fd*x$hT&nB#'
4+
async function hashPassword(password) {
5+
console.log(password)
6+
const salt= await bcrypt.genSalt(12)
7+
const hashPassword= await bcrypt.hash(password,salt)
8+
return hashPassword;
9+
}
10+
async function comparepassword(raw,haspass) {
11+
const chakepass= await bcrypt.compare(raw,haspass)
12+
if(chakepass){
13+
return true
14+
}
15+
return false
16+
}
17+
async function fecthuser(authtoken) {
18+
try {
19+
if(!authtoken){
20+
return false
21+
}
22+
const data= jwt.verify(authtoken,JWT_SERECT)
23+
return data
24+
25+
} catch (error) {
26+
console.log(error)
27+
return false
28+
29+
}
30+
}
31+
module.exports={hashPassword,comparepassword,fecthuser}

0 commit comments

Comments
 (0)