{"id":338283,"date":"2022-09-13T09:00:09","date_gmt":"2022-09-13T09:00:09","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=338283"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=338283","title":{"rendered":"<span>\u041f\u0438\u0448\u0435\u043c \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u0439 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c React, Nest \u0438 NX. \u0427\u0430\u0441\u0442\u044c 2: \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/c8b\/638\/d3c\/c8b638d3c875c6d721da44e73b636a32.jpeg\" width=\"1560\" height=\"1039\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c8b\/638\/d3c\/c8b638d3c875c6d721da44e73b636a32.jpeg\" data-blurred=\"true\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0414\u0440\u0443\u0437\u044c\u044f, \u0432\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u0418\u0433\u043e\u0440\u044c \u041a\u0430\u0440\u0435\u043b\u0438\u043d, \u044f frontend-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0432 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 \u0414\u043e\u043c\u043a\u043b\u0438\u043a. \u0412 <a href=\"https:\/\/habr.com\/ru\/company\/domclick\/blog\/672546\/\">\u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0447\u0430\u0441\u0442\u0438<\/a> \u043c\u044b \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438\u0441\u044c, \u043a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442, \u0430 \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u0430\u0448 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0438 \u043f\u043e\u044d\u0442\u0430\u043f\u043d\u043e \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <a href=\"https:\/\/github.com\/jaredhanson\/passport\">Passport<\/a>.<\/p>\n<p><strong>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/strong>\u00a0\u2014 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u043e\u0434\u043b\u0438\u043d\u043d\u043e\u0441\u0442\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<ul>\n<li>\n<p>\u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043f\u043e\u0434\u043b\u0438\u043d\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u0443\u0442\u0451\u043c \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0432\u0432\u0435\u0434\u0451\u043d\u043d\u043e\u0433\u043e \u0438\u043c \u043f\u0430\u0440\u043e\u043b\u044f (\u0434\u043b\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e\u00a0\u043b\u043e\u0433\u0438\u043d\u0430) \u0441 \u043f\u0430\u0440\u043e\u043b\u0435\u043c, \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d\u043d\u044b\u043c \u0432\u00a0\u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445\u00a0\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0445 \u043b\u043e\u0433\u0438\u043d\u043e\u0432;<\/p>\n<\/li>\n<li>\n<p>\u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u043b\u0438\u043d\u043d\u043e\u0441\u0442\u0438 \u044d\u043b\u0435\u043a\u0442\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043f\u0438\u0441\u044c\u043c\u0430 \u043f\u0443\u0442\u0451\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438\u00a0\u0446\u0438\u0444\u0440\u043e\u0432\u043e\u0439 \u043f\u043e\u0434\u043f\u0438\u0441\u0438\u00a0\u043f\u0438\u0441\u044c\u043c\u0430 \u043f\u043e\u00a0\u043e\u0442\u043a\u0440\u044b\u0442\u043e\u043c\u0443 \u043a\u043b\u044e\u0447\u0443 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u0435\u043b\u044f;<\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430\u00a0\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c\u043d\u043e\u0439 \u0441\u0443\u043c\u043c\u044b \u0444\u0430\u0439\u043b\u0430 \u043d\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u0441\u0443\u043c\u043c\u0435, \u0437\u0430\u044f\u0432\u043b\u0435\u043d\u043d\u043e\u0439 \u0430\u0432\u0442\u043e\u0440\u043e\u043c \u044d\u0442\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430.<\/p>\n<\/li>\n<\/ul>\n<p><a href=\"https:\/\/github.com\/jaredhanson\/passport\">Passport<\/a> \u2014 \u0441\u0430\u043c\u0430\u044f \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0432 node.js \u0441 \u0431\u043e\u0433\u0430\u0442\u044b\u043c \u043d\u0430\u0431\u043e\u0440\u043e\u043c <a href=\"http:\/\/www.passportjs.org\/\">\u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0439<\/a>, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043b\u0435\u0433\u043a\u043e \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 <a href=\"https:\/\/nestjs.com\/\">Nest<\/a>. \u041c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0441\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441\u043a\u0432\u043e\u0437\u043d\u043e\u0439 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430 RESTful API. \u0415\u0451 \u044d\u0442\u0430\u043f\u044b: <\/p>\n<ul>\n<li>\n<p>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u0440\u043e\u043b\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <a href=\"https:\/\/github.com\/kelektiv\/node.bcrypt.js\">bcrypt<\/a> \u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 <a href=\"https:\/\/jwt.io\/\">JWT<\/a>.<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0437\u0430\u0449\u0438\u0449\u0451\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0430 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 <a href=\"https:\/\/jwt.io\/\">JWT<\/a> \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435.<\/p>\n<\/li>\n<\/ul>\n<h3>\u041a\u043e\u0440\u043e\u0442\u043a\u043e \u043e \u043c\u043e\u0434\u0443\u043b\u044f\u0445, \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430\u0445 \u0438 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430\u0445 \u0432 Nest<\/h3>\n<p><strong>\u041c\u043e\u0434\u0443\u043b\u0438<\/strong> \u2014 \u044d\u0442\u043e \u043a\u043b\u0430\u0441\u0441\u044b, \u0430\u043d\u043d\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435\u00a0\u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u043e\u043c <code>@Module()<\/code>.\u00a0\u041e\u043d \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435\u00a0<a href=\"https:\/\/nestjs.com\/\">Nest<\/a>\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u043b\u044f \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p><strong>\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b<\/strong> \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0442 \u0437\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0442\u0432\u0435\u0442\u043e\u0432.<\/p>\n<p><strong>\u041f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u044b<\/strong> \u2014 \u044d\u0442\u043e \u0444\u0443\u043d\u0434\u0430\u043c\u0435\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f Nest.\u00a0\u041c\u043d\u043e\u0433\u0438\u0435 \u0438\u0437 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 Nest \u043c\u043e\u0433\u0443\u0442 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u0430\u043a \u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a\u0438: \u0441\u0435\u0440\u0432\u0438\u0441\u044b, \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438, \u0444\u0430\u0431\u0440\u0438\u043a\u0438, \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a\u0438 \u0438 \u0442. \u0434.<\/p>\n<p>\u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u0439\u0442\u0438 \u043d\u0430 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u0430\u0439\u0442\u0435 <a href=\"https:\/\/nestjs.com\/\">Nest<\/a>.<\/p>\n<h3>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0445 \u043f\u0430\u043a\u0435\u0442\u043e\u0432<\/h3>\n<p>\u041d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f:<\/p>\n<ul>\n<li>\n<p>\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 <code>passport<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u043c\u043e\u0434\u0443\u043b\u044c \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 <a href=\"https:\/\/nestjs.com\/\">Nest<\/a> <code>@nestjs\/passport<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u043f\u0430\u043a\u0435\u0442\u00a0<code>passport-jwt<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e JWT;<\/p>\n<\/li>\n<li>\n<p>\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 <code>bcrypt<\/code>, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u0440\u043e\u043b\u0438 \u043f\u0435\u0440\u0435\u0434 \u0438\u0445 \u0437\u0430\u043f\u0438\u0441\u044c\u044e \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445;<\/p>\n<\/li>\n<li>\n<p>\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f TypeScript-\u0442\u0438\u043f\u043e\u0432\u00a0<code>@types\/passport-jwt @types\/bcrypt<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u043f\u0430\u043f\u043a\u0443 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 <code>part-1<\/code>  \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u044b:<\/p>\n<pre><code class=\"bash\">$ npm install --save passport @nestjs\/passport $ npm install --save @nestjs\/jwt passport-jwt $ npm install --save bcrypt $ npm install --save-dev @types\/passport-jwt @types\/bcrypt<\/code><\/pre>\n<h3>\u0417\u0430\u043f\u0443\u0441\u043a \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432<\/h3>\n<p>\u0412 <a href=\"https:\/\/habr.com\/ru\/company\/domclick\/blog\/672546\/\">\u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 <\/a>\u043c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 Docker-\u043e\u0431\u0440\u0430\u0437 \u0441 MongoDB \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043b\u0438 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b. \u041f\u0435\u0440\u0435\u0434 \u043d\u0430\u0447\u0430\u043b\u043e\u043c \u0440\u0430\u0431\u043e\u0442\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043f\u0440\u043e\u0435\u043a\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u0430\u043c\u0438:<\/p>\n<pre><code>docker-compose up -d \/\/ \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c Docker nx run-many --parallel --target=serve --projects=backend,frontend \/\/ \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043c\u043e\u043d\u043e\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0441 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c\u0438<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u043f\u0430\u043f\u043a\u0443 src \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 backend \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/docs.nestjs.com\/cli\/overview\">Nest CLI<\/a> \u0441\u043e\u0437\u0434\u0430\u0451\u043c, \u043c\u043e\u0434\u0443\u043b\u044c, \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0438 \u0441\u0435\u0440\u0432\u0438\u0441. <a href=\"https:\/\/docs.nestjs.com\/cli\/overview\">Nest CLI<\/a> \u2014\u00a0\u044d\u0442\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0432\u0430\u0448\u0438 Nest-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"bash\">nest g module auth \/\/ \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043c\u043e\u0434\u0443\u043b\u044f nest g service auth --no-spec \/\/ \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 nest g controller auth --no-spec \/\/ \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430<\/code><\/pre>\n<p>\u041e\u043f\u0446\u0438\u044f <code>--no-spec<\/code> \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432 \u0441 \u0442\u0435\u0441\u0442\u0430\u043c\u0438 (\u0438\u0445 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0440\u0430\u043c\u043a\u0438 \u0441\u0442\u0430\u0442\u044c\u0438).<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u0444\u0430\u0439\u043b\u044b \u0438 \u0432 <code>AuthModule<\/code> \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b <code>AuthService<\/code> \u0438 <code>AuthController<\/code>, \u0430 <code>AuthModule<\/code> \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d \u0432 <code>AppModule<\/code>. \u042d\u0442\u043e \u0432\u0441\u0451 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0440\u0430\u0431\u043e\u0442\u0435 <a href=\"https:\/\/docs.nestjs.com\/cli\/overview\">Nest CLI<\/a>. \u0415\u0449\u0451 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u044c \u0438 \u0441\u0435\u0440\u0432\u0438\u0441 \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438:<\/p>\n<pre><code>nest g module users \/\/ \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043c\u043e\u0434\u0443\u043b\u044f nest g service users --no-spec \/\/ \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430<\/code><\/pre>\n<p>\u042f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u043e\u0442\u0440\u0430\u0437\u0438\u0442\u044c \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u0446\u0435\u043b\u0438 \u0438 \u0448\u0430\u0433\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430.<\/p>\n<h3>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438<\/h3>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u0435\u0442\u043e\u0434\u044b \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f, \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438, \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u0440\u043e\u043b\u0435\u0439. \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u043e\u0432 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u0432 \u043f\u0430\u043f\u043a\u0443 users, \u0441\u043e\u0437\u0434\u0430\u0442\u044c <a href=\"https:\/\/ru.wikipedia.org\/wiki\/DTO\">DTO<\/a> \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0438 \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f MongoDB.<\/p>\n<p><strong>models\/user.model.ts<\/strong><\/p>\n<pre><code class=\"typescript\">import { Prop, Schema, SchemaFactory } from '@nestjs\/mongoose'; import { Document } from 'mongoose'; import { IUser } from '..\/interfaces\/user.interface';  @Schema({ collection: 'users', timestamps: true }) \/\/ \u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0438\u043c\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432 \u0431\u0430\u0437\u0443 export class UserModel extends Document implements IUser {   @Prop({ required: true }) \/\/ \u0413\u043e\u0432\u043e\u0440\u0438\u0442, \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u044d\u0442\u043e \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435   username: string;        @Prop({ required: true })   password: string;        @Prop({ required: true })   email: string; } export const UserSchema = SchemaFactory.createForClass(UserModel); \/\/ \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u0441\u0445\u0435\u043c\u0443.<\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u0434 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c DTO \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c <code>class-validator class-transformer<\/code>:<\/p>\n<pre><code class=\"bash\">npm i --save class-validator class-transformer<\/code><\/pre>\n<p>\u041e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441 <code>Dto<\/code>, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c\u0441\u044f \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b.<\/p>\n<p><strong>dto\/user.dto.ts<\/strong><\/p>\n<pre><code class=\"typescript\">import { IsNotEmpty, IsEmail } from 'class-validator';  export class UserDto {   @IsNotEmpty() \/\/ \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0443\u0441\u0442\u044b\u043c   id: string;        @IsNotEmpty()   username: string;      @IsEmail() \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 email   email: string; }<\/code><\/pre>\n<p>\u041c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c <code>UserModule<\/code> \u0438 <code>UserService<\/code>. \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c <code>MongooseModule<\/code> \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414 \u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f, \u043f\u043e\u0438\u0441\u043a\u0430 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<p><strong>users.module.ts<\/strong><\/p>\n<pre><code class=\"typescript\">import { Module } from '@nestjs\/common'; import { MongooseModule } from '@nestjs\/mongoose'; import { UserModel, UserSchema } from '.\/models\/user.model'; import { UsersService } from '.\/users.service';  @Module({   imports: [     MongooseModule.forFeature([ \/\/ \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c MongooseModule       {         name: UserModel.name, \/\/ \u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0438\u043c\u044f \u043c\u043e\u0434\u0435\u043b\u0438, \u043e\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c\u0441\u044f \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445         schema: UserSchema, \/\/ \u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0441\u0445\u0435\u043c\u0443 \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445       },     ]),   ],   providers: [UsersService], \/\/ \u041f\u043e\u0441\u043b\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 Nest CLI \u0432 providers \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d UsersService   exports: [UsersService], \/\/ \u042d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c UsersService, \u044d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u043c\u0438 \u043c\u043e\u0434\u0443\u043b\u044f. })<\/code><\/pre>\n<p>\u0412 \u0441\u0435\u0440\u0432\u0438\u0441\u0435 \u0437\u0430\u043b\u043e\u0436\u0435\u043d\u0430 \u0432\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414 \u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438.<\/p>\n<details class=\"spoiler\">\n<summary>users.service.ts<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"typescript\">import { Injectable, HttpException, HttpStatus } from '@nestjs\/common'; import { UserDto } from '.\/dto\/user.dto'; import { CreateUserDto } from '.\/dto\/user.create.dto'; import { LoginUserDto } from '.\/dto\/user-login.dto'; import { toUserDto } from '..\/shared\/mapper'; import { InjectModel } from '@nestjs\/mongoose'; import { UserModel } from '.\/models\/user.model'; import { Model } from 'mongoose'; import { genSalt, hash, compare } from 'bcrypt';  @Injectable() export class UsersService {   constructor(     @InjectModel(UserModel.name)     private readonly userModel: Model&lt;UserModel>\/\/ \u0412\u043d\u0435\u0434\u0440\u044f\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0411\u0414 \u0432 \u0441\u0435\u0440\u0432\u0438\u0441 \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f   ) {}    async findOne(options?: object): Promise&lt;UserDto> {\/\/ \u041c\u0435\u0442\u043e\u0434 \u043f\u043e\u0438\u0441\u043a\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f     const user = await this.userModel.findOne(options).exec();\/\/ \u041c\u043e\u0434\u0435\u043b\u044c \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414     return toUserDto(user); \/\/ \u0413\u043e\u0442\u043e\u0432\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043a \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e   }    async findByLogin({ username, password }: LoginUserDto): Promise&lt;UserDto> {\/\/ \u041c\u0435\u0442\u043e\u0434 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u0438 \u043f\u0430\u0440\u043e\u043b\u044e     const user = await this.userModel.findOne({ username }).exec();      if (!user) { \/\/ \u0415\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0435\u0442, \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443 'User not found'       throw new HttpException('User not found', HttpStatus.UNAUTHORIZED);     }      const areEqual = await compare(password, user.password); \/\/ \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 bcrypt \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0430\u0440\u043e\u043b\u044c \u0438 \u0445\u0435\u0448; \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0440\u0430\u0432\u043d\u044b, \u0442\u043e \u0432\u0435\u0440\u043d\u0451\u0442\u0441\u044f true      if (!areEqual) {\/\/ \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u043e\u043b\u0438 \u043d\u0435 \u0440\u0430\u0432\u043d\u044b, \u0442\u043e \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443 'Invalid credentials'       throw new HttpException('Invalid credentials', HttpStatus.UNAUTHORIZED);     }      return toUserDto(user);    }    async findByPayload({ username }: any): Promise&lt;UserDto> {\/\/ \u041f\u043e\u0438\u0441\u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u0438\u043c\u0435\u043d\u0438     return await this.findOne({ username });   }    async create(userDto: CreateUserDto): Promise&lt;UserDto> {\/\/ \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f     const { username, password, email } = userDto;      const userInDb = await this.userModel.findOne({ username }).exec();     if (userInDb) {\/\/ \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0435\u0441\u0442\u044c \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443       throw new HttpException('User already exists', HttpStatus.BAD_REQUEST);     }      const salt = await genSalt(10); \/\/ \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 bycrypt \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0441\u043e\u043b\u044c     const hashPassword = await hash(password, salt); \/\/ bycrypt \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0445\u0435\u0448 \u043f\u0430\u0440\u043e\u043b\u044f      const user: UserModel = await new this.userModel({ \/\/ \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f       username,       password: hashPassword,       email,     });          await user.save(); \/\/ \u0421\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0432 \u0411\u0414      return toUserDto(user);   }    private _sanitizeUser(user: UserModel) {     delete user.password;     return user;   } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<h3>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/h3>\n<p>\u0420\u0430\u043d\u0435\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/docs.nestjs.com\/cli\/overview\">Nest CLI<\/a> \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0435\u0439, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0435\u0449\u0451 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 <a href=\"https:\/\/github.com\/jaredhanson\/passport\">Passport<\/a> \u0438 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e. \u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u043f\u0430\u043f\u043a\u0443 auth \u0438 \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0444\u0430\u0439\u043b jwt.strategy.ts \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439:<\/p>\n<details class=\"spoiler\">\n<summary>jwt.strategy.ts<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"typescript\">import { PassportStrategy } from '@nestjs\/passport'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { AuthService } from '.\/auth.service'; import { Injectable, HttpException, HttpStatus } from '@nestjs\/common'; import { JwtPayload } from '.\/interfaces\/payload.interface'; import { UserDto } from '..\/users\/dto\/user.dto';  @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) {\/\/ \u041d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u0441\u044f \u043e\u0442 PassportStrategy, \u0434\u043e\u0431\u0430\u043b\u044f\u0435\u043c \u043c\u0435\u0442\u043e\u0434\u044b \u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438   constructor(private readonly authService: AuthService) {     super({       jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),       secretOrKey: process.env.JWT_KEY + '',     });   }    async validate(payload: JwtPayload): Promise&lt;UserDto> { \/\/ \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u043c\u0435\u0442\u043e\u0434 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438     const user = await this.authService.validateUser(payload);     if (!user) {       throw new HttpException('Invalid token', HttpStatus.UNAUTHORIZED);     }     return user;   } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0432 \u043c\u043e\u0434\u0443\u043b\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<details class=\"spoiler\">\n<summary>auth.module.ts<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"typescript\">import { Module } from '@nestjs\/common'; import { AuthController } from '.\/auth.controller'; import { AuthService } from '.\/auth.service'; import { JwtModule } from '@nestjs\/jwt'; import { PassportModule } from '@nestjs\/passport'; import { UsersModule } from '..\/users\/users.module'; import { JwtStrategy } from '.\/jwt.strategy'; import { ConfigService } from '@nestjs\/config';  @Module({   imports: [     UsersModule,     PassportModule.register({ \/\/ \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c PassportModule \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0435\u0439       defaultStrategy: 'jwt',       property: 'user',       session: false,     }),     JwtModule.registerAsync({ \/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c JWT-\u043c\u043e\u0434\u0443\u043b\u044c \u0438 \u043f\u0440\u0435\u0434\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 .env       useFactory: (config: ConfigService) => {         return {           secret: config.get&lt;string>('JWT_KEY'),           signOptions: {             expiresIn: config.get&lt;string>('EXPIRESIN'),           },         };       },       inject: [ConfigService],     }),   ],   controllers: [AuthController],   providers: [AuthService, JwtStrategy],   exports: [PassportModule, JwtModule], }) export class AuthModule {}<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0412\u0430\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c <code>UsersModule<\/code>, \u0438\u043d\u0430\u0447\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0432 <code>auth<\/code>.<\/p>\n<details class=\"spoiler\">\n<summary>auth.service.ts<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"typescript\">import { Injectable, HttpException, HttpStatus } from '@nestjs\/common'; import { RegistrationStatus } from '.\/interfaces\/regisration-status.interface'; import { LoginStatus } from '.\/interfaces\/login-status.interface'; import { LoginUserDto } from '..\/users\/dto\/user-login.dto'; import { JwtPayload } from '.\/interfaces\/payload.interface'; import { JwtService } from '@nestjs\/jwt'; import { UsersService } from '..\/users\/users.service'; import { CreateUserDto } from '..\/users\/dto\/user.create.dto'; import { UserDto } from '..\/users\/dto\/user.dto';  @Injectable() export class AuthService {   constructor(     private readonly usersService: UsersService,     private readonly jwtService: JwtService   ) {}    async register(userDto: CreateUserDto): Promise&lt;RegistrationStatus> { \/\/ \u041c\u0435\u0442\u043e\u0434 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f     let status: RegistrationStatus = {       success: true,       message: 'user registered',     };      try {       await this.usersService.create(userDto); \/\/ \u041f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 usersService.create, \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0435\u0442, \u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0441\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439, \u0438\u043d\u0430\u0447\u0435 \u043e\u0448\u0438\u0431\u043a\u0430      } catch (err) {       status = {         success: false,         message: err,       };     }      return status;   }    async login(loginUserDto: LoginUserDto): Promise&lt;LoginStatus> { \/\/ \u041c\u0435\u0442\u043e\u0434 \u043b\u043e\u0433\u0438\u043d\u0430     const user = await this.usersService.findByLogin(loginUserDto); \/\/ \u0418\u0449\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044e     const token = this._createToken(user); \/\/ \u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0442\u043e\u043a\u0435\u043d     return { \/\/ \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435       username: user.username,       ...token,     };   }    async validateUser(payload: JwtPayload): Promise&lt;UserDto> { \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430\u043b\u0438\u0447\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f     const user = await this.usersService.findByPayload(payload);     if (!user) {       throw new HttpException('Invalid token', HttpStatus.UNAUTHORIZED);     }     return user;   }    private _createToken({ username }: UserDto): any { \/\/ \u041c\u0435\u0442\u043e\u0434 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0430     const expiresIn = process.env.EXPIRESIN + '';     const user: JwtPayload = { username };     const accessToken = this.jwtService.sign(user);       return {       expiresIn,       accessToken,     };   } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c\u0441\u044f \u043d\u0430 \u0444\u0430\u0439\u043b\u0435 auth.controller.ts \u0438 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u00a0<code>@Controller('auth')<\/code>,\u00a0\u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439\u00a0\u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430.\u00a0\u0423\u043a\u0430\u0436\u0435\u043c \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u0435\u0444\u0438\u043a\u0441 <code>'auth'<\/code>;\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u0430 \u043f\u0443\u0442\u0438 \u0432 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0431\u043e\u0440 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u043e\u0432. \u041c\u044b \u043e\u0431\u0441\u0443\u0436\u0434\u0430\u043b\u0438 \u0440\u0430\u043d\u0435\u0435, \u0447\u0442\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0442\u0432\u0435\u0442\u043e\u0432. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: <code>GET<\/code>, <code>POST<\/code>, <code>PUT<\/code>, <code>DELETE<\/code>. <\/p>\n<p>\u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 HTTP-\u043c\u0435\u0442\u043e\u0434\u0430 <code>@Post('register')<\/code>\u00a0 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u043c\u0435\u0442\u043e\u0434\u0443 <code>register()<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043a\u043e\u043d\u0435\u0447\u043d\u0443\u044e \u0442\u043e\u0447\u043a\u0443 \u0434\u043b\u044f HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.\u00a0\u041a\u043e\u043d\u0435\u0447\u043d\u0430\u044f \u0442\u043e\u0447\u043a\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u0435\u0442\u043e\u0434\u0443 HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 (\u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 POST) \u0438 \u043f\u0443\u0442\u0438 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0430.\u00a0\u041f\u0443\u0442\u044c \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0430 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435\u043c (\u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e) \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u0430, \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0434\u043b\u044f \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430, \u0438 \u043b\u044e\u0431\u043e\u0433\u043e \u043f\u0443\u0442\u0438, \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u0432 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0435 \u043c\u0435\u0442\u043e\u0434\u0430.\u00a0\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u044b \u043e\u0431\u044a\u044f\u0432\u0438\u043b\u0438 \u043f\u0440\u0435\u0444\u0438\u043a\u0441 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0430 <code>@Controller('auth')<\/code> \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 <code>@Post('register')<\/code>, Nest \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0441 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u043e\u043c <code>\/auth\/register<\/code>.<\/p>\n<p>\u0412 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435, \u043a\u043e\u0433\u0434\u0430 POST-\u0437\u0430\u043f\u0440\u043e\u0441 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u043a\u043e\u043d\u0435\u0447\u043d\u0443\u044e \u0442\u043e\u0447\u043a\u0443, Nest \u0448\u043b\u0451\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c\u0443\u00a0\u043c\u0435\u0442\u043e\u0434\u0443 <code>register()<\/code>.\u00a0\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0438\u043c\u044f \u043c\u0435\u0442\u043e\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0437\u0434\u0435\u0441\u044c, \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0435.\u00a0\u041c\u044b, \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0434\u043e\u043b\u0436\u043d\u044b \u043e\u0431\u044a\u044f\u0432\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u043f\u0440\u0438\u0432\u044f\u0437\u043a\u0438 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0430, \u043d\u043e Nest \u043d\u0435 \u043f\u0440\u0438\u0434\u0430\u0451\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u043c\u0443 \u0438\u043c\u0435\u043d\u0438. \u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0432\u0435\u0440\u043d\u0451\u0442 \u043a\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f 201 \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043e\u0442\u0432\u0435\u0442.<\/p>\n<p>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e <code>Pipes<\/code>. \u042d\u0442\u043e \u043a\u043b\u0430\u0441\u0441, \u0430\u043d\u043d\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u043e\u043c\u00a0<code>@Injectable()<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u00a0<code>PipeTransform<\/code>. \u0421 \u0435\u0433\u043e \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u0434\u043e\u0431\u043d\u043e \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435. \u041c\u044b \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 <code>@UsePipes(new ValidationPipe())<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u0441\u0445\u0435\u043c\u0435, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0439 \u043d\u0430\u043c\u0438 \u0432 <code>CreateUserDto<\/code>. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043c\u044b \u043d\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u043f\u0440\u0438 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 email, \u043d\u0430\u043c \u0432\u0435\u0440\u043d\u0451\u0442\u0441\u044f \u0442\u0430\u043a\u043e\u0439 \u043e\u0442\u0432\u0435\u0442:<\/p>\n<pre><code class=\"json\">{     \"statusCode\": 400,     \"message\": [         \"email must be an email\",         \"email should not be empty\"     ],     \"error\": \"Bad Request\" }<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442, \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u043e\u0434 \u0441\u0432\u043e\u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u043f\u043e\u043a\u0430 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c\u0441\u044f. \u041c\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438, \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u0440\u043e\u043b\u0435\u0439 \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043b\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 Passport. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0447\u0430\u0441\u0442\u044f\u0445 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043b\u043e\u0433\u0438\u043a\u0443 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438 \u0438 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0440\u043e\u043d\u0442\u0435\u043d\u0434. \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435!<\/p>\n<p>\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043f\u043e\u00a0<a href=\"https:\/\/github.com\/Igoryas\/nx-react-nest\">\u0441\u0441\u044b\u043b\u043a\u0435<\/a>.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/domclick\/blog\/687106\/\"> https:\/\/habr.com\/ru\/company\/domclick\/blog\/687106\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u0440\u0443\u0437\u044c\u044f, \u0432\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u0418\u0433\u043e\u0440\u044c \u041a\u0430\u0440\u0435\u043b\u0438\u043d, \u044f frontend-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0432 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 \u0414\u043e\u043c\u043a\u043b\u0438\u043a. \u0412 <a href=\"https:\/\/habr.com\/ru\/company\/domclick\/blog\/672546\/\">\u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0447\u0430\u0441\u0442\u0438<\/a> \u043c\u044b \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438\u0441\u044c, \u043a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442, \u0430 \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u0430\u0448 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0438 \u043f\u043e\u044d\u0442\u0430\u043f\u043d\u043e \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <a href=\"https:\/\/github.com\/jaredhanson\/passport\">Passport<\/a>.<\/p>\n<p><strong>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/strong>\u00a0\u2014 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u043e\u0434\u043b\u0438\u043d\u043d\u043e\u0441\u0442\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<ul>\n<li>\n<p>\u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043f\u043e\u0434\u043b\u0438\u043d\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u0443\u0442\u0451\u043c \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0432\u0432\u0435\u0434\u0451\u043d\u043d\u043e\u0433\u043e \u0438\u043c \u043f\u0430\u0440\u043e\u043b\u044f (\u0434\u043b\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e\u00a0\u043b\u043e\u0433\u0438\u043d\u0430) \u0441 \u043f\u0430\u0440\u043e\u043b\u0435\u043c, \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d\u043d\u044b\u043c \u0432\u00a0\u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445\u00a0\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0445 \u043b\u043e\u0433\u0438\u043d\u043e\u0432;<\/p>\n<\/li>\n<li>\n<p>\u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u043b\u0438\u043d\u043d\u043e\u0441\u0442\u0438 \u044d\u043b\u0435\u043a\u0442\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043f\u0438\u0441\u044c\u043c\u0430 \u043f\u0443\u0442\u0451\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438\u00a0\u0446\u0438\u0444\u0440\u043e\u0432\u043e\u0439 \u043f\u043e\u0434\u043f\u0438\u0441\u0438\u00a0\u043f\u0438\u0441\u044c\u043c\u0430 \u043f\u043e\u00a0\u043e\u0442\u043a\u0440\u044b\u0442\u043e\u043c\u0443 \u043a\u043b\u044e\u0447\u0443 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u0435\u043b\u044f;<\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430\u00a0\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c\u043d\u043e\u0439 \u0441\u0443\u043c\u043c\u044b \u0444\u0430\u0439\u043b\u0430 \u043d\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u0441\u0443\u043c\u043c\u0435, \u0437\u0430\u044f\u0432\u043b\u0435\u043d\u043d\u043e\u0439 \u0430\u0432\u0442\u043e\u0440\u043e\u043c \u044d\u0442\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430.<\/p>\n<\/li>\n<\/ul>\n<p><a href=\"https:\/\/github.com\/jaredhanson\/passport\">Passport<\/a> \u2014 \u0441\u0430\u043c\u0430\u044f \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0432 node.js \u0441 \u0431\u043e\u0433\u0430\u0442\u044b\u043c \u043d\u0430\u0431\u043e\u0440\u043e\u043c <a href=\"http:\/\/www.passportjs.org\/\">\u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0439<\/a>, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043b\u0435\u0433\u043a\u043e \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 <a href=\"https:\/\/nestjs.com\/\">Nest<\/a>. \u041c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0441\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441\u043a\u0432\u043e\u0437\u043d\u043e\u0439 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430 RESTful API. \u0415\u0451 \u044d\u0442\u0430\u043f\u044b: <\/p>\n<ul>\n<li>\n<p>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u0440\u043e\u043b\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <a href=\"https:\/\/github.com\/kelektiv\/node.bcrypt.js\">bcrypt<\/a> \u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 <a href=\"https:\/\/jwt.io\/\">JWT<\/a>.<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0437\u0430\u0449\u0438\u0449\u0451\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0430 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 <a href=\"https:\/\/jwt.io\/\">JWT<\/a> \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435.<\/p>\n<\/li>\n<\/ul>\n<h3>\u041a\u043e\u0440\u043e\u0442\u043a\u043e \u043e \u043c\u043e\u0434\u0443\u043b\u044f\u0445, \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430\u0445 \u0438 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430\u0445 \u0432 Nest<\/h3>\n<p><strong>\u041c\u043e\u0434\u0443\u043b\u0438<\/strong> \u2014 \u044d\u0442\u043e \u043a\u043b\u0430\u0441\u0441\u044b, \u0430\u043d\u043d\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435\u00a0\u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u043e\u043c <code>@Module()<\/code>.\u00a0\u041e\u043d \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435\u00a0<a href=\"https:\/\/nestjs.com\/\">Nest<\/a>\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u043b\u044f \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p><strong>\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b<\/strong> \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0442 \u0437\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0442\u0432\u0435\u0442\u043e\u0432.<\/p>\n<p><strong>\u041f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u044b<\/strong> \u2014 \u044d\u0442\u043e \u0444\u0443\u043d\u0434\u0430\u043c\u0435\u043d\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f Nest.\u00a0\u041c\u043d\u043e\u0433\u0438\u0435 \u0438\u0437 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 Nest \u043c\u043e\u0433\u0443\u0442 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u0430\u043a \u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a\u0438: \u0441\u0435\u0440\u0432\u0438\u0441\u044b, \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438, \u0444\u0430\u0431\u0440\u0438\u043a\u0438, \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a\u0438 \u0438 \u0442. \u0434.<\/p>\n<p>\u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u0439\u0442\u0438 \u043d\u0430 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u0430\u0439\u0442\u0435 <a href=\"https:\/\/nestjs.com\/\">Nest<\/a>.<\/p>\n<h3>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0445 \u043f\u0430\u043a\u0435\u0442\u043e\u0432<\/h3>\n<p>\u041d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f:<\/p>\n<ul>\n<li>\n<p>\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 <code>passport<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u043c\u043e\u0434\u0443\u043b\u044c \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 <a href=\"https:\/\/nestjs.com\/\">Nest<\/a> <code>@nestjs\/passport<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u043f\u0430\u043a\u0435\u0442\u00a0<code>passport-jwt<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e JWT;<\/p>\n<\/li>\n<li>\n<p>\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 <code>bcrypt<\/code>, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u0440\u043e\u043b\u0438 \u043f\u0435\u0440\u0435\u0434 \u0438\u0445 \u0437\u0430\u043f\u0438\u0441\u044c\u044e \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445;<\/p>\n<\/li>\n<li>\n<p>\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f TypeScript-\u0442\u0438\u043f\u043e\u0432\u00a0<code>@types\/passport-jwt @types\/bcrypt<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u043f\u0430\u043f\u043a\u0443 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 <code>part-1<\/code>  \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u044b:<\/p>\n<pre><code class=\"bash\">$ npm install --save passport @nestjs\/passport $ npm install --save @nestjs\/jwt passport-jwt $ npm install --save bcrypt $ npm install --save-dev @types\/passport-jwt @types\/bcrypt<\/code><\/pre>\n<h3>\u0417\u0430\u043f\u0443\u0441\u043a \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432<\/h3>\n<p>\u0412 <a href=\"https:\/\/habr.com\/ru\/company\/domclick\/blog\/672546\/\">\u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 <\/a>\u043c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 Docker-\u043e\u0431\u0440\u0430\u0437 \u0441 MongoDB \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043b\u0438 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b. \u041f\u0435\u0440\u0435\u0434 \u043d\u0430\u0447\u0430\u043b\u043e\u043c \u0440\u0430\u0431\u043e\u0442\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043f\u0440\u043e\u0435\u043a\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u0430\u043c\u0438:<\/p>\n<pre><code>docker-compose up -d \/\/ \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c Docker nx run-many --parallel --target=serve --projects=backend,frontend \/\/ \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043c\u043e\u043d\u043e\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0441 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c\u0438<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u043f\u0430\u043f\u043a\u0443 src \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 backend \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/docs.nestjs.com\/cli\/overview\">Nest CLI<\/a> \u0441\u043e\u0437\u0434\u0430\u0451\u043c, \u043c\u043e\u0434\u0443\u043b\u044c, \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0438 \u0441\u0435\u0440\u0432\u0438\u0441. <a href=\"https:\/\/docs.nestjs.com\/cli\/overview\">Nest CLI<\/a> \u2014\u00a0\u044d\u0442\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0432\u0430\u0448\u0438 Nest-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"bash\">nest g module auth \/\/ \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043c\u043e\u0434\u0443\u043b\u044f nest g service auth --no-spec \/\/ \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 nest g controller auth --no-spec \/\/ \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430<\/code><\/pre>\n<p>\u041e\u043f\u0446\u0438\u044f <code>--no-spec<\/code> \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432 \u0441 \u0442\u0435\u0441\u0442\u0430\u043c\u0438 (\u0438\u0445 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0440\u0430\u043c\u043a\u0438 \u0441\u0442\u0430\u0442\u044c\u0438).<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u0444\u0430\u0439\u043b\u044b \u0438 \u0432 <code>AuthModule<\/code> \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b <code>AuthService<\/code> \u0438 <code>AuthController<\/code>, \u0430 <code>AuthModule<\/code> \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d \u0432 <code>AppModule<\/code>. \u042d\u0442\u043e \u0432\u0441\u0451 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0440\u0430\u0431\u043e\u0442\u0435 <a href=\"https:\/\/docs.nestjs.com\/cli\/overview\">Nest CLI<\/a>. \u0415\u0449\u0451 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u044c \u0438 \u0441\u0435\u0440\u0432\u0438\u0441 \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438:<\/p>\n<pre><code>nest g module users \/\/ \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043c\u043e\u0434\u0443\u043b\u044f nest g service users --no-spec \/\/ \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430<\/code><\/pre>\n<p>\u042f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u043e\u0442\u0440\u0430\u0437\u0438\u0442\u044c \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u0446\u0435\u043b\u0438 \u0438 \u0448\u0430\u0433\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430.<\/p>\n<h3>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438<\/h3>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u0435\u0442\u043e\u0434\u044b \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f, \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438, \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u0440\u043e\u043b\u0435\u0439. \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u043e\u0432 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u0432 \u043f\u0430\u043f\u043a\u0443 users, \u0441\u043e\u0437\u0434\u0430\u0442\u044c <a href=\"https:\/\/ru.wikipedia.org\/wiki\/DTO\">DTO<\/a> \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0438 \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f MongoDB.<\/p>\n<p><strong>models\/user.model.ts<\/strong><\/p>\n<pre><code class=\"typescript\">import { Prop, Schema, SchemaFactory } from '@nestjs\/mongoose'; import { Document } from 'mongoose'; import { IUser } from '..\/interfaces\/user.interface';  @Schema({ collection: 'users', timestamps: true }) \/\/ \u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0438\u043c\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432 \u0431\u0430\u0437\u0443 export class UserModel extends Document implements IUser {   @Prop({ required: true }) \/\/ \u0413\u043e\u0432\u043e\u0440\u0438\u0442, \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u044d\u0442\u043e \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435   username: string;        @Prop({ required: true })   password: string;        @Prop({ required: true })   email: string; } export const UserSchema = SchemaFactory.createForClass(UserModel); \/\/ \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u0441\u0445\u0435\u043c\u0443.<\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u0434 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c DTO \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c <code>class-validator class-transformer<\/code>:<\/p>\n<pre><code class=\"bash\">npm i --save class-validator class-transformer<\/code><\/pre>\n<p>\u041e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441 <code>Dto<\/code>, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c\u0441\u044f \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b.<\/p>\n<p><strong>dto\/user.dto.ts<\/strong><\/p>\n<pre><code class=\"typescript\">import { IsNotEmpty, IsEmail } from 'class-validator';  export class UserDto {   @IsNotEmpty() \/\/ \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0443\u0441\u0442\u044b\u043c   id: string;        @IsNotEmpty()   username: string;      @IsEmail() \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 email   email: string; }<\/code><\/pre>\n<p>\u041c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c <code>UserModule<\/code> \u0438 <code>UserService<\/code>. \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c <code>MongooseModule<\/code> \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414 \u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f, \u043f\u043e\u0438\u0441\u043a\u0430 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<p><strong>users.module.ts<\/strong><\/p>\n<pre><code class=\"typescript\">import { Module } from '@nestjs\/common'; import { MongooseModule } from '@nestjs\/mongoose'; import { UserModel, UserSchema } from '.\/models\/user.model'; import { UsersService } from '.\/users.service';  @Module({   imports: [     MongooseModule.forFeature([ \/\/ \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c MongooseModule       {         name: UserModel.name, \/\/ \u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0438\u043c\u044f \u043c\u043e\u0434\u0435\u043b\u0438, \u043e\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c\u0441\u044f \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445         schema: UserSchema, \/\/ \u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0441\u0445\u0435\u043c\u0443 \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445       },     ]),   ],   providers: [UsersService], \/\/ \u041f\u043e\u0441\u043b\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 Nest CLI \u0432 providers \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d UsersService   exports: [UsersService], \/\/ \u042d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c UsersService, \u044d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u043c\u0438 \u043c\u043e\u0434\u0443\u043b\u044f. })<\/code><\/pre>\n<p>\u0412 \u0441\u0435\u0440\u0432\u0438\u0441\u0435 \u0437\u0430\u043b\u043e\u0436\u0435\u043d\u0430 \u0432\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414 \u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438.<\/p>\n<details class=\"spoiler\">\n<summary>users.service.ts<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"typescript\">import { Injectable, HttpException, HttpStatus } from '@nestjs\/common'; import { UserDto } from '.\/dto\/user.dto'; import { CreateUserDto } from '.\/dto\/user.create.dto'; import { LoginUserDto } from '.\/dto\/user-login.dto'; import { toUserDto } from '..\/shared\/mapper'; import { InjectModel } from '@nestjs\/mongoose'; import { UserModel } from '.\/models\/user.model'; import { Model } from 'mongoose'; import { genSalt, hash, compare } from 'bcrypt';  @Injectable() export class UsersService {   constructor(     @InjectModel(UserModel.name)     private readonly userModel: Model&lt;UserModel>\/\/ \u0412\u043d\u0435\u0434\u0440\u044f\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0411\u0414 \u0432 \u0441\u0435\u0440\u0432\u0438\u0441 \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f   ) {}    async findOne(options?: object): Promise&lt;UserDto> {\/\/ \u041c\u0435\u0442\u043e\u0434 \u043f\u043e\u0438\u0441\u043a\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f     const user = await this.userModel.findOne(options).exec();\/\/ \u041c\u043e\u0434\u0435\u043b\u044c \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414     return toUserDto(user); \/\/ \u0413\u043e\u0442\u043e\u0432\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043a \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e   }    async findByLogin({ username, password }: LoginUserDto): Promise&lt;UserDto> {\/\/ \u041c\u0435\u0442\u043e\u0434 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u0438 \u043f\u0430\u0440\u043e\u043b\u044e     const user = await this.userModel.findOne({ username }).exec();      if (!user) { \/\/ \u0415\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0435\u0442, \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443 'User not found'       throw new HttpException('User not found', HttpStatus.UNAUTHORIZED);     }      const areEqual = await compare(password, user.password); \/\/ \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 bcrypt \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0430\u0440\u043e\u043b\u044c \u0438 \u0445\u0435\u0448; \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0440\u0430\u0432\u043d\u044b, \u0442\u043e \u0432\u0435\u0440\u043d\u0451\u0442\u0441\u044f true      if (!areEqual) {\/\/ \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u043e\u043b\u0438 \u043d\u0435 \u0440\u0430\u0432\u043d\u044b, \u0442\u043e \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443 'Invalid credentials'       throw new HttpException('Invalid credentials', HttpStatus.UNAUTHORIZED);     }      return toUserDto(user);    }    async findByPayload({ username }: any): Promise&lt;UserDto> {\/\/ \u041f\u043e\u0438\u0441\u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u0438\u043c\u0435\u043d\u0438     return await this.findOne({ username });   }    async create(userDto: CreateUserDto): Promise&lt;UserDto> {\/\/ \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f     const { username, password, email } = userDto;      const userInDb = await this.userModel.findOne({ username }).exec();     if (userInDb) {\/\/ \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0435\u0441\u0442\u044c \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443       throw new HttpException('User already exists', HttpStatus.BAD_REQUEST);     }      const salt = await genSalt(10); \/\/ \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 bycrypt \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0441\u043e\u043b\u044c     const hashPassword = await hash(password, salt); \/\/ bycrypt \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0445\u0435\u0448 \u043f\u0430\u0440\u043e\u043b\u044f      const user: UserModel = await new this.userModel({ \/\/ \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f       username,       password: hashPassword,       email,     });          await user.save(); \/\/ \u0421\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0432 \u0411\u0414      return toUserDto(user);   }    private _sanitizeUser(user: UserModel) {     delete user.password;     return user;   } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<h3>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/h3>\n<p>\u0420\u0430\u043d\u0435\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/docs.nestjs.com\/cli\/overview\">Nest CLI<\/a> \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0435\u0439, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0435\u0449\u0451 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 <a href=\"https:\/\/github.com\/jaredhanson\/passport\">Passport<\/a> \u0438 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e. \u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u043f\u0430\u043f\u043a\u0443 auth \u0438 \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0444\u0430\u0439\u043b jwt.strategy.ts \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439:<\/p>\n<details class=\"spoiler\">\n<summary>jwt.strategy.ts<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"typescript\">import { PassportStrategy } from '@nestjs\/passport'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { AuthService } from '.\/auth.service'; import { Injectable, HttpException, HttpStatus } from '@nestjs\/common'; import { JwtPayload } from '.\/interfaces\/payload.interface'; import { UserDto } from '..\/users\/dto\/user.dto';  @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) {\/\/ \u041d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u0441\u044f \u043e\u0442 PassportStrategy, \u0434\u043e\u0431\u0430\u043b\u044f\u0435\u043c \u043c\u0435\u0442\u043e\u0434\u044b \u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438   constructor(private readonly authService: AuthService) {     super({       jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),       secretOrKey: process.env.JWT_KEY + '',     });   }    async validate(payload: JwtPayload): Promise&lt;UserDto> { \/\/ \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u043c\u0435\u0442\u043e\u0434 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438     const user = await this.authService.validateUser(payload);     if (!user) {       throw new HttpException('Invalid token', HttpStatus.UNAUTHORIZED);     }     return user;   } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0432 \u043c\u043e\u0434\u0443\u043b\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<details class=\"spoiler\">\n<summary>auth.module.ts<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"typescript\">import { Module } from '@nestjs\/common'; import { AuthController } from '.\/auth.controller'; import { AuthService } from '.\/auth.service'; import { JwtModule } from '@nestjs\/jwt'; import { PassportModule } from '@nestjs\/passport'; import { UsersModule } from '..\/users\/users.module'; import { JwtStrategy } from '.\/jwt.strategy'; import { ConfigService } from '@nestjs\/config';  @Module({   imports: [     UsersModule,<\/code><\/pre>\n<\/div>\n<\/details>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-338283","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/338283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=338283"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/338283\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=338283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=338283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=338283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}