npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongo-authify

v0.0.3

Published

Authentication for noobs using MongoDB

Downloads

6

Readme

mongo-authify

mongo-authify is an npm package that provides a simple authentication system for your MongoDB database. It allows you to easily manage user signup, login, and password reset functionality with a customizable schema for user data validation.

installation

You can install the package via npm:

npm install mongo-authify

Usage

initialisation

To use mongo-authify, you need to import it and create an instance of the Auth class. You need to provide your MongoDB URL as the first argument to the constructor:

const Auth = require("mongo-authify");

const auth = new Auth("mongodb://localhost:27017/my-database");

The Auth class takes the mongoURL as its argument. You can also pass a schema object, a ROOT url, and a NEW_PASSWORD_ENDPOINT as optional arguments.

Configuring Options

You can also configure various options when initializing Auth:

const auth = new Auth(mongoURL, schema, ROOT, NEW_PASSWORD_ENDPOINT);

auth.config({
    appName: "My App",
    schema: myCustomSchema, // check the scheme section
    ROOT: "https://my-app.com",
    NEW_PASSWORD_ENDPOINT: "/new-password",
    mongoURL: "mongodb://localhost:27017/my-database",
    mailer: {
        service: "Gmail",
        email: "[email protected]",
        password: "mypassword"
    }
});

The available options are:

  • mongoURL (required): the MongoDB URL to connect to
  • schema: the schema for user data validation (default is a simple schema for email and password fields)
  • ROOT: the root URL of your application (default is http://localhost:3000)
  • NEW_PASSWORD_ENDPOINT: the endpoint for resetting passwords (default is /reset-password)
  • appName: the name of your application (default is mongo-authify)
  • mailer: an object containing your email service, email, and password for sending password reset emails. If this option is not provided, the package will not be able to send password reset emails.

Signing Up

To create a new user, call the signup method and provide the required user data as an object:

const user = await auth.signup({
    fullName: "John Doe", // not required if schema.seperateNames===true
    firstName: "John",
    lastName: "Doe",
    password: "password123",
    email: "[email protected]"
});

The signup method signs up a user and returns a status and a message.

Logging in

To log in a user, call the login method and provide the user's email and password:

const user = await auth.login({
    email: "[email protected]",
    password: "password123"
});

The login method logs in a user and returns a user object if the login was successful, otherwise it returns a status and a message.

Forgot Password

To reset a user's password, call the resetPassword method and provide the user's email address:

await auth.resetPassword("[email protected]");

The Password reset url will redirect user to NEW_PASSWORD_ENDPOINT. Make sure to provide input fields to enter new password. The newPasswowrd can be managed by using any version of this code:

app.post('/new-password',async (req, res)=>{
	const {newPassword} = req.body
	const {prstr} = req.query
	const response = await auth.resetPassword({token:prstr, newPassword}) // returns {status:true/false}
	
  // handle response
  
  res.json(response)
})

Schema

A JSON schema is used to validate the user infornation like name, enail, pass, etc. The default Schema is:

const defaultSchema = {
	seperateNames: true,
	fullName: {
		min: 5,
		max: 50,
		required: true,
	},
	firstName: {
		min: 3,
		max: 50,
		required: true,
	},
	lastName: {
		min: 3,
		max: 50,
		required: false,
	},

	email: {
		required: true,
	},
	password: {
		min: 8,
		max: 50,
		required: {
			lowercase: false,
			uppercase: false,
			number: false,
			symbol: false,
		},
	},
};

This schema can be overrided by providing any modified key in schema argument of config() and/or constructor of the package. for example:

const customSchema = {
// make sure to provide all sub-keys of any key in Schema
  password: {
		min: 10,
		max: 128,
		required: {
			lowercase: true,
			uppercase: true,
			number: true,
			symbol: true,
		},
	},
}
const auth = new Auth(mongoURL, customSchema);
// OR
auth.config({
  schema:customSchema,
})

Provisioned Features

  • Email verification
  • Sending Mail on Signup
  • Managing Sessions

Contributing

Anyone interested in improving this repo are welocomed. Make sure to raise issues regarding:

  • Bugs
  • New Features
  • anything else