Skip to content

Commit d5957e8

Browse files
shahednasserBalu-Varanasi
authored andcommitted
docs: fix callback validation for third-party authentication (medusajs#14109)
* docs: fix callback validation for third-party authentication * address comment
1 parent ad7e2aa commit d5957e8

File tree

11 files changed

+272
-166
lines changed

11 files changed

+272
-166
lines changed
Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Medusa from "@medusajs/js-sdk"
2+
import { decodeToken } from "react-jwt"
23

34
export const sdk = new Medusa({
45
baseUrl: import.meta.env.VITE_BACKEND_URL || "/",
@@ -8,24 +9,33 @@ export const sdk = new Medusa({
89
},
910
})
1011

11-
await sdk.auth.callback(
12+
const token = await sdk.auth.callback(
1213
"user",
1314
"google",
1415
{
1516
code: "123",
1617
state: "456"
1718
}
1819
)
19-
2020
// all subsequent requests will use the token in the header
21-
sdk.admin.invite.accept(
22-
{
23-
24-
first_name: "John",
25-
last_name: "Smith",
26-
invite_token: "12345..."
27-
},
28-
)
29-
.then(({ user }) => {
30-
console.log(user)
31-
})
21+
22+
const decodedToken = decodeToken(token) as { actor_id: string, user_metadata: Record<string, unknown> }
23+
24+
const shouldCreateUser = decodedToken.actor_id === ""
25+
26+
if (shouldCreateUser) {
27+
const user = await sdk.admin.invite.accept(
28+
{
29+
email: decodedToken.user_metadata.email as string,
30+
first_name: "John",
31+
last_name: "Smith",
32+
invite_token: "12345..."
33+
},
34+
)
35+
36+
// refresh auth token
37+
await sdk.auth.refresh()
38+
// all subsequent requests will use the new token in the header
39+
} else {
40+
// User already exists and is authenticated
41+
}
Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Medusa from "@medusajs/js-sdk"
2+
import { decodeToken } from "react-jwt"
23

34
export const sdk = new Medusa({
45
baseUrl: import.meta.env.VITE_BACKEND_URL || "/",
@@ -8,24 +9,33 @@ export const sdk = new Medusa({
89
},
910
})
1011

11-
const authToken = await sdk.auth.callback(
12+
const token = await sdk.auth.callback(
1213
"user",
13-
"google",
14+
"github",
1415
{
1516
code: "123",
1617
state: "456"
1718
}
1819
)
19-
2020
// all subsequent requests will use the token in the header
21-
sdk.admin.invite.accept(
22-
{
23-
24-
first_name: "John",
25-
last_name: "Smith",
26-
invite_token: "12345..."
27-
},
28-
)
29-
.then(({ user }) => {
30-
console.log(user)
31-
})
21+
22+
const decodedToken = decodeToken(token) as { actor_id: string, user_metadata: Record<string, unknown> }
23+
24+
const shouldCreateUser = decodedToken.actor_id === ""
25+
26+
if (shouldCreateUser) {
27+
const user = await sdk.admin.invite.accept(
28+
{
29+
email: decodedToken.user_metadata.email as string,
30+
first_name: "John",
31+
last_name: "Smith",
32+
invite_token: "12345..."
33+
},
34+
)
35+
36+
// refresh auth token
37+
await sdk.auth.refresh()
38+
// all subsequent requests will use the new token in the header
39+
} else {
40+
// User already exists and is authenticated
41+
}

www/apps/api-reference/specs/admin/openapi.full.yaml

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64080,6 +64080,7 @@ paths:
6408064080
label: Google Provider
6408164081
source: |-
6408264082
import Medusa from "@medusajs/js-sdk"
64083+
import { decodeToken } from "react-jwt"
6408364084

6408464085
export const sdk = new Medusa({
6408564086
baseUrl: import.meta.env.VITE_BACKEND_URL || "/",
@@ -64089,31 +64090,41 @@ paths:
6408964090
},
6409064091
})
6409164092

64092-
await sdk.auth.callback(
64093+
const token = await sdk.auth.callback(
6409364094
"user",
6409464095
"google",
6409564096
{
6409664097
code: "123",
6409764098
state: "456"
6409864099
}
6409964100
)
64100-
6410164101
// all subsequent requests will use the token in the header
64102-
sdk.admin.invite.accept(
64103-
{
64104-
64105-
first_name: "John",
64106-
last_name: "Smith",
64107-
invite_token: "12345..."
64108-
},
64109-
)
64110-
.then(({ user }) => {
64111-
console.log(user)
64112-
})
64102+
64103+
const decodedToken = decodeToken(token) as { actor_id: string, user_metadata: Record<string, unknown> }
64104+
64105+
const shouldCreateUser = decodedToken.actor_id === ""
64106+
64107+
if (shouldCreateUser) {
64108+
const user = await sdk.admin.invite.accept(
64109+
{
64110+
email: decodedToken.user_metadata.email as string,
64111+
first_name: "John",
64112+
last_name: "Smith",
64113+
invite_token: "12345..."
64114+
},
64115+
)
64116+
64117+
// refresh auth token
64118+
await sdk.auth.refresh()
64119+
// all subsequent requests will use the new token in the header
64120+
} else {
64121+
// User already exists and is authenticated
64122+
}
6411364123
- lang: TypeScript
6411464124
label: GitHub Provider
6411564125
source: |-
6411664126
import Medusa from "@medusajs/js-sdk"
64127+
import { decodeToken } from "react-jwt"
6411764128

6411864129
export const sdk = new Medusa({
6411964130
baseUrl: import.meta.env.VITE_BACKEND_URL || "/",
@@ -64123,27 +64134,36 @@ paths:
6412364134
},
6412464135
})
6412564136

64126-
const authToken = await sdk.auth.callback(
64137+
const token = await sdk.auth.callback(
6412764138
"user",
64128-
"google",
64139+
"github",
6412964140
{
6413064141
code: "123",
6413164142
state: "456"
6413264143
}
6413364144
)
64134-
6413564145
// all subsequent requests will use the token in the header
64136-
sdk.admin.invite.accept(
64137-
{
64138-
64139-
first_name: "John",
64140-
last_name: "Smith",
64141-
invite_token: "12345..."
64142-
},
64143-
)
64144-
.then(({ user }) => {
64145-
console.log(user)
64146-
})
64146+
64147+
const decodedToken = decodeToken(token) as { actor_id: string, user_metadata: Record<string, unknown> }
64148+
64149+
const shouldCreateUser = decodedToken.actor_id === ""
64150+
64151+
if (shouldCreateUser) {
64152+
const user = await sdk.admin.invite.accept(
64153+
{
64154+
email: decodedToken.user_metadata.email as string,
64155+
first_name: "John",
64156+
last_name: "Smith",
64157+
invite_token: "12345..."
64158+
},
64159+
)
64160+
64161+
// refresh auth token
64162+
await sdk.auth.refresh()
64163+
// all subsequent requests will use the new token in the header
64164+
} else {
64165+
// User already exists and is authenticated
64166+
}
6414764167
tags:
6414864168
- Auth
6414964169
responses:
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Medusa from "@medusajs/js-sdk"
2+
import { decodeToken } from "react-jwt"
23

34
let MEDUSA_BACKEND_URL = "http://localhost:9000"
45

@@ -12,17 +13,28 @@ export const sdk = new Medusa({
1213
publishableKey: process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY,
1314
})
1415

15-
await sdk.auth.callback(
16+
const token = await sdk.auth.callback(
1617
"customer",
1718
"google",
1819
{
1920
code: "123",
2021
state: "456"
2122
}
2223
)
23-
2424
// all subsequent requests will use the token in the header
25-
const { customer } = await sdk.store.customer.create({
26-
27-
password: "supersecret"
28-
})
25+
26+
const decodedToken = decodeToken(token) as { actor_id: string, user_metadata: Record<string, unknown> }
27+
28+
const shouldCreateCustomer = decodedToken.actor_id === ""
29+
30+
if (shouldCreateCustomer) {
31+
const { customer } = await sdk.store.customer.create({
32+
email: decodedToken.user_metadata.email as string,
33+
})
34+
35+
// refresh auth token
36+
await sdk.auth.refresh()
37+
// all subsequent requests will use the new token in the header
38+
} else {
39+
// Customer already exists and is authenticated
40+
}
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Medusa from "@medusajs/js-sdk"
2+
import { decodeToken } from "react-jwt"
23

34
let MEDUSA_BACKEND_URL = "http://localhost:9000"
45

@@ -12,17 +13,28 @@ export const sdk = new Medusa({
1213
publishableKey: process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY,
1314
})
1415

15-
await sdk.auth.callback(
16+
const token = await sdk.auth.callback(
1617
"customer",
1718
"github",
1819
{
1920
code: "123",
2021
state: "456"
2122
}
2223
)
23-
2424
// all subsequent requests will use the token in the header
25-
const { customer } = await sdk.store.customer.create({
26-
27-
password: "supersecret"
28-
})
25+
26+
const decodedToken = decodeToken(token) as { actor_id: string, user_metadata: Record<string, unknown> }
27+
28+
const shouldCreateCustomer = decodedToken.actor_id === ""
29+
30+
if (shouldCreateCustomer) {
31+
const { customer } = await sdk.store.customer.create({
32+
email: decodedToken.user_metadata.email as string,
33+
})
34+
35+
// refresh auth token
36+
await sdk.auth.refresh()
37+
// all subsequent requests will use the new token in the header
38+
} else {
39+
// Customer already exists and is authenticated
40+
}

www/apps/api-reference/specs/store/openapi.full.yaml

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ paths:
290290
label: Google Provider
291291
source: |-
292292
import Medusa from "@medusajs/js-sdk"
293+
import { decodeToken } from "react-jwt"
293294

294295
let MEDUSA_BACKEND_URL = "http://localhost:9000"
295296

@@ -303,24 +304,36 @@ paths:
303304
publishableKey: process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY,
304305
})
305306

306-
await sdk.auth.callback(
307+
const token = await sdk.auth.callback(
307308
"customer",
308309
"google",
309310
{
310311
code: "123",
311312
state: "456"
312313
}
313314
)
314-
315315
// all subsequent requests will use the token in the header
316-
const { customer } = await sdk.store.customer.create({
317-
318-
password: "supersecret"
319-
})
316+
317+
const decodedToken = decodeToken(token) as { actor_id: string, user_metadata: Record<string, unknown> }
318+
319+
const shouldCreateCustomer = decodedToken.actor_id === ""
320+
321+
if (shouldCreateCustomer) {
322+
const { customer } = await sdk.store.customer.create({
323+
email: decodedToken.user_metadata.email as string,
324+
})
325+
326+
// refresh auth token
327+
await sdk.auth.refresh()
328+
// all subsequent requests will use the new token in the header
329+
} else {
330+
// Customer already exists and is authenticated
331+
}
320332
- lang: TypeScript
321333
label: GitHub Provider
322334
source: |-
323335
import Medusa from "@medusajs/js-sdk"
336+
import { decodeToken } from "react-jwt"
324337

325338
let MEDUSA_BACKEND_URL = "http://localhost:9000"
326339

@@ -334,20 +347,31 @@ paths:
334347
publishableKey: process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY,
335348
})
336349

337-
await sdk.auth.callback(
350+
const token = await sdk.auth.callback(
338351
"customer",
339352
"github",
340353
{
341354
code: "123",
342355
state: "456"
343356
}
344357
)
345-
346358
// all subsequent requests will use the token in the header
347-
const { customer } = await sdk.store.customer.create({
348-
349-
password: "supersecret"
350-
})
359+
360+
const decodedToken = decodeToken(token) as { actor_id: string, user_metadata: Record<string, unknown> }
361+
362+
const shouldCreateCustomer = decodedToken.actor_id === ""
363+
364+
if (shouldCreateCustomer) {
365+
const { customer } = await sdk.store.customer.create({
366+
email: decodedToken.user_metadata.email as string,
367+
})
368+
369+
// refresh auth token
370+
await sdk.auth.refresh()
371+
// all subsequent requests will use the new token in the header
372+
} else {
373+
// Customer already exists and is authenticated
374+
}
351375
tags:
352376
- Auth
353377
responses:

0 commit comments

Comments
 (0)