Skip to content

Commit

Permalink
feat(admin-next): Email password invite flow in admin 2.0 (#6821)
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl authored Mar 29, 2024
1 parent 45c49e8 commit d97af91
Show file tree
Hide file tree
Showing 15 changed files with 453 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -738,9 +738,10 @@
"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."
"invalidTokenHint": "Try requesting a new invite link.",
"passwordMismatch": "Passwords do not match"
},
"resetPassword": {
"title": "Reset password",
Expand Down Expand Up @@ -952,4 +953,4 @@
"seconds_one": "Second",
"seconds_other": "Seconds"
}
}
}
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
3 changes: 2 additions & 1 deletion packages/admin-next/dashboard/src/routes/invite/invite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useAdminAcceptInvite } from "medusa-react"
import { Trans, useTranslation } from "react-i18next"
import { Link, useSearchParams } from "react-router-dom"
import * as z from "zod"
import i18n from "i18next"

import { useState } from "react"
import { useForm } from "react-hook-form"
Expand All @@ -26,7 +27,7 @@ const CreateAccountSchema = z
if (password !== repeat_password) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Passwords do not match",
message: i18n.t("invite.passwordMismatch"),
path: ["repeat_password"],
})
}
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 d97af91

Please sign in to comment.