Skip to content

Commit

Permalink
updated pass post
Browse files Browse the repository at this point in the history
  • Loading branch information
lazydeveloper committed Jul 9, 2024
1 parent abe90f8 commit 3a91c35
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
Binary file added assets/img/postpass.mp4
Binary file not shown.
24 changes: 11 additions & 13 deletions content/posts/adding_password_protection_to_a_hugo_post.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ This method lacks complete security. Anyone with basic Hugo knowledge can access
This post do not talk about

{{< admonition type=success title="What this post cover" open=false >}}
1. Easy method to assign a password to any Hugo post.
1. Easy method to assign a password to any Hugo post (not 100% secured)
2. Users will be prompted to enter a password to access the post.
3. Unique password for each post
{{< /admonition >}}




Before we proceed, open the post [Top Secret](http://localhost:1313/posts/top_secret/) that is protection with a password. When prompted, enter the password `1234` to read the top secret.

## Add a new parameter, `password`
![password-protection](https://pouch.jumpshare.com/preview/GA_t8X7rJkwOSdsolhK4H7ND9kHBOgRq7g98Vcr_R62DovJUg6TLF0gQt5140AoHfJD7SLqCLZnupx2aUWWIM4A9dVtAxDj6JHloBdAvI18)

----
## 1. Add a new parameter, `password`
To give you a sense of what I mean by "Add a new parameter" take a look at the `password` parameter at the line [#9](https://github.com/lazydeveloper/lazydeveloper.github.io/blob/1477fa46fe7425899d15470398e4405f6c0cfb0a/content/posts/top_secret.md?plain=1#L8) in the code below.
```
---
Expand All @@ -42,11 +42,12 @@ password: "1234"
```
Any value you input for the password parameter here (for example, 1234) will act as the password for accessing this post. So, include this new parameter in an existing or new post, as we'll proceed to implement the password protection logic in the following step.

## Implementing logic for the password parameter
----
## 2. Implementing logic for the password parameter

It's time to write the logic, Let's add a script to the default post layout. In my case its [single.html](https://github.com/lazydeveloper/lazydeveloper.github.io/blob/main/themes/LoveIt/layouts/posts/single.html) located at `themes/LoveIt/layouts/posts/`. The path or the file name may be different in your setup.

### Add the following code snippets to your layout file (single.html).
**Add the following code snippets to your layout file (single.html).**

01. Add this div at the starting of the article class. This will ensures that the content is hidden initially if a password is set.
```
Expand All @@ -71,19 +72,16 @@ It's time to write the logic, Let's add a script to the default post layout. In
};
</script>
```
Here my [single.html](https://github.com/lazydeveloper/lazydeveloper.github.io/blob/1477fa46fe7425899d15470398e4405f6c0cfb0a/themes/LoveIt/layouts/posts/single.html) file for quick reference. Look at the line#[21](https://github.com/lazydeveloper/lazydeveloper.github.io/blob/1477fa46fe7425899d15470398e4405f6c0cfb0a/themes/LoveIt/layouts/posts/single.html#L22) and line#[104 to 118](https://github.com/lazydeveloper/lazydeveloper.github.io/blob/1477fa46fe7425899d15470398e4405f6c0cfb0a/themes/LoveIt/layouts/posts/single.html#L104)
## Testing the protection
----
## 3. Testing the protection
Now that you know how to add a password parameter and the logic to check the password, you can create a new post with a password parameter or add the password parameter to an existing post. This method should work fine.
However, as previously discussed, this method is not 100% secure. At the time of writing this post, I'm not certain about replacing the password with a GitHub variable or secret, or using any similar mechanism to hide the password. If I manage to achieve this (fingers crossed), I will update this post accordingly.
Please share your opinions or any other suggestions by [commenting here](https://github.com/lazydeveloper/lazydeveloper.github.io/discussions/new/
Please share your opinions or any other suggestions by [commenting here](https://github.com/lazydeveloper/lazydeveloper.github.io/discussions/new/)
Expand Down
14 changes: 7 additions & 7 deletions public/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
><url>
<loc>https://shiva.dev/posts/adding_password_protection_to_a_hugo_post/</loc><lastmod>2024-07-07T13:55:20-05:00</lastmod></url><url>
<loc>https://shiva.dev/categories/</loc><lastmod>2024-07-08T16:30:39-04:00</lastmod></url><url>
<loc>https://shiva.dev/categories/development/</loc><lastmod>2024-07-08T16:30:39-04:00</lastmod></url><url>
<loc>https://shiva.dev/tags/hugo/</loc><lastmod>2024-07-08T16:30:39-04:00</lastmod></url><url>
<loc>https://shiva.dev/</loc><lastmod>2024-07-08T16:30:39-04:00</lastmod></url><url>
<loc>https://shiva.dev/tags/</loc><lastmod>2024-07-08T16:30:39-04:00</lastmod></url><url>
<loc>https://shiva.dev/posts/top_secret/</loc><lastmod>2024-07-08T16:30:39-04:00</lastmod></url><url>
<loc>https://shiva.dev/posts/adding_password_protection_to_a_hugo_post/</loc><lastmod>2024-07-09T01:52:13-04:00</lastmod></url><url>
<loc>https://shiva.dev/categories/</loc><lastmod>2024-07-09T01:52:13-04:00</lastmod></url><url>
<loc>https://shiva.dev/categories/development/</loc><lastmod>2024-07-09T01:52:13-04:00</lastmod></url><url>
<loc>https://shiva.dev/tags/hugo/</loc><lastmod>2024-07-09T01:52:13-04:00</lastmod></url><url>
<loc>https://shiva.dev/</loc><lastmod>2024-07-09T01:52:13-04:00</lastmod></url><url>
<loc>https://shiva.dev/tags/</loc><lastmod>2024-07-09T01:52:13-04:00</lastmod></url><url>
<loc>https://shiva.dev/posts/top_secret/</loc><lastmod>2024-07-09T01:38:52-04:00</lastmod></url><url>
<loc>https://shiva.dev/posts/lunr_search_for_hugo/</loc><lastmod>2024-06-23T13:12:21-04:00</lastmod></url><url>
<loc>https://shiva.dev/tags/lunr/</loc><lastmod>2024-06-23T13:12:21-04:00</lastmod></url><url>
<loc>https://shiva.dev/tags/search/</loc><lastmod>2024-06-23T13:12:21-04:00</lastmod></url><url>
Expand Down

0 comments on commit 3a91c35

Please sign in to comment.