0.0.2-0 • Published 5 years ago
quik-auth
Licence
MIT
Version
0.0.2-0
Deps
3
Size
13 kB
Vulns
3
Weekly
0
quik-auth @0.0.1
A small package which makes authentification using express-mongodb and jwt much simpler. The only effort is to build your own mongoose.Schema and set values for a few functions. More features will be added soon...
How to Configure:
Run npm i quik-auth and Require the package
Make a .env file in your root directory and initialize
DB_CONNECT = Your MongoDB authentification endpoint
PRIVATE_KEY = A private key for signing JWT
- The function launchServer() takes in 2 arguments
- port: Defines which port to start the server
- app: This is the return value of Express(), which is used to make some routes
schema();
launchServer(port, app);
app.get(
'/private/route',
auth,
(req,res) => res.send("Its a private route!")
);
Call the function schema()
You can either call the function directly which will use this default schema. You can also pass your own custom schema as an argument to the function so that you can use a custom schema
This is the default schema:
// Importing dependencies
const mongoose = require("mongoose");
// Defining post schema
const userSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
min: 6,
max: 12,
},
email: {
type: String,
required: true,
min: 6,
max: 32,
},
password: {
type: String,
required: true,
min: 6,
max: 124,
},
date: {
type: Date,
default: Date.now,
},
},
{ collection: "users" }
);
module.exports = mongoose.model("User", userSchema);
auth is a Middleware, which can be passed on to your other routes in order to make it private. This will be the middleware code running in the background
const token = req.header("token");
if (!token) return res.status(401).json({ message: "Auth Error" });
try {
const decoded = jwt.verify(token, process.env.PRIVATE_KEY);
req.user = decoded.user;
next();
} catch (err) {
console.log(err);
res.status(500).send({ message: "Invalid Token" });
}
- Hopefully it will workout
Developer Log
The package is still in its early stage and I'm a new developer with little experience. I'll do my best to polish out this package in the upcoming weeks. I'll be adding more features too. I've got many plans and less experience to execute hopefully it will end up all fine.
The repository is kinda a mess right now. It's not in a place to ask for contributors. I'll be fixing it soon, more details on that later. Meanwhile start an Issue if you spot some bugs of errors in my code.
Thanks For Your Support
Run npm i quik-auth and Require the package
Make a .env file in your root directory and initialize
DB_CONNECT = Your MongoDB authentification endpoint
PRIVATE_KEY = A private key for signing JWT
- The function launchServer() takes in 2 arguments
- port: Defines which port to start the server
- app: This is the return value of Express(), which is used to make some routes
schema();
launchServer(port, app);
app.get(
'/private/route',
auth,
(req,res) => res.send("Its a private route!")
);
Call the function schema()
You can either call the function directly which will use this default schema. You can also pass your own custom schema as an argument to the function so that you can use a custom schema
This is the default schema:
// Importing dependencies
const mongoose = require("mongoose");
// Defining post schema
const userSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
min: 6,
max: 12,
},
email: {
type: String,
required: true,
min: 6,
max: 32,
},
password: {
type: String,
required: true,
min: 6,
max: 124,
},
date: {
type: Date,
default: Date.now,
},
},
{ collection: "users" }
);
module.exports = mongoose.model("User", userSchema);
auth is a Middleware, which can be passed on to your other routes in order to make it private. This will be the middleware code running in the background
const token = req.header("token");
if (!token) return res.status(401).json({ message: "Auth Error" });
try {
const decoded = jwt.verify(token, process.env.PRIVATE_KEY);
req.user = decoded.user;
next();
} catch (err) {
console.log(err);
res.status(500).send({ message: "Invalid Token" });
}
- Hopefully it will workout
Developer Log
The package is still in its early stage and I'm a new developer with little experience. I'll do my best to polish out this package in the upcoming weeks. I'll be adding more features too. I've got many plans and less experience to execute hopefully it will end up all fine.
The repository is kinda a mess right now. It's not in a place to ask for contributors. I'll be fixing it soon, more details on that later. Meanwhile start an Issue if you spot some bugs of errors in my code.
Thanks For Your Support
Run npm i quik-auth and Require the package
Make a .env file in your root directory and initialize
DB_CONNECT = Your MongoDB authentification endpoint
PRIVATE_KEY = A private key for signing JWT
- port: Defines which port to start the server
- app: This is the return value of Express(), which is used to make some routes
schema();
launchServer(port, app);
app.get(
'/private/route',
auth,
(req,res) => res.send("Its a private route!")
);
Call the function schema()
You can either call the function directly which will use this default schema. You can also pass your own custom schema as an argument to the function so that you can use a custom schema
This is the default schema:
// Importing dependencies
const mongoose = require("mongoose");
// Defining post schema
const userSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
min: 6,
max: 12,
},
email: {
type: String,
required: true,
min: 6,
max: 32,
},
password: {
type: String,
required: true,
min: 6,
max: 124,
},
date: {
type: Date,
default: Date.now,
},
},
{ collection: "users" }
);
module.exports = mongoose.model("User", userSchema);
auth is a Middleware, which can be passed on to your other routes in order to make it private. This will be the middleware code running in the background
const token = req.header("token");
if (!token) return res.status(401).json({ message: "Auth Error" });
try {
const decoded = jwt.verify(token, process.env.PRIVATE_KEY);
req.user = decoded.user;
next();
} catch (err) {
console.log(err);
res.status(500).send({ message: "Invalid Token" });
}