
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
localtoken
Advanced tools
##: inbox_tray: localtoken
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**
npm install --save localtoken
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:
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:

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

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
Create a folder named routes and inside it create a file named user_routes.js.
Your code should look like this:

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.
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.

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.
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!
FAQs
A new way to authenticate.
We found that localtoken demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.