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

simple-auth-connection

v1.0.1

Published

An npm package that provides simple authentication such as login , signup and database connection using mongoose

Downloads

10

Readme

simple-auth-connection

Description

An npm package that simplifies MongoDB connection using Mongoose, providing easy-to-use functions for user signup, login, and database setup with encryption using bcryptjs. This package is written entirely in TypeScript.

Installation

npm install simple-auth-connection

Usage

1. Import the Package

const { start, createUser, loginUser } = require('simple-auth-connection');

Or for ES6 modules

import { start, createUser, loginUser } from 'simple-auth-connection';

2. Start MongoDB Connection

Provide MongoDB URI to start the connection

await start('YOUR_MONGODB_URI');

3. User Signup

const userData = {
  name: 'John Doe',
  email: '[email protected]',
  password: 'securepassword',
};

try {
  const newUser = await createUser(userData);
  console.log('User created:', newUser);
} catch (error) {
  console.error('Error creating user:', error);
}

4. User Login

const loginData = {
  email: '[email protected]',
  password: 'securepassword',
};

try {
  const token = await loginUser(loginData);
  console.log('Login successful. Token:', token);
} catch (error) {
  console.error('Invalid credentials:', error);
}

JWT Token :

Upon successful login using loginUser, a JWT token is returned. This token contains the email of the logged-in user as the payload. Use this token for subsequent authenticated requests by including it in the Authorization header.

Functions :

start(MONGODB_URI: string): Promise<void> Initializes the MongoDB connection. Pass your MongoDB URI as an argument.

createUser(userData: { name: string, email: string, password: string }): Promise<object> Creates a new user in the database. Provide user data including name, email, and password.

loginUser(loginData: { email: string, password: string }): Promise<string> Logs in the user. Provide login data with email and password. Return jwt token .

Security :

The package uses bcryptjs to encrypt user passwords, ensuring secure storage.

Login and JWT Tokens:

Upon successful login using loginUser, a JSON Web Token (JWT) is generated and returned.

Example of Using JWT Token in Authenticated Requests

const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'; // Replace with the actual token

// Include the token in the Authorization header
fetch('your_authenticated_endpoint', {
  method: 'GET',
  headers: {
    'Authorization': `Bearer ${token}`
  },
})
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

Additional Notes

The package automatically creates a MongoDB collection named 'users' to store user data. Passwords are encrypted using bcryptjs for security.

Contributing

Contributions, issues, and feature requests are welcome! Open an issue or submit a pull request.

Fork the repository on GitHub. Create a branch for your changes. Commit your changes with descriptive messages. Push your changes to your fork. Submit a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author