feat: Add support for authentication to the sdk, and plug it in the admin (#7349)
* feat: Add support for authentication to the sdk, and plug it in the admin * fix: await fetch before returning in sdk
This commit is contained in:
@@ -1,20 +1,14 @@
|
||||
import { UseMutationOptions, useMutation } from "@tanstack/react-query"
|
||||
|
||||
import { client } from "../../lib/client"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { EmailPassReq } from "../../types/api-payloads"
|
||||
import { EmailPassRes } from "../../types/api-responses"
|
||||
|
||||
export const useEmailPassLogin = (
|
||||
options?: UseMutationOptions<EmailPassRes, Error, EmailPassReq>
|
||||
options?: UseMutationOptions<void, Error, EmailPassReq>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => client.auth.authenticate.emailPass(payload),
|
||||
onSuccess: async (data: { token: string }, variables, context) => {
|
||||
const { token } = data
|
||||
|
||||
// Create a new session with the token
|
||||
await client.auth.login(token)
|
||||
|
||||
mutationFn: (payload) => sdk.auth.login(payload),
|
||||
onSuccess: async (data, variables, context) => {
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
|
||||
@@ -1,48 +1,7 @@
|
||||
import { useMutation } from "@tanstack/react-query"
|
||||
import { adminAuthKeys, useAdminCustomQuery } from "medusa-react"
|
||||
import { medusa } from "../medusa"
|
||||
import { AcceptInviteInput, CreateAuthUserInput } from "./types/auth"
|
||||
|
||||
export const useV2Session = (options: any = {}) => {
|
||||
const { data, isLoading, isError, error } = useAdminCustomQuery(
|
||||
"/admin/users/me",
|
||||
adminAuthKeys.details(),
|
||||
{},
|
||||
options
|
||||
)
|
||||
|
||||
const user = data?.user
|
||||
|
||||
return { user, isLoading, isError, error }
|
||||
}
|
||||
|
||||
export const useV2LoginAndSetSession = () => {
|
||||
return useMutation(
|
||||
(payload: { email: string; password: string }) =>
|
||||
medusa.client.request("POST", "/auth/admin/emailpass", {
|
||||
email: payload.email,
|
||||
password: payload.password,
|
||||
}),
|
||||
{
|
||||
onSuccess: async (args: { token: string }) => {
|
||||
const { token } = args
|
||||
|
||||
// Convert the JWT to a session cookie
|
||||
// TODO: Consider if the JWT is a good choice for session token
|
||||
await medusa.client.request(
|
||||
"POST",
|
||||
"/auth/session",
|
||||
{},
|
||||
{},
|
||||
{
|
||||
Authorization: `Bearer ${token}`,
|
||||
}
|
||||
)
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export const useV2CreateAuthUser = (provider = "emailpass") => {
|
||||
// TODO: Migrate type to work for other providers, e.g. Google
|
||||
return useMutation((args: CreateAuthUserInput) =>
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
import { EmailPassReq } from "../../types/api-payloads"
|
||||
import { EmailPassRes } from "../../types/api-responses"
|
||||
import { postRequest } from "./common"
|
||||
|
||||
async function emailPass(payload: EmailPassReq) {
|
||||
return postRequest<EmailPassRes>("/auth/admin/emailpass", payload)
|
||||
}
|
||||
|
||||
async function login(token: string) {
|
||||
return postRequest<void>("/auth/session", undefined, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const auth = {
|
||||
authenticate: {
|
||||
emailPass,
|
||||
},
|
||||
login,
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import Medusa from "@medusajs/js-sdk"
|
||||
import { apiKeys } from "./api-keys"
|
||||
import { auth } from "./auth"
|
||||
import { campaigns } from "./campaigns"
|
||||
import { categories } from "./categories"
|
||||
import { collections } from "./collections"
|
||||
@@ -28,7 +28,6 @@ import { workflowExecutions } from "./workflow-executions"
|
||||
import { shippingProfiles } from "./shipping-profiles"
|
||||
|
||||
export const client = {
|
||||
auth: auth,
|
||||
apiKeys: apiKeys,
|
||||
campaigns: campaigns,
|
||||
categories: categories,
|
||||
@@ -57,3 +56,10 @@ export const client = {
|
||||
stockLocations: stockLocations,
|
||||
workflowExecutions: workflowExecutions,
|
||||
}
|
||||
|
||||
export const sdk = new Medusa({
|
||||
baseUrl: __BACKEND_URL__ || "http://localhost:9000",
|
||||
auth: {
|
||||
type: "session",
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user