directus / directus/docs

Nextjs 14 documentation inaccuracy issue

Open
#124 7 comments 0 reactions 0 assignees View on GitHub
Docs
Dominant language
TypeScript
Stars
22
Forks
82
Avg merge
18h 49m
Merged PRs (30d)
4

Description

### Page

https://docs.directus.io/blog/implement-directus-auth-in-next-js-14.html#before-you-start

### Describe the Inaccuracy

It doesn't work. I think is wrong the procedure to login. how directus know witch cookie need to be used to authenticate requests?

Directus client:
```ts
import { authentication, createDirectus, rest } from "@directus/sdk";

const client: any = createDirectus(process.env.API_URL || process.env.NEXT_PUBLIC_API_URL!)
.with(authentication("cookie", {credentials: "include", autoRefresh: true}))
.with(rest({credentials: "include"}));

export { client };
```

login:
```ts
export const directusLogin = async (credentials: {email: string, password: string}) => {
const res = await fetch(`${process.env.API_URL}/auth/login`, {
method: "POST",
body: JSON.stringify(credentials),
headers: { "Content-Type": "application/json" },
});
const user = await res.json();
// If no error and we have user data, return it
if (!user) {
throw new Error("Wrong credentials!");
}
const formatedData = JSON.stringify(user)
cookies().set(COOKIE_NAME, formatedData)
return user;
};
```

My profile page:
```ts
import {userSession} from "@/lib/actions/auth/auth";
import {client} from "@/lib/utils";
import {readMe} from "@directus/sdk";

export default async function ProfilePage(){
const user = await userSession();

const me = client.request(readMe());

return (
<>

{JSON.stringify(me)}



);
}
```

In user is stored correctly the token, but the directus calls are not authenticated. The email and password are correct

it tell me:
```ts
{
errors: [ { message: 'Invalid user credentials.', extensions: [Object] } ],
response: Response {
status: 401,
statusText: 'Unauthorized',
headers: Headers {
'content-security-policy': "script-src 'self' 'unsafe-eval';worker-src 'self' blob:;child-src 'self' blob:;img-src 'self' data: blob: https://raw.githubusercontent.com https://avatars.githubusercontent.com;media-src 'self';connect-src 'self' https://*;default-src 'self';base-uri 'self';font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';object-src 'none';script-src-attr 'none';style-src 'self' https: 'unsafe-inline'",
'x-powered-by': 'Directus',
vary: 'Origin',
'access-control-allow-credentials': 'true',
'access-control-expose-headers': 'Content-Range',
'content-type': 'application/json; charset=utf-8',
'content-length': '96',
etag: 'W/"60-SpvBqFAbsdy4SkXwsevzfPClFZA"',
date: 'Mon, 26 Aug 2024 11:02:49 GMT',
connection: 'keep-alive',
'keep-alive': 'timeout=5'
},
body: ReadableStream { locked: true, state: 'closed', supportsBYOB: true },
bodyUsed: true,
ok: false,
redirected: false,
type: 'basic',
url: 'http://0.0.0.0:8055/users/me'
}
}

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.