Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(admin-next): Email password invite flow in admin 2.0 #6821

Merged
merged 4 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading