Skip to content

Commit

Permalink
fix: unused strapi token
Browse files Browse the repository at this point in the history
  • Loading branch information
matmut7 committed Jun 7, 2024
1 parent d82ff37 commit bce74f9
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 35 deletions.
27 changes: 0 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,36 +71,9 @@ touch .env
3. Paste in the following.

```yaml
NEXT_PUBLIC_STRAPI_API_TOKEN=your-api-token
NEXT_PUBLIC_STRAPI_API_URL=http://127.0.0.1:1337
```

4. Before starting our Next JS app we need to go inside our Strapi Admin and create a token that we will be using for displaying our **content**.

Inside your Strapi Admin Panel navigate to `Settings` -> `API Tokens` and click on the `Create new API Token`.

![api-tokens](https://user-images.githubusercontent.com/6153188/231865572-cebc5538-374c-4050-91cd-c303fae25a3d.png)

Here are our Token Settings

Name: Public API Token Content
Description: Access to public content.
Token duration: Unlimited
Token type: Custom

In Permissions let's give the following access.

| Content | Permissions |
| -------- | :--------------: |
| Article | find and findOne |
| Author | find and findOne |
| Category | find and findOne |
| Page | find and findOne |

![permissions](https://user-images.githubusercontent.com/6153188/231865625-a3634d89-0f40-4a6d-a356-8f654abd88b9.gif)

Once you have your token add it to your `NEXT_PUBLIC_STRAPI_API_TOKEN` variable name in the `.env` file.

5. Start your frontend

```bash
Expand Down
3 changes: 1 addition & 2 deletions next-app/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
NEXT_PUBLIC_STRAPI_API_TOKEN=your-api-token
NEXT_PUBLIC_STRAPI_API_URL=http://127.0.0.1:1337
NEXT_PUBLIC_STRAPI_API_URL=http://127.0.0.1:1337
3 changes: 1 addition & 2 deletions next-app/app/blog/[blog]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ export async function generateMetadata({
}: {
params: { blog: string }
}): Promise<Metadata> {
const token = process.env.NEXT_PUBLIC_STRAPI_API_TOKEN;
const path = `/articles`;
const urlParamsObject = {
filters: { slug: params.blog },
};
const options = { headers: { Authorization: `Bearer ${token}` } };
const options = {};
const post = await fetchAPI(path, urlParamsObject, options);
const postAttributes = post.data[0].attributes;
return {
Expand Down
3 changes: 1 addition & 2 deletions next-app/app/blog/[blog]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export default async function Blog({ params }) {
}

async function getBlogPostBySlug(slug: string) {
const token = process.env.NEXT_PUBLIC_STRAPI_API_TOKEN;
const path = `/articles`;
const urlParamsObject = {
filters: { slug },
Expand All @@ -60,7 +59,7 @@ async function getBlogPostBySlug(slug: string) {
cover: { fields: ["url", "alternativeText", "caption"] },
},
};
const options = { headers: { Authorization: `Bearer ${token}` } };
const options = {};
const response = await fetchAPI(path, urlParamsObject, options);
const post = response.data[0];
const data = post.attributes;
Expand Down
3 changes: 1 addition & 2 deletions next-app/app/blog/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export default async function Blog() {

async function getBlogPosts() {
try {
const token = process.env.NEXT_PUBLIC_STRAPI_API_TOKEN;
const path = `/articles`;
const urlParamsObject = {
populate: {
Expand All @@ -74,7 +73,7 @@ async function getBlogPosts() {
},
sort: { PublishAt: "desc" },
};
const options = { headers: { Authorization: `Bearer ${token}` } };
const options = {};
const responseData = await fetchAPI(path, urlParamsObject, options);
return responseData.data;
} catch (error) {
Expand Down

0 comments on commit bce74f9

Please sign in to comment.