fix: Update js-sdk with token (#10211)

This commit is contained in:
Oli Juhl
2024-11-22 09:32:48 +01:00
committed by GitHub
parent 44265a928d
commit 1efe4e9e33
3 changed files with 41 additions and 37 deletions
@@ -58,11 +58,12 @@ export const useLogout = (options?: UseMutationOptions<void, FetchError>) => {
} }
export const useUpdateProviderForEmailPass = ( export const useUpdateProviderForEmailPass = (
token: string,
options?: UseMutationOptions<void, FetchError, { password: string }> options?: UseMutationOptions<void, FetchError, { password: string }>
) => { ) => {
return useMutation({ return useMutation({
mutationFn: (payload) => mutationFn: (payload) =>
sdk.auth.updateProvider("user", "emailpass", payload), sdk.auth.updateProvider("user", "emailpass", payload, token),
onSuccess: async (data, variables, context) => { onSuccess: async (data, variables, context) => {
options?.onSuccess?.(data, variables, context) options?.onSuccess?.(data, variables, context)
}, },
@@ -114,7 +114,7 @@ const ChooseNewPassword = ({ token }: { token: string }) => {
}, },
}) })
const { mutateAsync, isPending } = useUpdateProviderForEmailPass() const { mutateAsync, isPending } = useUpdateProviderForEmailPass(token)
const handleSubmit = form.handleSubmit(async ({ password }) => { const handleSubmit = form.handleSubmit(async ({ password }) => {
if (!invite) { if (!invite) {
@@ -123,7 +123,6 @@ const ChooseNewPassword = ({ token }: { token: string }) => {
await mutateAsync( await mutateAsync(
{ {
email: invite.entity_id,
password, password,
}, },
{ {
+38 -34
View File
@@ -14,13 +14,13 @@ export class Auth {
/** /**
* This method is used to retrieve a registration JWT token for a user, customer, or custom actor type. It sends a request to the * This method is used to retrieve a registration JWT token for a user, customer, or custom actor type. It sends a request to the
* [Retrieve Registration Token API route](https://docs.medusajs.com/api/store#auth_postactor_typeauth_provider_register). * [Retrieve Registration Token API route](https://docs.medusajs.com/api/store#auth_postactor_typeauth_provider_register).
* *
* @param actor - The actor type. For example, `user` for admin user, or `customer` for customer. * @param actor - The actor type. For example, `user` for admin user, or `customer` for customer.
* @param method - The authentication provider to use. For example, `emailpass` or `google`. * @param method - The authentication provider to use. For example, `emailpass` or `google`.
* @param payload - The data to pass in the request's body for authentication. When using the `emailpass` provider, * @param payload - The data to pass in the request's body for authentication. When using the `emailpass` provider,
* you pass the email and password. * you pass the email and password.
* @returns The JWT token used for registration later. * @returns The JWT token used for registration later.
* *
* @example * @example
* sdk.auth.register( * sdk.auth.register(
* "customer", * "customer",
@@ -54,19 +54,19 @@ export class Auth {
/** /**
* This method retrieves the JWT authenticated token for an admin user, customer, or custom * This method retrieves the JWT authenticated token for an admin user, customer, or custom
* actor type. It sends a request to the [Authenticate API Route](https://docs.medusajs.com/api/admin#auth_postactor_typeauth_provider). * actor type. It sends a request to the [Authenticate API Route](https://docs.medusajs.com/api/admin#auth_postactor_typeauth_provider).
* *
* If the `auth.type` of the SDK is set to `session`, this method will also send a request to the * If the `auth.type` of the SDK is set to `session`, this method will also send a request to the
* [Set Authentication Session API route](https://docs.medusajs.com/api/admin#auth_postsession). * [Set Authentication Session API route](https://docs.medusajs.com/api/admin#auth_postsession).
* *
* Subsequent requests using the SDK will automatically have the necessary authentication headers / session * Subsequent requests using the SDK will automatically have the necessary authentication headers / session
* set. * set.
* *
* @param actor - The actor type. For example, `user` for admin user, or `customer` for customer. * @param actor - The actor type. For example, `user` for admin user, or `customer` for customer.
* @param method - The authentication provider to use. For example, `emailpass` or `google`. * @param method - The authentication provider to use. For example, `emailpass` or `google`.
* @param payload - The data to pass in the request's body for authentication. When using the `emailpass` provider, * @param payload - The data to pass in the request's body for authentication. When using the `emailpass` provider,
* you pass the email and password. * you pass the email and password.
* @returns The authentication JWT token * @returns The authentication JWT token
* *
* @example * @example
* sdk.auth.login( * sdk.auth.login(
* "customer", * "customer",
@@ -106,12 +106,12 @@ export class Auth {
/** /**
* This method is used to validate an Oauth callback from a third-party service, such as Google, for an admin user, customer, or custom actor types. * This method is used to validate an Oauth callback from a third-party service, such as Google, for an admin user, customer, or custom actor types.
* It sends a request to the [Validate Authentication Callback](https://docs.medusajs.com/api/admin#auth_postactor_typeauth_providercallback). * It sends a request to the [Validate Authentication Callback](https://docs.medusajs.com/api/admin#auth_postactor_typeauth_providercallback).
* *
* @param actor - The actor type. For example, `user` for admin user, or `customer` for customer. * @param actor - The actor type. For example, `user` for admin user, or `customer` for customer.
* @param method - The authentication provider to use. For example, `google`. * @param method - The authentication provider to use. For example, `google`.
* @param query - The query parameters from the Oauth callback, which should be passed to the API route. * @param query - The query parameters from the Oauth callback, which should be passed to the API route.
* @returns The authentication JWT token * @returns The authentication JWT token
* *
* @example * @example
* sdk.auth.callback( * sdk.auth.callback(
* "customer", * "customer",
@@ -122,10 +122,10 @@ export class Auth {
* ).then((token) => { * ).then((token) => {
* console.log(token) * console.log(token)
* }) * })
* *
* *
* @privateRemarks * @privateRemarks
* The callback expects all query parameters from the Oauth callback to be passed to * The callback expects all query parameters from the Oauth callback to be passed to
* the backend, and the provider is in charge of parsing and validating them * the backend, and the provider is in charge of parsing and validating them
*/ */
callback = async ( callback = async (
@@ -148,9 +148,9 @@ export class Auth {
/** /**
* This method refreshes a JWT authentication token, which is useful after validating the Oauth callback * This method refreshes a JWT authentication token, which is useful after validating the Oauth callback
* with {@link callback}. It sends a request to the [Refresh Authentication Token API route](https://docs.medusajs.com/api/admin#auth_postadminauthtokenrefresh). * with {@link callback}. It sends a request to the [Refresh Authentication Token API route](https://docs.medusajs.com/api/admin#auth_postadminauthtokenrefresh).
* *
* @returns The refreshed JWT authentication token. * @returns The refreshed JWT authentication token.
* *
* @example * @example
* sdk.auth.refresh() * sdk.auth.refresh()
* .then((token) => { * .then((token) => {
@@ -174,7 +174,7 @@ export class Auth {
/** /**
* This method deletes the authentication session of the currently logged-in user to log them out. * This method deletes the authentication session of the currently logged-in user to log them out.
* It sends a request to the [Delete Authentication Session API route](https://docs.medusajs.com/api/admin#auth_deletesession). * It sends a request to the [Delete Authentication Session API route](https://docs.medusajs.com/api/admin#auth_deletesession).
* *
* @example * @example
* sdk.auth.logout() * sdk.auth.logout()
* .then(() => { * .then(() => {
@@ -194,15 +194,15 @@ export class Auth {
/** /**
* This method requests a reset password token for an admin user, customer, or custom actor type. * This method requests a reset password token for an admin user, customer, or custom actor type.
* It sends a request to the [Generate Reset Password Token API route](https://docs.medusajs.com/api/admin#auth_postactor_typeauth_providerresetpassword). * It sends a request to the [Generate Reset Password Token API route](https://docs.medusajs.com/api/admin#auth_postactor_typeauth_providerresetpassword).
* *
* To reset the password later using the token delivered to the user, use the {@link updateProvider} method. * To reset the password later using the token delivered to the user, use the {@link updateProvider} method.
* *
* Related guide: [How to allow customers to reset their passwords in a storefront](https://docs.medusajs.com/resources/storefront-development/customers/reset-password). * Related guide: [How to allow customers to reset their passwords in a storefront](https://docs.medusajs.com/resources/storefront-development/customers/reset-password).
* *
* @param actor - The actor type. For example, `user` for admin user, or `customer` for customer. * @param actor - The actor type. For example, `user` for admin user, or `customer` for customer.
* @param provider - The authentication provider to use. For example, `emailpass`. * @param provider - The authentication provider to use. For example, `emailpass`.
* @param body - The data required to identify the user. * @param body - The data required to identify the user.
* *
* @example * @example
* sdk.auth.resetPassword( * sdk.auth.resetPassword(
* "customer", * "customer",
@@ -222,7 +222,7 @@ export class Auth {
/** /**
* The user's identifier. For example, when using the `emailpass` provider, * The user's identifier. For example, when using the `emailpass` provider,
* this would be the user's email. * this would be the user's email.
*/ */
identifier: string identifier: string
} }
) => { ) => {
@@ -235,27 +235,27 @@ export class Auth {
/** /**
* This method is used to update user-related data authentication data. * This method is used to update user-related data authentication data.
* *
* More specifically, use this method when updating the password of an admin user, customer, or * More specifically, use this method when updating the password of an admin user, customer, or
* custom actor type after requesting to reset their password with {@link resetPassword}. * custom actor type after requesting to reset their password with {@link resetPassword}.
* *
* This method sends a request to [this API route](https://docs.medusajs.com/api/admin#auth_postactor_typeauth_providerupdate). * This method sends a request to [this API route](https://docs.medusajs.com/api/admin#auth_postactor_typeauth_providerupdate).
* *
* Related guide: [How to allow customers to reset their passwords in a storefront](https://docs.medusajs.com/resources/storefront-development/customers/reset-password). * Related guide: [How to allow customers to reset their passwords in a storefront](https://docs.medusajs.com/resources/storefront-development/customers/reset-password).
* *
* @param actor - The actor type. For example, `user` for admin user, or `customer` for customer. * @param actor - The actor type. For example, `user` for admin user, or `customer` for customer.
* @param provider - The authentication provider to use. For example, `emailpass`. * @param provider - The authentication provider to use. For example, `emailpass`.
* @param body - The data necessary to update the user's authentication data. When resetting the user's password, * @param body - The data necessary to update the user's authentication data. When resetting the user's password,
* send the `email` and `password` properties. * send the `password` property.
* *
* @example * @example
* sdk.auth.updateProvider( * sdk.auth.updateProvider(
* "customer", * "customer",
* "emailpass", * "emailpass",
* { * {
* email: "customer@gmail.com",
* password: "supersecret" * password: "supersecret"
* } * },
* token
* ) * )
* .then(() => { * .then(() => {
* // password updated * // password updated
@@ -264,12 +264,16 @@ export class Auth {
updateProvider = async ( updateProvider = async (
actor: string, actor: string,
provider: string, provider: string,
body: Record<string, unknown> body: Record<string, unknown>,
token: string
) => { ) => {
await this.client.fetch(`/auth/${actor}/${provider}/update`, { await this.client.fetch(
method: "POST", `/auth/${actor}/${provider}/update?token=${token}`,
body, {
}) method: "POST",
body,
}
)
} }
/** /**