Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usable with App router? #18

Open
SadmanYasar opened this issue Jul 14, 2024 · 0 comments
Open

Usable with App router? #18

SadmanYasar opened this issue Jul 14, 2024 · 0 comments

Comments

@SadmanYasar
Copy link

SadmanYasar commented Jul 14, 2024

Hi. This seems like the ideal solution for adding directus to refine but I can't seem to figure out if this will work with app router. By default, the login page is generated with server side like this:

import { AuthPage } from "@components/auth-page";
import { authProviderServer, authProvider } from "@providers/auth-provider";
import { redirect } from "next/navigation";

export default async function Login() {
  const data = await getData();

  if (data.authenticated) {
    redirect(data?.redirectTo || "/");
  }

  return <AuthPage type="login" />;
}

async function getData() {
  const { authenticated, redirectTo, error } = await authProviderServer.check();

  return {
    authenticated,
    redirectTo,
    error,
  };
}

Should this be made client side by using useEffect hook and checking for access_token from sdk or use a Cookies strategy in the sdk for authentication and keep it server side? I was thinking of something like this:

//auth-provider.tsx
export const authProvider: AuthProvider = {
  login: async ({ email, password }) => {
    const { access_token, expires } = await directusAuthHelper.login(email, password);
    if (access_token) {
      Cookies.set("auth", JSON.stringify(access_token), {
        expires: expires,
        path: "/",
      });
      return {
        success: true,
        redirectTo: "/",
      };
    }
//auth-provider.server.ts
export const authProviderServer: Pick<AuthProvider, "check"> = {
  check: async () => {
    const cookieStore = cookies();
    const auth = cookieStore.get("auth");
    if (auth) {
      // Optionally verify the token's validity with Directus here
      return {
        authenticated: true,
      };
    }

    return {
      authenticated: false,
      logout: true,
      redirectTo: "/login",
    };
  },
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant