{"id":2234,"date":"2022-01-25T17:52:49","date_gmt":"2022-01-25T17:52:49","guid":{"rendered":"https:\/\/lvboard.infostore.in.ua\/?p=2234"},"modified":"2022-01-25T17:52:49","modified_gmt":"2022-01-25T17:52:49","slug":"adding-a-next-js-administration-dashboard-to-your-site-in-seconds","status":"publish","type":"post","link":"https:\/\/lvboard.infostore.in.ua\/?p=2234","title":{"rendered":"Adding a Next.js Administration Dashboard to your site in seconds"},"content":{"rendered":"\n<p>Making an administration dashboard for all my freelance clients is always a challenge. It&#8217;s just annoying to create a new database, add it to your site, enable authentication, etc.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>It&#8217;s understandable why a lot of people use WordPress instead of raw code, mainly for the reason of the dashboard and how easy it is to edit things.<\/p>\n\n\n\n<p>Guess what? I just changed that for all you React and Next.js developers out there. You won&#8217;t have to use WordPress over your favorite stack anymore or spend hours sweating your brain out coding an admin dashboard. All it takes to implement this is a git clone, editing some JSON and ENV values, and your dashboard is ready to go!<\/p>\n\n\n\n<p>Take a look at the demo and try it out!<br><a href=\"https:\/\/next-admin.ironcladdev.repl.co\/\">https:\/\/next-admin.ironcladdev.repl.co\/<\/a><\/p>\n\n\n\n<h3><a href=\"https:\/\/dev.to\/ironcladdev\/adding-a-nextjs-administration-dashboard-to-your-site-in-seconds-7jb#getting-the-code\"><\/a>Getting the Code<\/h3>\n\n\n\n<p>Open your favorite code editor and run&nbsp;<code>git clone https:\/\/github.com\/Conner1115\/next-admin.git<\/code>. You won&#8217;t even have to run&nbsp;<code>npx create-next-app<\/code>&nbsp;since all the files are already there for you. Simply run&nbsp;<code>npm run dev<\/code>&nbsp;and then start building your site around that.<\/p>\n\n\n\n<h3><a href=\"https:\/\/dev.to\/ironcladdev\/adding-a-nextjs-administration-dashboard-to-your-site-in-seconds-7jb#customizing-authentication\"><\/a>Customizing Authentication<\/h3>\n\n\n\n<p>Before you start on the admin dashboard go to the&nbsp;<code>.env<\/code>&nbsp;file. Set a strong password, which is the one you&#8217;ll be using to log in and out of your dashboard, and a long random string for the session cookie. This enables security and prevents brute-forcing.<\/p>\n\n\n\n<p>You&#8217;ll need to set two fields in the&nbsp;<code>.env<\/code>&nbsp;file.&nbsp;<code>ADMIN_SESSION<\/code>&nbsp;which is the long random string and&nbsp;<code>ADMIN_PASSWORD<\/code>&nbsp;which is the password you&#8217;ll use to log in.<\/p>\n\n\n\n<p>Don&#8217;t worry, I&#8217;ve already applied Rate Limiting on the password &#8216;n all (three failed login attempts per thirty minutes), so no worries on the bad guys getting in your site and messing up all the content.<\/p>\n\n\n\n<h3><a href=\"https:\/\/dev.to\/ironcladdev\/adding-a-nextjs-administration-dashboard-to-your-site-in-seconds-7jb#customizing-the-dashboard\"><\/a>Customizing the Dashboard<\/h3>\n\n\n\n<p>Go to the&nbsp;<code>data<\/code>&nbsp;folder and then open up&nbsp;<code>data.json<\/code>. This is where you will control the different types of data that can be edited.<\/p>\n\n\n\n<p>If you went to the admin page in the demo, which is probably corrupted to a massive extent, and played around with it, you can see that the fields consist of single string fields, multiple strings fields, and arrays.<\/p>\n\n\n\n<h4><a href=\"https:\/\/dev.to\/ironcladdev\/adding-a-nextjs-administration-dashboard-to-your-site-in-seconds-7jb#create-a-single-string-field\"><\/a>Create a single String Field<\/h4>\n\n\n\n<p>Open up&nbsp;<code>data.json<\/code>, clear out all the existing fields, and add an item&nbsp;<code>\"homepage\"<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"homepage\": \"This is the text that goes on the homepage\"\n}\n<\/code><\/pre>\n\n\n\n<p>Run&nbsp;<code>npm run dev<\/code>&nbsp;in the terminal and go to&nbsp;<a href=\"http:\/\/localhost:3000\/admin\">http:\/\/localhost:3000\/admin<\/a>.<br>You should be seeing something already after you log in.<br><a href=\"https:\/\/res.cloudinary.com\/practicaldev\/image\/fetch\/s--1wgrdIva--\/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880\/https:\/\/dev-to-uploads.s3.amazonaws.com\/uploads\/articles\/4u94wu27t3b5ax5qanf4.png\"><\/a><\/p>\n\n\n\n<p>Single-text-fields can be useful for editing short headlines, descriptions, paragraphs, and more. As for pages that have multiple paragraphs, features, or other things that require more content, you can use Multi-string fields.<\/p>\n\n\n\n<h4><a href=\"https:\/\/dev.to\/ironcladdev\/adding-a-nextjs-administration-dashboard-to-your-site-in-seconds-7jb#create-a-multistring-field\"><\/a>Create a Multi-string field<\/h4>\n\n\n\n<p>Open up&nbsp;<code>data.json<\/code>&nbsp;and add a field &#8220;paragraphs&#8221; as an array of strings. This array can be of any length.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"homepage\": \"This is the text that goes on the homepage\",\n  \"paragraphs\": &#91;\"This is paragraph One\", \"This is paragraph Two\", \"I'm the third paragraph\"]\n}\n<\/code><\/pre>\n\n\n\n<p>In this example, the length of this array will be three and generate three editable text fields.<br><a href=\"https:\/\/res.cloudinary.com\/practicaldev\/image\/fetch\/s--77WqOFf4--\/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880\/https:\/\/dev-to-uploads.s3.amazonaws.com\/uploads\/articles\/e2e3lfcr0zclijoam41a.png\"><\/a><\/p>\n\n\n\n<h4><a href=\"https:\/\/dev.to\/ironcladdev\/adding-a-nextjs-administration-dashboard-to-your-site-in-seconds-7jb#array-field\"><\/a>Array Field<\/h4>\n\n\n\n<p>Array Fields are particularly useful for simple blogs and other storage functions where authentication isn&#8217;t required.<\/p>\n\n\n\n<p>Create a new field &#8220;posts&#8221; and set it to an empty array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"homepage\": \"This is the text that goes on the homepage\",\n  \"paragraphs\": &#91;\"This is paragraph One\", \"This is paragraph Two\", \"I'm the third paragraph\"],\n  \"posts\": &#91;]\n}\n<\/code><\/pre>\n\n\n\n<p>Go&nbsp;<code>schemas.json<\/code>&nbsp;and add an object &#8220;posts&#8221;. From there you can set the input fields.&nbsp;<strong>Make sure the field in schemas.json is the same name as data.json<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"posts\": {\n    \"title\": \"text\",\n    \"text\": \"textarea\",\n    \"date\": \"date\",\n    \"luckynumber\": \"number\"\n  }\n}\n<\/code><\/pre>\n\n\n\n<p>That, of course, is just an example.&nbsp;<code>\"date\"<\/code>&nbsp;and&nbsp;<code>\"luckynumber\"<\/code>&nbsp;exist as examples to show what types of inputs there are. The type of input (besides &#8220;textarea&#8221;) will always be a type in an html&nbsp;<code>&lt;input&gt;<\/code>&nbsp;tag. If &#8220;textarea&#8221; is used, a&nbsp;<code>&lt;textarea&gt;<\/code>&nbsp;tag will be generated.<\/p>\n\n\n\n<p>Now go to your admin dashboard and try it out. After creating a post, you should see a new element added to your schema array in&nbsp;<code>data.json<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  ...\n  \"posts\": &#91;\n    {\n      \"title\": \"Title\",\n      \"text\": \"Test Text\",\n      \"date\": \"2022-01-21\",\n      \"luckynumber\": \"1\",\n      \"id\": \"d8d2ef38-b02c-496b-9833-c53fa3b063c9\"\n    }\n  ]\n}\n<\/code><\/pre>\n\n\n\n<p>All schema-type array fields automatically will be given a random&nbsp;<code>uuid<\/code>.<\/p>\n\n\n\n<p>You may add as many fields to the&nbsp;<code>data.json<\/code>&nbsp;object as you want. All top-level object keys will be added and editable in the dashboard.<\/p>\n\n\n\n<h3><a href=\"https:\/\/dev.to\/ironcladdev\/adding-a-nextjs-administration-dashboard-to-your-site-in-seconds-7jb#accessing-the-data\"><\/a>Accessing the data<\/h3>\n\n\n\n<p>To get the data from&nbsp;<code>data.json<\/code>&nbsp;and use it in your pages, simply import it in one of your pages and use it as a valid javascript object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json from '..\/..\/data\/data.json';\n\nexport default function Home(){\n  return (\n    &lt;div&gt;\n      &lt;h1&gt;{json.headline}&lt;\/h1&gt;\n    &lt;\/div&gt;\n  );\n}\n<\/code><\/pre>\n\n\n\n<p>You can access the json file from the client side of your application as well as the server side.<\/p>\n\n\n\n<h3><a href=\"https:\/\/dev.to\/ironcladdev\/adding-a-nextjs-administration-dashboard-to-your-site-in-seconds-7jb#writing-to-raw-datajson-endraw-\"><\/a>Writing to&nbsp;<code>data.json<\/code><\/h3>\n\n\n\n<p>Writing to data.json from anywhere else besides the original admin api routes can be done from another api route. You may use the&nbsp;<code>writeJSON<\/code>&nbsp;function which is located under&nbsp;<code>scripts\/util.js<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<code>writeJSON<\/code>&nbsp;function takes a single parameter, which is a function that should return an object.<\/p>\n\n\n\n<p>The object returned will overwrite the entire json file completely.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"field\": \"value\"\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>writeJSON((json) =&gt; ({\n  ...json,\n  \/\/updating a field\n  \"field\": \"newvalue\",\n  \/\/adding a new field\n  \"field2\", \"value\"\n}))\n<\/code><\/pre>\n\n\n\n<p>Have fun, try your best to not abuse the playground demo too much, and feel free to use this admin page in any of your projects.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Making an administration dashboard for all my freelance clients is always a challenge. It&#8217;s just annoying to create a new database, add it to your site, enable authentication, etc.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[30],"tags":[177,161],"_links":{"self":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2234"}],"collection":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2234"}],"version-history":[{"count":1,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2234\/revisions"}],"predecessor-version":[{"id":2235,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2234\/revisions\/2235"}],"wp:attachment":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}