Skip to content

Nextjs 14 documentation issue #124

Open
@thedarkknight197

Description

@thedarkknight197

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:

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:

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:

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 (
        <>
            <p className={"text-white"}>{JSON.stringify(me)}</p>
        </>
    );
}

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

it tell me:

{
  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'
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions