Skip to content

CyberFlaw/quik-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

79 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

quik-auth @0.0.1

image info image info image info image info

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:

  1. Run npm i quik-auth and Require the package

  2. Make a .env file in your root directory and initialize

    DB_CONNECT = Your MongoDB authentification endpoint PRIVATE_KEY = A private key for signing JWT


  3. The function launchServer() takes in 2 arguments
    1. port: Defines which port to start the server
    2. 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!")
    );
    
  4. 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);
    
    

  5. 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" });
      }
    
    
  6. 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 ❤

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published