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

localtoken

v0.1.2

Published

A new way to authenticate.

Downloads

23

Readme

##: inbox_tray: localtoken

A New Way to Authenticate

A module that aims to simulate the local storage of browsers.

Follow the project on github and leave a little star. ** https: //github.com/deeborges/localtoken**

Installation

npm install --save localtoken

Middleware Structure

The first thing to do is create a directory called middleware and inside it create a file called auth.js.

Auth.js should contain the code below: alt text

  • Easy, let's understand how everything works ... *

In this file we have the token creation with the jsonwebtoken module. He will be responsible for * generating * and * decoding * the token.

The next step is to define a folder called controller. Inside it we will create a file called user.js. When you do ** GET ** you should render the login view of your application. The code will look something like this:

alt text

Continuing in user.js we will add a new code snippet, which should look like this:

alt text

When the user submits the information, our code will verify that the information matches what is in our repository (it is worth mentioning that you must already have your repository with the database information). To check if the hashed password in the bank matches what the user entered, I use the bcrypt module.

If the information is correct you will launch the user into * storage * (storage is our localtoken module). That simple.

A key named * login * will be released and the value of that key will be * user information *.

I point out that the key name may be of your choice

  • So far all beauty!*

Now we will deal with the routes

Create a folder named routes and inside it create a file named user_routes.js. Your code should look like this:

alt text

What happens here in very simple. You can call auth middleware for the routes you want to protect. We are simply telling our middleware which routes it will work on.

the route that gets the login GET cannot have middleware because the route will be protected and you will not be able to do anything with it.

And how will it work, I still don't understand ???

Then the middleware will take action on the specified routes, in our specific case the * login route *. When the route is requested, in the case * the POST route *, it will enter our controller, which in turn will do the whole process of validating the information sent with the information we already have. The controller, if validated successfully, will send the information to localtoken. Let's get back to our middleware. alt text

If we have information in our storage, we simply send * next () * and our application goes to the next step, the next step, which was defined there in the controller.

If nothing is found, we render an error or a cute message.

Now, every time we request other routes * such as editing, for example, * middleware will already have user information logged into storage and will allow you to access that route.

  • Simple, no!? *

Note: I recommend working more efficiently on validations. You can even create a dynamic to render the view, believe me, I've tested it, it works.

I hope you enjoyed!