Skip to content

Commit

Permalink
fix: Return login redirect URLas a 200 response
Browse files Browse the repository at this point in the history
  • Loading branch information
sradevski committed Sep 4, 2024
1 parent 93cb39e commit b69f1ec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
29 changes: 18 additions & 11 deletions packages/core/js-sdk/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,24 @@ export class Auth {
}

login = async (
actor: "customer" | "user",
method: "emailpass",
payload: HttpTypes.AdminSignInWithEmailPassword
actor: string,
method: string,
payload: HttpTypes.AdminSignInWithEmailPassword | Record<string, unknown>
) => {
const { token } = await this.client.fetch<{ token: string }>(
`/auth/${actor}/${method}`,
{
method: "POST",
body: payload,
}
)
// There will either be token or location returned from the backend.
const { token, location } = await this.client.fetch<{
token?: string
location?: string
}>(`/auth/${actor}/${method}`, {
method: "POST",
body: payload,
})

// In the case of an oauth login, we return the redirect location to the caller.
// They can decide if they do an immediate redirect or put it in an <a> tag.
if (location) {
return { location }
}

// By default we just set the token in memory, if configured to use sessions we convert it into session storage instead.
if (this.config?.auth?.type === "session") {
Expand All @@ -49,7 +56,7 @@ export class Auth {
headers: { Authorization: `Bearer ${token}` },
})
} else {
this.client.setToken(token)
this.client.setToken(token as string)
}

return token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
)

if (location) {
res.redirect(location)
return
return res.status(200).json({ location })
}

if (success) {
Expand Down

0 comments on commit b69f1ec

Please sign in to comment.