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

@aytacworld/express-module-login

v2.0.0

Published

Adding login feature to express server

Downloads

5

Readme

@aytacworld/express-module-login

This module is ment to be used with @aytacworld/express.

It will add login routes and login services to the application.

Needed modules

For this module you need authDb module to be loaded before loading this module.

The needed modules needs to be loaded before loading login module.

There are already 2 modules for that purpose.

Installation

using npm

npm i @aytacworld/express-module-login

using yarn

yarn add @aytacworld/express-module-login

Usage

app.js

const Express = require('@aytacworld/express');
const ExpressMongo = require('@aytacworld/express-module-mongo');
const ExpressLogin = require('@aytacworld/express-module-login');
const myOtherRoutes = require('./routes');

const app = new Express({});

app.module(ExpressMongo, ExpressLogin);
app.route('/', myOtherRoutes);

app.listen();

The following routes will be added.

  1. GET /login
  2. POST /login
  3. GET /login/logout

If you need to check if the user is logged in, you can use the helper module(which is built-in in @aytacworld/express).

routes.js

const path = require('path');
const router = require('express').Router();

// login module is added.
const Router = (config, { helper, db }) => {
  helper.addPathToViewEngine(path.resolve(__dirname, 'views'));

  router.get('/', async (req, res) => {
    res.render('homepage', { user: req.user }); // req.user can be undefined
  });

  router.get('/profile',
    helper.isAuthenticated(), // checks whether the user is filled in or not
    async (req, res) => {
      res.render('profile', { user: req.user }); // req.user is always filled in
    });

  return router;
};

module.exports = Router;

Config

{
  "EXPRESS_MODULE_LOGIN_DISABLE_VIEW": true, // Boolean, default: undefined, this will don't add default pug files in your project, so you need to create your own login page
  "EXPRESS_MODULE_LOGIN_VIEW_NAME": "my-custom-login-view", // String, default: 'aytacworld-express-module-login'
  "EXPRESS_MODULE_LOGIN_CSS": "/public/login.css", // String, default: undefined
}

Updating login page

There are 3 ways of updating the login view-page:

  1. set EXPRESS_MODULE_LOGIN_DISABLE_VIEW to true in your config file and create a aytacworld-express-module-login.pug in your views folder.

  2. set EXPRESS_MODULE_LOGIN_VIEW_NAME to another view name and create a view with the name you provide.

  3. set EXPRESS_MODULE_LOGIN_CSS to point to your css file, then you can use classes.

This is the login.pug template, so you can find the class you need.

login.pug

h1.module-login-h1 Please sign in
form(method='POST' action='/login').module-login-form
  input(type='hidden' name="redirectUrl" value=`${redirectUrl || ''}`)
  .module-login-form-control
    label.module-login-label Username
      input(type='text' placeholder='Username' name='username' required).module-login-input.module-login-input-username
  .module-login-form-control
    label.module-login-label Password
      input(type='password' placeholder='Password' name='password' required).module-login-input.module-login-input-password
  .module-login-form-control
    button(type='submit').module-login-button Login

Database structure

{
  users: {
    static findById(id): Promise<user>;
    static comparePassword(username, password): Promise<user|false>;
  },
}

User model

class User {
  id: any;
  username: string;
  password: string;
}

INFO: don't forget to hash the password before saving in database!

MIT License

Copyright (c) 2019 Adem Aytaç

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.