0.1.2 • Published 5 years ago

localtoken v0.1.2

Weekly downloads
4
License
ISC
Repository
-
Last release
5 years ago

##: 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 calledauth.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 theuser 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 nameduser_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!

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.29

5 years ago

0.0.28

5 years ago

0.0.27

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

1.0.0

5 years ago