Skip to content

Commit

Permalink
BA: updates authentication package v1.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
anicioalexandre committed Sep 27, 2023
1 parent 73b70de commit 61d7685
Show file tree
Hide file tree
Showing 26 changed files with 68 additions and 16 deletions.
6 changes: 6 additions & 0 deletions packages/authentication/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @baseapp-frontend/authentication

## 1.1.2

### Patch Changes

- Add an `ApiClass` option to all hooks that use some internal api class like `AuthApi` or `UserApi`. The user would be able to use its own api class, as long it has the required methods that each hook needs.

## 1.1.1

### Patch Changes
Expand Down
3 changes: 2 additions & 1 deletion packages/authentication/modules/access/useLogin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const useLogin = ({
tokenType = TokenTypes.jwt,
cookieName = ACCESS_COOKIE_NAME,
refreshCookieName = REFRESH_COOKIE_NAME,
ApiClass = AuthApi,
}: IUseLogin) => {
const queryClient = useQueryClient()
const [mfaEphemeralToken, setMfaEphemeralToken] = useState<string | null>(null)
Expand Down Expand Up @@ -87,7 +88,7 @@ const useLogin = ({

const mutation = useMutation({
mutationFn: (data: ILoginRequest) =>
tokenType === TokenTypes.jwt ? AuthApi.login(data) : AuthApi.simpleTokenLogin(data),
tokenType === TokenTypes.jwt ? ApiClass.login(data) : ApiClass.simpleTokenLogin(data),
...loginOptions, // needs to be placed bellow all overridable options
onError: (err, variables, context) => {
loginOptions?.onError?.(err, variables, context)
Expand Down
4 changes: 4 additions & 0 deletions packages/authentication/modules/access/useLogin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import { TokenTypes } from '@baseapp-frontend/utils'

import { UseMutationOptions } from '@tanstack/react-query'

import AuthApi from '../../../services/auth'
import { ICookieName, ILoginMfaRequest, ILoginRequest, LoginResponse } from '../../../types/auth'

type ApiClass = Pick<typeof AuthApi, 'login' | 'simpleTokenLogin'>

export interface IUseLogin extends ICookieName {
// TODO: refactor types
validationSchema?: any
defaultValues?: ILoginRequest
loginOptions?: UseMutationOptions<LoginResponse, unknown, ILoginRequest, any>
mfaOptions?: UseMutationOptions<LoginResponse, unknown, ILoginMfaRequest, any>
tokenType?: TokenTypes
ApiClass?: ApiClass
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IUseRecoverPassword } from './types'
const useRecoverPassword = ({
validationSchema = DEFAULT_VALIDATION_SCHEMA,
defaultValues = DEFAULT_INITIAL_VALUES,
ApiClass = AuthApi,
options,
}: IUseRecoverPassword) => {
const form = useForm({
Expand All @@ -20,7 +21,7 @@ const useRecoverPassword = ({
})

const mutation = useMutation({
mutationFn: ({ email }) => AuthApi.recoverPassword({ email }),
mutationFn: ({ email }) => ApiClass.recoverPassword({ email }),
...options, // needs to be placed bellow all overridable options
onError: (err, variables, context) => {
options?.onError?.(err, variables, context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { UseMutationOptions } from '@tanstack/react-query'

import AuthApi from '../../../services/auth'
import { IForgotPasswordRequest } from '../../../types/auth'

type ApiClass = Pick<typeof AuthApi, 'recoverPassword'>

export interface IUseRecoverPassword {
// TODO: refactor types
validationSchema?: any
defaultValues?: IForgotPasswordRequest
options?: UseMutationOptions<void, unknown, IForgotPasswordRequest, any>
ApiClass?: ApiClass
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IUseResetPassword } from './types'
const useResetPassword = ({
validationSchema = DEFAULT_VALIDATION_SCHEMA,
defaultValues = DEFAULT_INITIAL_VALUES,
ApiClass = AuthApi,
options,
}: IUseResetPassword) => {
const form = useForm({
Expand All @@ -20,7 +21,7 @@ const useResetPassword = ({
})

const mutation = useMutation({
mutationFn: ({ newPassword, token }) => AuthApi.resetPassword({ newPassword, token }),
mutationFn: ({ newPassword, token }) => ApiClass.resetPassword({ newPassword, token }),
...options, // needs to be placed bellow all overridable options
onError: (err, variables, context) => {
options?.onError?.(err, variables, context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { UseMutationOptions } from '@tanstack/react-query'

import AuthApi from '../../../services/auth'
import { IResetPasswordRequest } from '../../../types/auth'

type ApiClass = Pick<typeof AuthApi, 'resetPassword'>

export interface IUseResetPassword {
// TODO: refactor types
validationSchema?: any
defaultValues?: IResetPasswordRequest
options?: UseMutationOptions<void, unknown, IResetPasswordRequest, any>
ApiClass?: ApiClass
}
3 changes: 2 additions & 1 deletion packages/authentication/modules/access/useSignUp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IUseSignUp } from './types'
const useSignUp = <TRegisterRequest extends IRegisterRequest, TRegisterResponse = void>({
validationSchema = DEFAULT_VALIDATION_SCHEMA,
defaultValues = DEFAULT_INITIAL_VALUES as TRegisterRequest,
ApiClass = AuthApi,
options,
}: IUseSignUp<TRegisterRequest, TRegisterResponse>) => {
const form = useForm({
Expand All @@ -21,7 +22,7 @@ const useSignUp = <TRegisterRequest extends IRegisterRequest, TRegisterResponse
})

const mutation = useMutation({
mutationFn: (values) => AuthApi.register<TRegisterResponse>(values),
mutationFn: (values) => ApiClass.register<TRegisterResponse>(values),
...options, // needs to be placed bellow all overridable options
onError: (err, variables, context) => {
options?.onError?.(err, variables, context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { UseMutationOptions } from '@tanstack/react-query'

import AuthApi from '../../../services/auth'
import { IRegisterRequest } from '../../../types/auth'

export interface IUseSignUp<TRegisterRequest = IRegisterRequest, TRegisterResponse> {
type ApiClass = Pick<typeof AuthApi, 'register'>

export interface IUseSignUp<TRegisterRequest = IRegisterRequest, TRegisterResponse = void> {
// TODO: refactor types
validationSchema?: any
defaultValues?: TRegisterRequest
ApiClass?: ApiClass
options?: UseMutationOptions<TRegisterResponse, unknown, TRegisterRequest, any>
}
4 changes: 2 additions & 2 deletions packages/authentication/modules/mfa/useMfaActivate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { useMutation } from '@tanstack/react-query'
import MfaApi from '../../../services/mfa'
import { IUseMfaActivate } from './types'

const useMfaActivate = ({ options }: IUseMfaActivate = {}) =>
const useMfaActivate = ({ options, ApiClass = MfaApi }: IUseMfaActivate = {}) =>
useMutation({
mutationFn: ({ method }) => MfaApi.activate({ method }),
mutationFn: ({ method }) => ApiClass.activate({ method }),
...options, // needs to be placed bellow all overridable options
onError: (err, variables, context) => {
options?.onError?.(err, variables, context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { UseMutationOptions } from '@tanstack/react-query'

import MfaApi from '../../../services/mfa'
import { IMfaActivationResponse, IMfaRequest } from '../../../types/mfa'

type ApiClass = Pick<typeof MfaApi, 'activate'>

export interface IUseMfaActivate {
options?: UseMutationOptions<IMfaActivationResponse, unknown, Pick<IMfaRequest, 'method'>, any>
ApiClass?: ApiClass
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const useMfaActivateConfirm = ({
method,
validationSchema = CODE_VALIDATION_SCHEMA,
defaultValues = CODE_VALIDATION_INITIAL_VALUES,
ApiClass = MfaApi,
options = {},
}: IUseMfaActivateConfirm) => {
const form = useForm({
Expand All @@ -21,7 +22,7 @@ const useMfaActivateConfirm = ({
const queryClient = useQueryClient()

const mutation = useMutation({
mutationFn: (data) => MfaApi.confirmActivation(data),
mutationFn: (data) => ApiClass.confirmActivation(data),
...options, // needs to be placed bellow all overridable options
onError: (err, variables, context) => {
options?.onError?.(err, variables, context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { UseMutationOptions } from '@tanstack/react-query'

import MfaApi from '../../../services/mfa'
import { IMfaConfirmationResponse, IMfaRequest, MfaMethod } from '../../../types/mfa'

type ApiClass = Pick<typeof MfaApi, 'confirmActivation'>

export interface IUseMfaActivateConfirm {
// TODO: refactor types
validationSchema?: any
defaultValues?: Partial<IMfaRequest>
options?: UseMutationOptions<IMfaConfirmationResponse, unknown, IMfaRequest, any>
ApiClass?: ApiClass
method: MfaMethod
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { useQuery } from '@tanstack/react-query'
import MfaApi, { MFA_API_KEY } from '../../../services/mfa'
import { IUseMfaActiveMethods } from './types'

const useMfaActiveMethods = ({ options }: IUseMfaActiveMethods = {}) => {
const useMfaActiveMethods = ({ options, ApiClass = MfaApi }: IUseMfaActiveMethods = {}) => {
const { enabled = true, ...restOptions } = options || {}

const { data, ...rest } = useQuery({
queryFn: () => MfaApi.getActiveMethods(),
queryFn: () => ApiClass.getActiveMethods(),
queryKey: MFA_API_KEY.getActiveMethods(),
...restOptions, // needs to be placed bellow all overridable options
enabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import { IDjangoPaginatedResponse } from '@baseapp-frontend/utils'

import { UseQueryOptions } from '@tanstack/react-query'

import MfaApi from '../../../services/mfa'
import { IMfaActiveMethodResponse } from '../../../types/mfa'

type ApiClass = Pick<typeof MfaApi, 'getActiveMethods'>

export interface IUseMfaActiveMethods {
options?: UseQueryOptions<
IDjangoPaginatedResponse<IMfaActiveMethodResponse>,
unknown,
IDjangoPaginatedResponse<IMfaActiveMethodResponse>,
any
>
ApiClass?: ApiClass
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { useQuery } from '@tanstack/react-query'
import MfaApi, { MFA_API_KEY } from '../../../services/mfa'
import { IUseMfaConfiguration } from './types'

const useMfaConfiguration = ({ options }: IUseMfaConfiguration = {}) => {
const useMfaConfiguration = ({ options, ApiClass = MfaApi }: IUseMfaConfiguration = {}) => {
const { enabled = true, ...restOptions } = options || {}
const { data: configuration, ...rest } = useQuery({
queryFn: () => MfaApi.getConfiguration(),
queryFn: () => ApiClass.getConfiguration(),
queryKey: MFA_API_KEY.getConfiguration(),
...options, // needs to be placed bellow all overridable options
enabled,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { UseQueryOptions } from '@tanstack/react-query'

import MfaApi from '../../../services/mfa'
import { IMfaConfigurationResponse } from '../../../types/mfa'

type ApiClass = Pick<typeof MfaApi, 'getConfiguration'>

export interface IUseMfaConfiguration {
options?: UseQueryOptions<IMfaConfigurationResponse, unknown, IMfaConfigurationResponse, any>
ApiClass?: ApiClass
}
4 changes: 2 additions & 2 deletions packages/authentication/modules/mfa/useMfaDeactivate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { useMutation, useQueryClient } from '@tanstack/react-query'
import MfaApi, { MFA_API_KEY } from '../../../services/mfa'
import { IUseMfaDeactivate } from './types'

const useMfaDeactivate = ({ options }: IUseMfaDeactivate = {}) => {
const useMfaDeactivate = ({ options, ApiClass = MfaApi }: IUseMfaDeactivate = {}) => {
const queryClient = useQueryClient()

return useMutation({
mutationFn: ({ method, code }) => MfaApi.deactivate({ method, code }),
mutationFn: ({ method, code }) => ApiClass.deactivate({ method, code }),
...options, // needs to be placed bellow all overridable options
onError: (err, variables, context) => {
options?.onError?.(err, variables, context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { UseMutationOptions } from '@tanstack/react-query'

import MfaApi from '../../../services/mfa'
import { IMfaDeactivateRequest } from '../../../types/mfa'

type ApiClass = Pick<typeof MfaApi, 'deactivate'>

export interface IUseMfaDeactivate {
options?: UseMutationOptions<void, unknown, IMfaDeactivateRequest, any>
ApiClass?: ApiClass
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import { IUseSimpleTokenUser } from './types'
const useSimpleTokenUser = <TUser extends Partial<IUser>>({
options,
cookieName = ACCESS_COOKIE_NAME,
ApiClass = UserApi,
}: IUseSimpleTokenUser<TUser> = {}) => {
const token = Cookies.get(cookieName)
const queryClient = useQueryClient()

const { data: user, ...rest } = useQuery({
queryFn: () => UserApi.getUser<TUser>(),
queryFn: () => ApiClass.getUser<TUser>(),
queryKey: USER_API_KEY.getUser(),
staleTime: Infinity, // makes cache never expire automatically
enabled: !!token,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { UseQueryOptions } from '@tanstack/react-query'

import UserApi from '../../../services/user'
import { ICookieName } from '../../../types/auth'

type ApiClass = Pick<typeof UserApi, 'getUser'>

export interface IUseSimpleTokenUser<IUser> extends ICookieName {
options?: UseQueryOptions<IUser, unknown, IUser, any>
ApiClass?: ApiClass
}
2 changes: 1 addition & 1 deletion packages/authentication/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@baseapp-frontend/authentication",
"description": "Authentication modules.",
"version": "1.1.1",
"version": "1.1.2",
"main": "./dist/index.ts",
"module": "./dist/index.mjs",
"scripts": {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 61d7685

Please sign in to comment.