Skip to content

Commit d815e81

Browse files
authored
feat: Update register SDK to not use an options parameter (#15642)
The flag itself was not doing much, and it didn't make sense to keep just for the sake of making the typings happy. Note that this can cause typescript issues, which are easy to resolve by checking on the verification required flag, similar to what we do in the admin dashboard
1 parent a4a1dc9 commit d815e81

6 files changed

Lines changed: 40 additions & 88 deletions

File tree

.changeset/heavy-geese-drop.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@medusajs/js-sdk": minor
3+
"@medusajs/dashboard": patch
4+
---
5+
6+
Update the signature of the sdk register method to either return verification response or a token

packages/admin/dashboard/src/hooks/api/auth.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { AuthLoginResponse, FetchError } from "@medusajs/js-sdk"
1+
import {
2+
AuthLoginResponse,
3+
AuthRegisterResponse,
4+
FetchError,
5+
} from "@medusajs/js-sdk"
26
import { HttpTypes } from "@medusajs/types"
37
import { UseMutationOptions, useMutation } from "@tanstack/react-query"
48
import { sdk } from "../../lib/client"
@@ -26,7 +30,7 @@ export const useSignInWithEmailPass = (
2630

2731
export const useSignUpWithEmailPass = (
2832
options?: UseMutationOptions<
29-
string,
33+
AuthRegisterResponse,
3034
FetchError,
3135
HttpTypes.AdminSignInWithEmailPassword
3236
>

packages/admin/dashboard/src/routes/invite/invite.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,20 @@ const CreateView = ({
197197

198198
const handleSubmit = form.handleSubmit(async (data) => {
199199
try {
200-
const authToken = await signUpEmailPass({
200+
const signupResponse = await signUpEmailPass({
201201
email: data.email,
202202
password: data.password,
203203
})
204204

205+
// This should not happen since email verification is not enabled in the admin, but it should be covered as a scenario.
206+
if (typeof signupResponse !== "string") {
207+
throw new Error(
208+
"Email verification is required, but not supported by this flow."
209+
)
210+
}
211+
212+
const authToken = signupResponse
213+
205214
const invitePayload = {
206215
email: data.email,
207216
first_name: data.first_name,

packages/core/js-sdk/src/__tests__/auth.spec.ts

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -139,35 +139,6 @@ describe("Auth", () => {
139139
expect(storage.setItem).not.toHaveBeenCalled()
140140
})
141141

142-
it("throws from register by default when verification is required", async () => {
143-
server.use(
144-
http.post(
145-
`${baseUrl}/auth/user/emailpass/register`,
146-
async ({ request }) => {
147-
expect(await request.json()).toEqual({
148-
email: "test@example.com",
149-
password: "secret",
150-
})
151-
152-
return HttpResponse.json({
153-
verification_required: true,
154-
verification: verification,
155-
})
156-
}
157-
)
158-
)
159-
160-
const auth = createAuth()
161-
162-
await expect(
163-
auth.register("user", "emailpass", {
164-
email: "test@example.com",
165-
password: "secret",
166-
})
167-
).rejects.toThrow("Unexpected registration response")
168-
expect(storage.setItem).not.toHaveBeenCalled()
169-
})
170-
171142
it("returns a verification requirement from register when opted in", async () => {
172143
server.use(
173144
http.post(
@@ -187,17 +158,10 @@ describe("Auth", () => {
187158
)
188159

189160
const auth = createAuth()
190-
const result = await auth.register(
191-
"user",
192-
"emailpass",
193-
{
194-
email: "test@example.com",
195-
password: "secret",
196-
},
197-
{
198-
returnVerification: true,
199-
}
200-
)
161+
const result = await auth.register("user", "emailpass", {
162+
email: "test@example.com",
163+
password: "secret",
164+
})
201165

202166
expect(result).toEqual({
203167
verification_required: true,

packages/core/js-sdk/src/auth/index.ts

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export type AuthMfaRequiredResponse = {
3131
/**
3232
* Response returned when authentication succeeds but must be completed with
3333
* verification before issuing a token.
34-
*
34+
*
3535
* @since 2.15.5
3636
*/
3737
export type AuthVerificationRequiredResponse = {
@@ -47,24 +47,11 @@ export type AuthVerificationRequiredResponse = {
4747

4848
/**
4949
* Response returned from a registration attempt.
50-
*
50+
*
5151
* @since 2.15.5
5252
*/
5353
export type AuthRegisterResponse = string | AuthVerificationRequiredResponse
5454

55-
/**
56-
* Options used when registering with an auth provider.
57-
*
58-
* @since 2.15.5
59-
*/
60-
export type AuthRegisterOptions = {
61-
/**
62-
* Return verification state instead of throwing when registration
63-
* requires verification before a token can be issued.
64-
*/
65-
returnVerification?: boolean
66-
}
67-
6855
/**
6956
* Response returned from an authentication attempt.
7057
*/
@@ -202,7 +189,7 @@ export type AuthMfaVerifyChallengePayload = {
202189

203190
/**
204191
* Payload used to request a verification token.
205-
*
192+
*
206193
* @since 2.15.5
207194
*/
208195
export type AuthVerificationRequestPayload = {
@@ -218,7 +205,7 @@ export type AuthVerificationRequestPayload = {
218205

219206
/**
220207
* Payload used to confirm a verification token.
221-
*
208+
*
222209
* @since 2.15.5
223210
*/
224211
export type AuthVerificationConfirmPayload = {
@@ -230,7 +217,7 @@ export type AuthVerificationConfirmPayload = {
230217

231218
/**
232219
* Response returned after requesting a verification token.
233-
*
220+
*
234221
* @since 2.15.5
235222
*/
236223
export type AuthVerificationRequestResponse = {
@@ -242,7 +229,7 @@ export type AuthVerificationRequestResponse = {
242229

243230
/**
244231
* Response returned after confirming verification.
245-
*
232+
*
246233
* @since 2.15.5
247234
*/
248235
export type AuthVerificationConfirmResponse = {
@@ -471,7 +458,7 @@ export class Auth {
471458

472459
/**
473460
* Methods for requesting and confirming verification.
474-
*
461+
*
475462
* @since 2.15.5
476463
*/
477464
verification = {
@@ -563,11 +550,10 @@ export class Auth {
563550
* password: "supersecret"
564551
* })
565552
*/
566-
register = (async (
553+
register = async (
567554
actor: string,
568555
method: string,
569-
payload: HttpTypes.AdminSignUpWithEmailPassword | Record<string, unknown>,
570-
options?: AuthRegisterOptions
556+
payload: HttpTypes.AdminSignUpWithEmailPassword | Record<string, unknown>
571557
): Promise<AuthRegisterResponse> => {
572558
const { token, verification_required, verification } =
573559
await this.client.fetch<AuthProviderResponse>(
@@ -579,36 +565,20 @@ export class Auth {
579565
)
580566

581567
if (verification_required && verification) {
582-
if (options?.returnVerification) {
583-
return {
584-
verification_required: true,
585-
verification,
586-
}
568+
return {
569+
verification_required: true,
570+
verification,
587571
}
588-
589-
throw new Error("Unexpected registration response")
590572
}
591573

592574
if (!token) {
593575
throw new Error("Unexpected registration response")
594576
}
595577

578+
// The reason we don't use setToken_ (i.e. start a session) here is because the token doesn't have any actor types attached to it yet.
579+
// The token should be sent in a separate request to create an actor type, after which we can initiate the session.
596580
this.client.setToken(token)
597-
598581
return token
599-
}) as {
600-
(
601-
actor: string,
602-
method: string,
603-
payload: HttpTypes.AdminSignUpWithEmailPassword | Record<string, unknown>,
604-
options: AuthRegisterOptions & { returnVerification: true }
605-
): Promise<AuthRegisterResponse>
606-
(
607-
actor: string,
608-
method: string,
609-
payload: HttpTypes.AdminSignUpWithEmailPassword | Record<string, unknown>,
610-
options?: AuthRegisterOptions
611-
): Promise<string>
612582
}
613583

614584
/**

packages/core/js-sdk/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export {
5252
type AuthMfaVerifyChallengePayload,
5353
type AuthMfaVerifyPayload,
5454
type AuthRedirectResponse,
55-
type AuthRegisterOptions,
5655
type AuthRegisterResponse,
5756
} from "./auth/index.js"
5857
export { Store } from "./store/index.js"

0 commit comments

Comments
 (0)