-
-
Notifications
You must be signed in to change notification settings - Fork 5k
feat: add admin MFA UI #15493
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: add admin MFA UI #15493
Changes from 4 commits
f5c18e9
8f8ab02
39bc38f
cea4aa3
c6eeabb
37dca43
7f821b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import type { | ||
| AuthMfaFactorResponse, | ||
| AuthMfaListResponse, | ||
| AuthMfaRecoveryCodesResponse, | ||
| AuthMfaSetupResponse, | ||
| FetchError, | ||
| } from "@medusajs/js-sdk" | ||
| import type { AuthTypes } from "@medusajs/types" | ||
| import { | ||
| QueryKey, | ||
| UseMutationOptions, | ||
| UseQueryOptions, | ||
| useMutation, | ||
| useQuery, | ||
| } from "@tanstack/react-query" | ||
| import { sdk } from "../../lib/client" | ||
| import { queryClient } from "../../lib/query-client" | ||
| import { queryKeysFactory } from "../../lib/query-key-factory" | ||
|
|
||
| const MFA_QUERY_KEY = "mfa" as const | ||
| export const mfaQueryKeys = queryKeysFactory(MFA_QUERY_KEY) | ||
|
|
||
| export type { | ||
| AuthLoginResponse, | ||
| AuthMfaRequiredResponse, | ||
| AuthMfaSetupResponse, | ||
| } from "@medusajs/js-sdk" | ||
|
|
||
| export type AuthMfa = AuthTypes.AuthMfaDTO | ||
| export type AuthMfaChallenge = AuthTypes.AuthMfaChallengeDTO | ||
| export type AuthMfaChallengeMethod = AuthTypes.AuthMfaChallengeMethod | ||
| export type AuthMfaProvider = AuthTypes.AuthMfaProvider | ||
|
|
||
| export const callbackWithCloudAuth = async ( | ||
| query: Record<string, unknown> | ||
| ): ReturnType<typeof sdk.auth.callback> => | ||
| sdk.auth.callback("user", "cloud", query) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume this works just fine when doing auth with Cloud? How does it work, does it require MFA after the oauth or it's skipped entirely?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cloud OAuth can return an MFA challenge after the OAuth callback. Admin then shows the same MFA challenge screen and only continues after challenge verification. So admin MFA is not skipped |
||
|
|
||
| export const useAuthMfa = ( | ||
| options?: Omit< | ||
| UseQueryOptions< | ||
| AuthMfaListResponse, | ||
| FetchError, | ||
| AuthMfaListResponse, | ||
| QueryKey | ||
| >, | ||
| "queryFn" | "queryKey" | ||
| > | ||
| ) => { | ||
| const { data, ...rest } = useQuery({ | ||
| queryFn: () => sdk.auth.mfa.list(), | ||
| queryKey: mfaQueryKeys.lists(), | ||
| ...options, | ||
| }) | ||
|
|
||
| return { ...data, ...rest } | ||
| } | ||
|
|
||
| export const useStartAuthMfa = ( | ||
| options?: UseMutationOptions< | ||
| AuthMfaSetupResponse, | ||
| FetchError, | ||
| { | ||
| provider: AuthTypes.AuthMfaProvider | ||
| label?: string | null | ||
| issuer?: string | ||
| metadata?: Record<string, unknown> | null | ||
| } | ||
| > | ||
| ) => { | ||
| return useMutation({ | ||
| mutationFn: (payload) => sdk.auth.mfa.start(payload), | ||
| ...options, | ||
| onSuccess: (data, variables, context) => { | ||
| queryClient.invalidateQueries({ queryKey: mfaQueryKeys.lists() }) | ||
| options?.onSuccess?.(data, variables, context) | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| export const useVerifyAuthMfa = ( | ||
| id: string, | ||
| options?: UseMutationOptions< | ||
| AuthMfaFactorResponse, | ||
| FetchError, | ||
| { code: string } | ||
| > | ||
| ) => { | ||
| return useMutation({ | ||
| mutationFn: (payload) => sdk.auth.mfa.verify(id, payload), | ||
| ...options, | ||
| onSuccess: (data, variables, context) => { | ||
| queryClient.invalidateQueries({ queryKey: mfaQueryKeys.lists() }) | ||
| options?.onSuccess?.(data, variables, context) | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| export const useDisableAuthMfa = ( | ||
| id: string, | ||
| options?: UseMutationOptions< | ||
| AuthMfaFactorResponse, | ||
| FetchError, | ||
| { method?: AuthTypes.AuthMfaChallengeMethod; code?: string } | void | ||
| > | ||
| ) => { | ||
| return useMutation({ | ||
| mutationFn: (payload) => sdk.auth.mfa.disable(id, payload ?? {}), | ||
| ...options, | ||
| onSuccess: (data, variables, context) => { | ||
| queryClient.invalidateQueries({ queryKey: mfaQueryKeys.lists() }) | ||
| options?.onSuccess?.(data, variables, context) | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| export const useGenerateAuthMfaRecoveryCodes = ( | ||
| options?: UseMutationOptions< | ||
| AuthMfaRecoveryCodesResponse, | ||
| FetchError, | ||
| { count?: number } | void | ||
| > | ||
| ) => { | ||
| return useMutation({ | ||
| mutationFn: (payload) => sdk.auth.mfa.generateRecoveryCodes(payload ?? {}), | ||
| ...options, | ||
| }) | ||
| } | ||
|
|
||
| export const useVerifyAuthMfaChallenge = ( | ||
| options?: UseMutationOptions< | ||
| string, | ||
| FetchError, | ||
| { id: string; method: AuthTypes.AuthMfaChallengeMethod; code: string } | ||
| > | ||
| ) => { | ||
| return useMutation({ | ||
| mutationFn: ({ id, method, code }) => | ||
| sdk.auth.mfa.verifyChallenge(id, { method, code }), | ||
| ...options, | ||
| }) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.