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

@zentechdev/emuser

v0.0.5

Published

Express and Mongo stack user models and endpoints

Downloads

3

Readme

Express and Mongo stack user models and endpoints

🏠 Homepage

Install

npm install @zentechdev/emuser --save

Usage

Using this library requires that the application has a valid Mongoose connection to a database.

Some handlers require body parameters, somerequire URL parameters, some require authentication, and some are a some combination of those three. Most IDEs will show the handler's documentation comment if you hover over the name of the function, which will tell you what is required for using that handler. Additionally, you can find it in the chart below.

// Import model and handlers
const { User: model, handlers } = require('@zentechdev/emuser');

// Interact with user model, provides direct access to mongoose model
const user1 = User.model.findOne({ username: req.user.username });

// Use user handler in an endpoint, check documentation for handler requirements
const express = require('express');
const router = express.Router();
router.get('/api/v1/login', handlers.postLogin);

Run tests

npm run test

Modules

This section will go more in depth into the modules available in this package and provide examples of how to use them.

Models

The models module contains the Mongoose model for the users and utility tokens. The models are used by the handlers to interact with the database. The models can also be used directly to interact with the database.

The first model is the User model. This represents a user in the database. The following table displays the fields of this model.

User Schema | Field Name | Type | Required | | ----------- | ------ | -------- | | username | String | Yes | | password | String | Yes | | firstName | String | Yes | | lastName | String | Yes | | userType | String | Yes | | email | String | Yes | | image | String | No |

Examples

const { User: model } = require('@zentechdev/emuser');

// must call query functions from inside an async function and use
// await or a callback, or use the .then() construction

const getUserByUsername = async (username) => {
    const user await User.findOne({ username: 'user123' });
    return user;
};

const newUser = new User({
    firstName: 'Test',
    lastName: 'User',
    username: 'testuser123',
    password: 'password2022',
    email: '[email protected]',
    userType: 'member'
});
await newUser.save();

Handlers

The handlers module containts the functions that will interact with the database using the request object and constructing a response using the response object. These functions should be passed directly into the Express router endpoints, as shown below. The requirements to use each function are also shown below

Handler Functions | Function Name | Requires Auth | URL Params | Body Params | Purpose | | ------------------ | ------------- | ---------- | ----------- | ------- | | getCurrentUserInfo | Yes | None | None | Returns current user's info | | getUserAuth | Yes | None | None | Returns current user's auth status | | getUserInfo | Yes | username | None | Returns given user's info | | postUser | No | None | username, firstName, lastName, email, userType, and password | Registers a new user | | postLogin | No | None | username, password | Logs a user in and returns a token | | postProfilePicture | Yes | None | File object uploaded in a form | Updates a user's profile picture with the uploaded image | | getProfilePicture | No | fileName | None | Returns a profile picture as a download |

Examples

const express = require('express');
const { handlers } = require('@zentechdev/emuser');
const requireAuth = require('../middleware/auth');

const router = express.Router();

// GET /api/v1/users
// Returns details about current user
// Requires none
router.get('/', requireAuth, handlers.getCurrentUserInfo);

// GET /api/v1/users/auth
// Checks if a user is logged in
// Requires none
router.get('/auth', requireAuth, handlers.getUserAuth);

// GET /api/v1/users/:username
// Returns details about given user
// Requires none
router.get('/:username', requireAuth, handlers.getUserInfo);

// POST /api/v1/users
// Registers a new account
// Requires username, firstName, lastName, email, userType, password
router.post('/', handlers.postUser);

// POST /api/v1/users/login
// Logs a user in
// Requires username, password
router.post('/login', handlers.postLogin);

// POST /api/v1/users/picture
// Updates a user's profile picture
// Requires image file
router.post('/picture', requireAuth, handlers.postProfilePicture);

// GET /api/v1/users/picture/:fileName
// Returns a user's profile picture
// Requires fileName param
router.get('/picture/:fileName', handlers.getProfilePicture);

module.exports = router;

Author

👤 Owen Alexander

  • Website: www.linkedin.com/in/owen-alexander-8620981b3
  • Github: @oalexander-dev

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2022 Owen Alexander. This project is MIT licensed.


This README was generated with ❤️ by readme-md-generator