{
    "componentChunkName": "component---src-templates-blog-post-js",
    "path": "/implement-jwt-based-user-authentication-in-a-mern-stack-app-part-7/",
    "result": {"data":{"markdownRemark":{"html":"<div class=\"table-of-contents\">\n<ul>\n<li><a href=\"#what-is-a-env-file\">What is a .env file?</a></li>\n<li><a href=\"#connecting-to-mongodb\">Connecting to MongoDB</a></li>\n</ul>\n</div>\n<h6 id=\"this-blog-post-is-part-of-a-series-you-must-finish-part-1-part-2-part-3-part-4-part-5--part-6-before-continuing-here\" style=\"position:relative;\"><a href=\"#this-blog-post-is-part-of-a-series-you-must-finish-part-1-part-2-part-3-part-4-part-5--part-6-before-continuing-here\" aria-label=\"this blog post is part of a series you must finish part 1 part 2 part 3 part 4 part 5  part 6 before continuing here permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>This blog post is part of a series. You must finish <a href=\"https://hemanta.io/implement-jwt-based-user-authentication-in-a-mern-stack-app-part-1/\">part-1</a>, <a href=\"https://hemanta.io/implement-jwt-based-user-authentication-in-a-mern-stack-app-part-2/\">part-2</a>, <a href=\"https://hemanta.io/implement-jwt-based-user-authentication-in-a-mern-stack-app-part-3/\">part-3</a>, <a href=\"https://hemanta.io/implement-jwt-based-user-authentication-in-a-mern-stack-app-part-4/\">part-4</a>, <a href=\"https://hemanta.io/implement-jwt-based-user-authentication-in-a-mern-stack-app-part-5/\">part-5</a> &#x26; <a href=\"https://hemanta.io/implement-jwt-based-user-authentication-in-a-mern-stack-app-part-6/\">part-6</a> before continuing here.</h6>\n<p>We will use mongoose to interact with our database. Mongoose will act as the frontend to our MongoDB database.</p>\n<blockquote>\n<p><strong>What is mongoose?</strong> <br>\nMongoose is what is known as an Object Data Model (ODM). An ODM represents the website’s data as JavaScript objects, which are then mapped to the underlying database.<br>\nODMs are often slower because they often use translation code to map between objects and the database format. The benefit of using an ODM is that programmers can continue to think in terms of JavaScript objects rather than database semantics. Moreover, using an ODM often results in lower costs for development and maintenance.</p>\n</blockquote>\n<p>Sign in to your MongoDB Atlas account. In the clusters view, click on <del>CONNECT</del>.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 700px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/40a9dc3114cb016860b2561269739c01/a27c6/atlas16.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 63.5%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABnklEQVQoz5VSS47bMAz14XqXrnuOnqarLmfbA7TAYAZN4vhTK3ZsfShSlCi5sBPMpLvpAyEIT3okxadqWHw72WZys4/rf6Iahj8vr6/1ueYQblTZ8SFxzrlkSSmJiORcSmGOzB/qokJOp1Fbupe9190hOac9Yy63KHnb3/kkUrkYfqrlMNF5CacrYZS3xB4RELW13aB6dam7vleXc9/3gwJC532VyF6HRqveArggt1KS87quGIL1vlOq7vrjuT3UzfHcHpvu1HbTogGxyktvmhdsn/XQaGONMeM4Lno7QyILMBtjrGsu6lf7+7k7XbVejNXWbeIUeZ7G+TpG5v1F73NGIofo0K9p/X748enbl89PX5WdQ+CN39pOaV60c56IAvPjMCkEQARCj6TBDvo6mtl5v5PkiSpmVurCG8K+8ltxZgbwFIIxJqZ08yLvSDECQBVjtNamlHZ1eGz7dm8zLZf1318jIjHGTawu4zhNi9YxxvffVdbN27VIyVtkeYyUpZRSiYhzDgAcABIhUZa71cg0mdkFDDmS8GNAxBTTX9mz7y0ArUn0AAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"MongoDB Atlas account creation\"\n        title=\"MongoDB Atlas account creation\"\n        src=\"/static/40a9dc3114cb016860b2561269739c01/a27c6/atlas16.png\"\n        srcset=\"/static/40a9dc3114cb016860b2561269739c01/56d15/atlas16.png 200w,\n/static/40a9dc3114cb016860b2561269739c01/d9f49/atlas16.png 400w,\n/static/40a9dc3114cb016860b2561269739c01/a27c6/atlas16.png 700w\"\n        sizes=\"(max-width: 700px) 100vw, 700px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>Then, click on <del>Connect your application</del>.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 774px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/9fcb2ade855892a0b54307dd50a3da26/d1716/atlas17.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 65.49999999999999%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABaklEQVQoz4WSwY7dIAxF8///0g/oortuuu+ulUaVqpkkYDBgG0LAVElmnl6n89SjK2QBF2PDVEoB5533HhFDsAAAbl1NKWX8j0lVtXfteqm/cduhqo/NY8zZ3eQrMbE/L+KcswA5ZxEp2/aBORHNZvWIy7qCc4sxLhWUClSD1HGm1ZMPzMfBJMycDiglIpaYCGMilkR0X8J7897bwk5ypsPFF621q3x9wLnUJ67l889vgN4YC2eR1rp4pGVmuWCRnP9qfj/9k4Zl5DjYDfFDXkclUIJjkl9jJejJ3gl6WCaKy5flu0V8ntfZ2F+/n31IdW/b3krd87afQdv7fcO0j9EZpx1fLL40wc7YODQOXUKTUJPfIpQA17gTao439Zx6mKea0xM8JbQJ1ggrmhnNHOyySxyVdTs0qowqV3ypF9LCE9Xy6cdXG4LzwYI34FbrrPOJhThH4lw2HePdK3c9NL29/j9/QPWmR/wBw5byQhzQSXsAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"MongoDB Atlas account creation\"\n        title=\"MongoDB Atlas account creation\"\n        src=\"/static/9fcb2ade855892a0b54307dd50a3da26/d1716/atlas17.png\"\n        srcset=\"/static/9fcb2ade855892a0b54307dd50a3da26/56d15/atlas17.png 200w,\n/static/9fcb2ade855892a0b54307dd50a3da26/d9f49/atlas17.png 400w,\n/static/9fcb2ade855892a0b54307dd50a3da26/d1716/atlas17.png 774w\"\n        sizes=\"(max-width: 774px) 100vw, 774px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>Copy the connection string.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 775px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/8836d387de19e5df717eaf79bf208fa4/49bb6/atlas18.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 56.49999999999999%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABU0lEQVQoz4WP227cMAxE/f8fWPQlaZHuxpYoiRIvsi62VHibFkjTIoMDgiQw4HApe2FiYVFRFaVEKRElOvoxP9My5tSz6FHk2OUobRzt7Nx36Xuqkqpq32utOecxxt9mydlwgBytolWkIrkXt6dYxZfkS6KmVzqRf5h/3G5gIUtW1iyZiY/j88Bv5jmnBcAY+9Fbr6K6rhsz997bQ7XW1vp/fh5DVZi51lL2S3LpGh/UUkprbc7xkeXldjfgWXKIEROFmEgUE3mMkfn88Oe7y2criobdqsGI3wjWSn4WHvs7fm3+1AeyzIyanDWvFD16650J3nowGGxwFwkdBvBw7TGAgy2hy8oN12Uq5gge1uBWb1+duXGEllPNsShWjb/7UDMWeaPvPMgs4+wJNnD3b+7Z+ht5i7Al2Lxfn+yTd2u02+buX+H7F/McExTCTNiUZi8/AWg9ehxLsx9UAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"MongoDB Atlas account creation\"\n        title=\"MongoDB Atlas account creation\"\n        src=\"/static/8836d387de19e5df717eaf79bf208fa4/49bb6/atlas18.png\"\n        srcset=\"/static/8836d387de19e5df717eaf79bf208fa4/56d15/atlas18.png 200w,\n/static/8836d387de19e5df717eaf79bf208fa4/d9f49/atlas18.png 400w,\n/static/8836d387de19e5df717eaf79bf208fa4/49bb6/atlas18.png 775w\"\n        sizes=\"(max-width: 775px) 100vw, 775px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>Back to our application, create a <del>.env</del> file in the <del>mern-auth</del> folder.\nIn the <del>.env</del> file, paste the connection string that you copied from MongoDB Atlas as the value of the key <del>MONGO_URI</del>. Don’t place the connection string within double quotes.</p>\n<div class=\"gatsby-code-title\">mern-auth/.env</div>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"sh\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">MONGO_URI=mongodb+srv://sundaray:</span><span class=\"mtk10\">&lt;</span><span class=\"mtk1\">password</span><span class=\"mtk10\">&gt;</span><span class=\"mtk1\">@sandbox.y8blv.mongodb.net/myFirstDatabase</span><span class=\"mtk10\">?</span><span class=\"mtk1\">retryWrites=true</span><span class=\"mtk10\">&amp;</span><span class=\"mtk1\">w=majority</span></span></span></code></pre>\n<p>Replace <del>&#x3C;password></del> with the password that you have set for the database user. And replace <del>myFirstDatabase</del> with the name of your database.</p>\n<h3 id=\"what-is-a-env-file\" style=\"position:relative;\"><a href=\"#what-is-a-env-file\" aria-label=\"what is a env file permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is a .env file?</h3>\n<p><del>.env</del> is a simple configuration text file that is used to define environment variables that we want to pass into our application’s environment. By convention <del>.env</del> variable names are uppercase words separated by underscores. Variable names are directly followed by an <del>=</del>, which in turn is directly followed by the value, for example: <del>VARIABLE_NAME=value</del>.</p>\n<p>The <del>.env</del> file will contain secret environment variables that we don’t want to expose to the outside world. If you are using git to push your code to a remote repository such as GitHub you wouldn’t want to push the <del>.env</del> file to GitHub and make it public. Therefore, add the <del>.env</del> file in the <del>.gitignore</del> file.</p>\n<div class=\"gatsby-code-title\">mern-auth/.gitignore</div>\n<pre class=\"grvsc-container grvsc-has-line-highlighting synthwave-84\" data-language=\"sh\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk4 mtki\"># See https://help.github.com/articles/ignoring-files/ for more about ignoring files.</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"2\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"3\"></span><span class=\"grvsc-source\"><span class=\"mtk4 mtki\"># dependencies</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"4\"></span><span class=\"grvsc-source\"><span class=\"mtk10\">**</span><span class=\"mtk1\">/node_modules</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"5\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">/.pnp</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"6\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">.pnp.js</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"7\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"8\"></span><span class=\"grvsc-source\"><span class=\"mtk4 mtki\"># testing</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"9\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">/coverage</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"10\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"11\"></span><span class=\"grvsc-source\"><span class=\"mtk4 mtki\"># production</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"12\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">/build</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"13\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"14\"></span><span class=\"grvsc-source\"><span class=\"mtk4 mtki\"># misc</span></span></span>\n<span class=\"grvsc-line grvsc-line-highlighted\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"15\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">.env</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"16\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">.DS_Store</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"17\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">.env.local</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"18\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">.env.development.local</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"19\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">.env.test.local</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"20\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">.env.production.local</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"21\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"22\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">npm-debug.log</span><span class=\"mtk10\">*</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"23\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">yarn-debug.log</span><span class=\"mtk10\">*</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"24\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">yarn-error.log</span><span class=\"mtk10\">*</span></span></span></code></pre>\n<p>We have stored the connection string in the <del>.env</del> file. Next, we will use mongoose to connect to our MongoDB database.</p>\n<h3 id=\"connecting-to-mongodb\" style=\"position:relative;\"><a href=\"#connecting-to-mongodb\" aria-label=\"connecting to mongodb permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Connecting to MongoDB</h3>\n<p>First, we need to install mongoose.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"sh\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">PS C:</span><span class=\"mtk6\">\\U</span><span class=\"mtk1\">sers</span><span class=\"mtk6\">\\D</span><span class=\"mtk1\">elhivery</span><span class=\"mtk6\">\\D</span><span class=\"mtk1\">esktop</span><span class=\"mtk6\">\\m</span><span class=\"mtk1\">ern-auth</span><span class=\"mtk10\">&gt;</span><span class=\"mtk1\"> npm i mongoose</span></span></span></code></pre>\n<p>Note that installing mongoose adds all its dependencies including the MongoDB database driver. The MongoDB Node.js driver allows Node.js applications to connect to MongoDB and work with data.</p>\n<p>We also need to install a module called <del>dotenv</del> that will help us load the environment variables we have defined inside the <del>.env</del> file into <del>process.env</del>.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"sh\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\"> PS C:</span><span class=\"mtk6\">\\U</span><span class=\"mtk1\">sers</span><span class=\"mtk6\">\\D</span><span class=\"mtk1\">elhivery</span><span class=\"mtk6\">\\D</span><span class=\"mtk1\">esktop</span><span class=\"mtk6\">\\m</span><span class=\"mtk1\">ern-auth</span><span class=\"mtk10\">&gt;</span><span class=\"mtk1\"> npm i dotenv</span></span></span></code></pre>\n<blockquote>\n<p>Note that you can install multiple npm packages with a single command. All we need to do is to use the npm install command followed by multiple package names separated by spaces, as shown below:</p>\n</blockquote>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"sh\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-source\"><span class=\"mtk1\">PS C:</span><span class=\"mtk6\">\\U</span><span class=\"mtk1\">sers</span><span class=\"mtk6\">\\D</span><span class=\"mtk1\">elhivery</span><span class=\"mtk6\">\\D</span><span class=\"mtk1\">esktop</span><span class=\"mtk6\">\\m</span><span class=\"mtk1\">ern-auth</span><span class=\"mtk10\">&gt;</span><span class=\"mtk1\"> npm i mongoose dotenv</span></span></span></code></pre>\n<p>Inside the <del>server</del> folder, create a folder named <del>config</del> and inside the <del>config</del> folder create a file named <del>db.js</del> and paste the following code snippet in that file.</p>\n<div class=\"gatsby-code-title\">server/config/db.js</div>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"js\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk10\">const</span><span class=\"mtk1\"> </span><span class=\"mtk3\">mongoose</span><span class=\"mtk1\"> </span><span class=\"mtk12\">=</span><span class=\"mtk1\"> </span><span class=\"mtk6\">require</span><span class=\"mtk1\">(</span><span class=\"mtk16\">&quot;mongoose&quot;</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"2\"></span><span class=\"grvsc-source\"><span class=\"mtk10\">const</span><span class=\"mtk1\"> </span><span class=\"mtk3\">dotenv</span><span class=\"mtk1\"> </span><span class=\"mtk12\">=</span><span class=\"mtk1\"> </span><span class=\"mtk6\">require</span><span class=\"mtk1\">(</span><span class=\"mtk16\">&quot;dotenv&quot;</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"3\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"4\"></span><span class=\"grvsc-source\"><span class=\"mtk3\">dotenv</span><span class=\"mtk1\">.</span><span class=\"mtk6\">config</span><span class=\"mtk1\">()</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"5\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"6\"></span><span class=\"grvsc-source\"><span class=\"mtk10\">const</span><span class=\"mtk1\"> </span><span class=\"mtk6\">connectDB</span><span class=\"mtk1\"> </span><span class=\"mtk12\">=</span><span class=\"mtk1\"> </span><span class=\"mtk10\">async</span><span class=\"mtk1\"> () </span><span class=\"mtk10\">=&gt;</span><span class=\"mtk1\"> {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"7\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">  </span><span class=\"mtk10\">try</span><span class=\"mtk1\"> {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"8\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk10\">const</span><span class=\"mtk1\"> </span><span class=\"mtk3\">conn</span><span class=\"mtk1\"> </span><span class=\"mtk12\">=</span><span class=\"mtk1\"> </span><span class=\"mtk10\">await</span><span class=\"mtk1\"> </span><span class=\"mtk3\">mongoose</span><span class=\"mtk1\">.</span><span class=\"mtk6\">connect</span><span class=\"mtk1\">(</span><span class=\"mtk3\">process</span><span class=\"mtk1\">.</span><span class=\"mtk3\">env</span><span class=\"mtk1\">.</span><span class=\"mtk7\">MONGO_URI</span><span class=\"mtk1\">, {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"9\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">      </span><span class=\"mtk3\">useNewUrlParser</span><span class=\"mtk14\">:</span><span class=\"mtk1\"> </span><span class=\"mtk5\">true</span><span class=\"mtk1\">,</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"10\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">      </span><span class=\"mtk3\">useCreateIndex</span><span class=\"mtk14\">:</span><span class=\"mtk1\"> </span><span class=\"mtk5\">true</span><span class=\"mtk1\">,</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"11\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">      </span><span class=\"mtk3\">useUnifiedTopology</span><span class=\"mtk14\">:</span><span class=\"mtk1\"> </span><span class=\"mtk5\">true</span><span class=\"mtk1\">,</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"12\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">    })</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"13\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk9\">console</span><span class=\"mtk1\">.</span><span class=\"mtk6\">log</span><span class=\"mtk1\">(</span><span class=\"mtk16\">`MongoDB connected: </span><span class=\"mtk8\">${</span><span class=\"mtk3\">conn</span><span class=\"mtk14\">.</span><span class=\"mtk3\">connection</span><span class=\"mtk14\">.</span><span class=\"mtk7\">host</span><span class=\"mtk8\">}</span><span class=\"mtk16\">`</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"14\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">  } </span><span class=\"mtk10\">catch</span><span class=\"mtk1\"> (</span><span class=\"mtk3\">error</span><span class=\"mtk1\">) {</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"15\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk9\">console</span><span class=\"mtk1\">.</span><span class=\"mtk6\">error</span><span class=\"mtk1\">(</span><span class=\"mtk16\">`Error: </span><span class=\"mtk8\">${</span><span class=\"mtk3\">error</span><span class=\"mtk14\">.</span><span class=\"mtk7\">message</span><span class=\"mtk8\">}</span><span class=\"mtk16\">`</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"16\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">    </span><span class=\"mtk3\">process</span><span class=\"mtk1\">.</span><span class=\"mtk6\">exit</span><span class=\"mtk1\">(</span><span class=\"mtk7\">1</span><span class=\"mtk1\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"17\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">  }</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"18\"></span><span class=\"grvsc-source\"><span class=\"mtk1\">}</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"19\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"20\"></span><span class=\"grvsc-source\"><span class=\"mtk9\">module</span><span class=\"mtk1\">.</span><span class=\"mtk9\">exports</span><span class=\"mtk1\"> </span><span class=\"mtk12\">=</span><span class=\"mtk1\"> </span><span class=\"mtk3\">connectDB</span></span></span></code></pre>\n<p>Mongoose requires a connection to our MongoDB database. We connect to our MongoDB database using <del>mongoose.connect</del> method. The first argument to the connect method is our connection string.</p>\n<p>The <del>dotenv</del> module loads the environment variables that we have defined inside the <del>.env</del> file as key-value pairs inside <del>process.env</del>. Therefore, we can access our connection string using <del>process.env.MONGO_URI</del>.</p>\n<p>We can programmatically terminate a Node.js application by using the <del>exit</del> method of the <del>process</del> core module. When Node.js runs this line, the process is immediately forced to terminate. We can also pass an integer that signals the operating system the exit code.</p>\n<p>Go to <a href=\"https://hemanta.io/implement-jwt-based-user-authentication-in-a-mern-stack-app-part-8/\">part-8</a></p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .synthwave-84 { background-color: #262335; }\n  .synthwave-84 .mtki { font-style: italic; }\n  .synthwave-84 .mtk1 { color: #FFFFFF; }\n  .synthwave-84 .mtk10 { color: #FEDE5D; }\n  .synthwave-84 .mtk4 { color: #848BBD; }\n  .synthwave-84 .mtk6 { color: #36F9F6; }\n  .synthwave-84 .mtk3 { color: #FF7EDB; }\n  .synthwave-84 .mtk12 { color: #FFFFFFEE; }\n  .synthwave-84 .mtk16 { color: #FF8B39; }\n  .synthwave-84 .mtk7 { color: #2EE2FA; }\n  .synthwave-84 .mtk14 { color: #B6B1B1; }\n  .synthwave-84 .mtk5 { color: #F97E72; }\n  .synthwave-84 .mtk9 { color: #FE4450; }\n  .synthwave-84 .mtk8 { color: #72F1B8; }\n  .synthwave-84 .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(255, 255, 255, 0.1));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(255, 255, 255, 0.5));\n  }\n</style>","frontmatter":{"title":"Implement JWT Based User Authentication in a MERN Stack App - Part 7","date":"2021-05-23"}}},"pageContext":{"slug":"/implement-jwt-based-user-authentication-in-a-mern-stack-app-part-7/","prev":{"fields":{"slug":"/implement-jwt-based-user-authentication-in-a-mern-stack-app-part-6/"},"frontmatter":{"modules":null}},"next":{"fields":{"slug":"/implement-jwt-based-user-authentication-in-a-mern-stack-app-part-8/"},"frontmatter":{"modules":null}}}},
    "staticQueryHashes": ["3159585216"]}