Esimerkki sisältää databaseApiExample sovelluksen koodin. Tässä esimerkissä luodaan tietokantaan
user-taulu ja sille CRUD-operaatiot. Tauluun lisättävä salasana kryptataan käyttäen
bcrypt algoritmia.
user-taulun rakenne
Laitetaan user-tauluun kentät
- username varchar(20) primary key
- fname varchar(20)
- password varchar(255)
Taulu voidaan luoda koodilla
CREATE TABLE user(
username varchar(20) primary key,
fname varchar(20),
password varchar(255)
) Engine=InnoDB;
Ja lisätään kokeeksi yksi käyttäjä koodilla
INSERT INTO user VALUES('user01','Teuvo','pass01');
Jatkossa käyttäjät luodaan ainoastaan REST APIn kautta, jotta salasana saadaan kryptattua.
bcryptjs
Asenna sovellukseen bcryptjs komennolla
npm install bcryptjs.
Sen kuvaus ja käyttöohje löytyy sivulta https://www.npmjs.com/package/bcryptjs
Tässä esimerkissä käytetään
asynkroonista hash metodia.
Sovelluksen muokkaus
- Kloonaa tämä repo ja asenna komennolla npm install
- Luo tiedostot routes/user.js ja models/user_model.js
- Kirjoita em. tiedostoihin tarvittavat koodit
- Kryptaa modelissa salasana ennen sen viemistä tietokantaan
- Kirjoita login-controlleri, joka lähettää responsena true tai false
- Muokkaa login-controlleria, niin että onnistuneen kirjautumisen seurauksena se palauttaakin webtokenin.
Webtokenin generoinnista löytyy ohjeet sivulta: https://peatutor.com/express/Examples/webtoken.php
- Suojaa reitit book ja user webtoken autentikoinnilla
Huomioita sovelluksesta
Tiedostossa app.js lause
const app=express(); luo Express-sovelluksen instanssin, joka edustaa koko sovellusta.
Kaikissa kontrollereissa eli routes kansion tiedostoissa lause
const router=express.Router(); luo reititin-olion, joka edustaa osaa sovelluksen reiteistä ja middlewareista, mikä on hyödyllistä koodin modulaarisuuden kannalta.
Secret key
Tokenin generoinnissa tarvitaan Secret Key. Jotta sovellus toimii generoi se ajamalla komento node generate_token ja kopio saatu string .env tiedostoon jonka malli on tiedostossa .env_example.
The example includes the code for the databaseApiExample application. In this example, a
user table is created in the database along with CRUD operations. The password added to the table is encrypted using the
bcrypt algorithm.
Structure of the user table
The user table will have the following fields:
- username varchar(20) primary key
- fname varchar(20)
- password varchar(255)
The table can be created with the following code:
CREATE TABLE user(
username varchar(20) primary key,
fname varchar(20),
password varchar(255)
) Engine=InnoDB;
And add a test user with the following code:
INSERT INTO user VALUES('user01','Teuvo','pass01');
In the future, users will only be created through the REST API to ensure the password is encrypted.
bcryptjs
Install bcryptjs in the application with the command
npm install bcryptjs.
Its description and usage instructions can be found at https://www.npmjs.com/package/bcryptjs
In this example, the
asynchronous hash method is used.
Modifying the application
- Clone this repo and install with the command npm install
- Create the files routes/user.js and models/user_model.js
- Write the necessary code in the aforementioned files
- Encrypt the password in the model before saving it to the database
- Write a login controller that responds with true or false
- Modify the login controller so that upon successful login, it returns a web token.
Instructions for generating the web token can be found at: https://peatutor.com/express/Examples/webtoken.php
- Protect the book and user routes with web token authentication
Notes about the application
In the app.js file, the statement
const app=express(); creates an instance of the Express application, which represents the entire application.
In all controllers, i.e., files in the routes folder, the statement
const router=express.Router(); creates a router object that represents a part of the application's routes and middleware, which is useful for code modularity.
Secret key
A Secret Key is required for generating the token. To make the application work, generate it by running the command node generate_token and copy the obtained string into the .env file, which is modeled in the .env_example file.