Skip to content

Commit

Permalink
feat: Emailpass invite flow
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Mar 27, 2024
1 parent 0b23e7e commit 8592426
Show file tree
Hide file tree
Showing 13 changed files with 448 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@
"invalidInvite": "The invite is invalid or has expired.",
"successTitle": "Your account has been created",
"successHint": "Get started with Medusa Admin right away.",
"successAction": "Start using Medusa",
"successAction": "Sign in to start using Medusa",
"invalidTokenTitle": "Your invite token is invalid",
"invalidTokenHint": "Try requesting a new invite link."
},
Expand Down
24 changes: 23 additions & 1 deletion packages/admin-next/dashboard/src/lib/api-v2/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +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(
Expand All @@ -15,7 +16,7 @@ export const useV2Session = (options: any = {}) => {
return { user, isLoading, isError, error }
}

export const useV2LoginWithSession = () => {
export const useV2LoginAndSetSession = () => {
return useMutation(
(payload: { email: string; password: string }) =>
medusa.client.request("POST", "/auth/admin/emailpass", {
Expand All @@ -41,3 +42,24 @@ export const useV2LoginWithSession = () => {
}
)
}

export const useV2CreateAuthUser = (provider = "emailpass") => {
// TODO: Migrate type to work for other providers, e.g. Google
return useMutation((args: CreateAuthUserInput) =>
medusa.client.request("POST", `/auth/admin/${provider}`, args)
)
}

export const useV2AcceptInvite = (inviteToken: string) => {
return useMutation((input: AcceptInviteInput) =>
medusa.client.request(
"POST",
`/admin/invites/accept?token=${inviteToken}`,
input.payload,
{},
{
Authorization: `Bearer ${input.token}`,
}
)
)
}
13 changes: 13 additions & 0 deletions packages/admin-next/dashboard/src/lib/api-v2/types/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type AcceptInviteInput = {
payload: {
first_name: string
last_name: string
}
// Token for the created auth user
token: string
}

export type CreateAuthUserInput = {
email: string
password: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export const v2Routes: RouteObject[] = [
path: "*",
lazy: () => import("../../routes/no-match"),
},
{
path: "/invite",
lazy: () => import("../../v2-routes/invite"),
},
{
element: <ProtectedRoute />,
errorElement: <ErrorBoundary />,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Invite as Component } from "./invite"
Loading

0 comments on commit 8592426

Please sign in to comment.