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

[BREAKING CHANGE] Switch to context based client #13

Closed
TheEdoRan opened this issue Jul 7, 2023 · 2 comments · Fixed by #14
Closed

[BREAKING CHANGE] Switch to context based client #13

TheEdoRan opened this issue Jul 7, 2023 · 2 comments · Fixed by #14
Labels
breaking change A change that breaks current code enhancement New feature or request released

Comments

@TheEdoRan
Copy link
Owner

TheEdoRan commented Jul 7, 2023

The current way to instantiate a new client, that supports authenticated actions is:

import { createSafeActionClient } from "next-safe-action";

export const action = createSafeActionClient({
  getAuthData: async () => {
    // return auth object
  },
});

Then, a new action that needs auth info is defined like this:

"use server";

import { z } from "zod";
import { action } from "@/lib/safe-action";

export const exampleAction = action({ input, withAuth: true },
  async (parsedInput, { userId }) => {
    console.log(userId);
    ...
  }
);

This implementation is fine for authentication/authorization purposes, but it's extremely specific too.
There's a better, easier, and generic way to implement auth actions and anything else a user needs: context.

⚠️ BREAKING CHANGE: Context based client

Using context, you can define multiple clients for different purposes. Here are two examples with a base client, and an auth one:

import { createSafeActionClient } from "next-safe-action";

export const action = createSafeActionClient();
export const authAction = createSafeActionClient({
  buildContext: async () => {
    // return your context object, for example:
    return {
      userId: "123";
    }
  },
});

Then, a new action that needs auth info would be defined like this:

"use server";

import { z } from "zod";
import { authAction } from "@/lib/safe-action"; // import `authAction` client

// We don't need to pass an object with `input` and `withAuth` keys as first argument
// to the client here, because we know by the client name (`authAction`), that
// the second argument of function that defines server action's code will be { `userId` }.
export const exampleAction = authAction(input, async (parsedInput, { userId }) => {
    console.log(userId);
    ...
  }
);

With context based clients, the second argument of server action function (in this case { userId }), will always be the context object. If you don't define a buildContext function while instantiating a safe action client, then context would be an empty object.

The idea is to include these changes in version 3.0.0 of the library.

@TheEdoRan TheEdoRan added the enhancement New feature or request label Jul 7, 2023
TheEdoRan added a commit that referenced this issue Jul 8, 2023
BREAKING CHANGE: `getAuthData` removed from `createOpts`; `withAuth: true` removed from action opts;
use context based clients

re #13
@TheEdoRan
Copy link
Owner Author

TheEdoRan commented Jul 8, 2023

You can find and experiment with these new changes with beta version.

@TheEdoRan TheEdoRan added the breaking change A change that breaks current code label Jul 8, 2023
@github-actions
Copy link

🎉 This issue has been resolved in version 3.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change A change that breaks current code enhancement New feature or request released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant