Skip to content

Commit

Permalink
Merge pull request #232 from Dias999/feature/Auth_enhancements
Browse files Browse the repository at this point in the history
Feature/auth enhancements
  • Loading branch information
andreneto97 authored Sep 17, 2024
2 parents 140e582 + 17ff2aa commit cbee0ff
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 178 deletions.
2 changes: 1 addition & 1 deletion packages/react-data-provider/src/ClientProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const ClientProvider = ({
onRefreshTokenError,
children,
}: Props) => {
const [baseUrl, setBaseUrl] = useState<string>();
const [baseUrl, setBaseUrl] = useState<string>(outerBaseUrl || '');

useEffect(() => {
if (outerBaseUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ type Route = 'signIn' | 'signUp' | 'forgotPassword' | 'resetPassword';
type Query = {
uri?: string;
method?: string;
onSuccess?: (data: unknown) => void;
onError?: (error: unknown) => void;
onSuccess?: ((data: unknown) => void) | null;
onError?: ((error: unknown) => void) | null;
};

type FormData = Record<string, unknown> | null;

export interface AuthFormSubmoduleProps {
route: Route;
query?: Query;
Expand All @@ -50,7 +52,7 @@ export interface AuthFormSubmoduleProps {
formSchema?: RJSFSchema;
formUiSchema?: UiSchema;
advancedProperties?: Record<string, AdvancedProperty>;
formData?: Record<string, unknown> | null;
formData?: FormData;
signInRequestPath?: string;
signInPath?: string;
signUpPath?: string;
Expand All @@ -61,6 +63,7 @@ export interface AuthFormSubmoduleProps {
hideLogo?: boolean;
headerComponent?: ReactNode;
overrideDefaults?: boolean;
submitDataFormatter?: (data: FormData) => FormData;
}

const renderTitle = (title: string | ReactNode) => {
Expand Down Expand Up @@ -110,9 +113,9 @@ const AuthFormSubmodule = (props: AuthFormSubmoduleProps) => {
// request para /me

// set no user using setUser
props.query?.onSuccess(data);
props.query?.onSuccess?.(data);
},
onError: props.query?.onError,
onError: (error) => props.query?.onError?.(error),
},
);

Expand All @@ -121,12 +124,19 @@ const AuthFormSubmodule = (props: AuthFormSubmoduleProps) => {

if (props.route === 'signIn') {
const { username, password } = fields;
doLogin({ username, password, loginPath: props.signInRequestPath });
const loginData = props.submitDataFormatter
? props.submitDataFormatter(fields)
: { username, password, loginPath: props.signInRequestPath };
doLogin(loginData);
return;
}

if (props.route === 'resetPassword') {
await performRequest({ ...fields, passcode });
const fieldsWithPasscode = { ...fields, passcode };
const resetPassData = props.submitDataFormatter
? props.submitDataFormatter(fieldsWithPasscode)
: fieldsWithPasscode;
await performRequest(resetPassData);
return;
}

Expand Down Expand Up @@ -163,32 +173,39 @@ const AuthFormSubmodule = (props: AuthFormSubmoduleProps) => {
{!props.hideTitle && renderTitle(props.title ?? defaultRouteTitle)}

<SchemaForm.Form
schema={{
...defaultFormSchema,
...props.formSchema,
required: props.overrideDefaults
? props.formSchema?.required || []
: [
...(defaultFormSchema.required || []),
...(props.formSchema?.required || []),
],
properties: props.overrideDefaults
? props.formSchema?.properties || {}
schema={
props.overrideDefaults && props.formSchema
? props.formSchema
: {
...(defaultFormSchema.properties || {}),
...(props.formSchema?.properties || {}),
},
}}
uiSchema={{ ...defaultAuthUiSchema, ...props.formUiSchema }}
...defaultFormSchema,
...props.formSchema,
required: [
...(defaultFormSchema.required || []),
...(props.formSchema?.required || []),
],
properties: {
...defaultFormSchema.properties,
...props.formSchema?.properties,
},
}
}
uiSchema={
props.overrideDefaults && props.formUiSchema
? props.formUiSchema
: { ...defaultAuthUiSchema, ...props.formUiSchema }
}
validator={validator}
formData={props.formData || formData}
onChange={({ formData }) => setFormData(formData)}
onSubmit={handleSubmit}
noHtml5Validate={true}
showErrorList={false}
advancedProperties={props.advancedProperties}
customValidate={(formData, errors) =>
validateForm(formData, errors, props.customValidation || [])
customValidate={
props.customValidation
? (formData, errors) =>
validateForm(formData, errors, props.customValidation)
: undefined
}
widgets={widgets}
>
Expand Down
71 changes: 71 additions & 0 deletions packages/react-navigation/src/components/AuthRoute.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React from 'react';

import { Navigate } from 'react-router';
import { useAuth } from '@concepta/react-auth-provider';
import { AuthModule, AuthModuleProps } from '@concepta/react-material-ui/';
import { toast } from 'react-toastify';

type Route = 'signIn' | 'signUp' | 'forgotPassword' | 'resetPassword';

type AuthRouteProps = {
home: string;
moduleProps?: AuthModuleProps;
route: Route;
};

const AuthRoute = (props: AuthRouteProps) => {
const { home, moduleProps, route } = props;
const { accessToken: authAccessToken } = useAuth();

const accessToken = authAccessToken ?? localStorage.getItem('accessToken');

if (accessToken) {
return <Navigate to={home} replace />;
}

const query: AuthModuleProps['query'] = {
onSuccess: () => toast.success('Success!'),
onError: (error: any) =>
toast.error(
error?.response?.data?.message ||
'An error has occurred. Please try again later or contact support for assistance.',
),
...props.moduleProps?.query,
};

const routeProps: Record<Route, AuthModuleProps> = {
resetPassword: {
route: 'resetPassword',
signInPath: '/sign-in',
query,
},
forgotPassword: {
route: 'forgotPassword',
signInPath: '/sign-in',
query,
},
signIn: {
route: 'signIn',
},
signUp: {
route: 'signUp',
signInPath: '/sign-in',
query,
},
};

return (
<AuthModule
{...routeProps[route]}
{...{
...moduleProps,
query:
props.moduleProps.overrideDefaults && props.moduleProps?.query
? props.moduleProps?.query
: routeProps[route].query,
}}
/>
);
};

export default AuthRoute;
42 changes: 0 additions & 42 deletions packages/react-navigation/src/components/ForgotPasswordRoute.tsx

This file was deleted.

24 changes: 0 additions & 24 deletions packages/react-navigation/src/components/LoginRoute.tsx

This file was deleted.

39 changes: 0 additions & 39 deletions packages/react-navigation/src/components/ResetPasswordRoute.tsx

This file was deleted.

23 changes: 15 additions & 8 deletions packages/react-navigation/src/components/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ import {
NavbarProps,
} from '@concepta/react-material-ui/';

import LoginRoute from './LoginRoute';
import DefaultRoute from './DefaultRoute';
import SignUpRoute from './SignUpRoute';
import ResetPasswordRoute from './ResetPasswordRoute';
import ForgotPasswordRoute from './ForgotPasswordRoute';
import AuthRoute from './AuthRoute';

export type AuthModule = {
signIn?: AuthModuleProps;
Expand Down Expand Up @@ -90,25 +87,34 @@ const Router = ({
element: renderSignIn ? (
renderSignIn(home)
) : (
<LoginRoute home={home} moduleProps={authModuleProps?.signIn} />
<AuthRoute
home={home}
moduleProps={authModuleProps?.signIn}
route="signIn"
/>
),
},
{
path: '/sign-up',
element: renderSignUp ? (
renderSignUp(home)
) : (
<SignUpRoute home={home} moduleProps={authModuleProps?.signUp} />
<AuthRoute
home={home}
moduleProps={authModuleProps?.signUp}
route="signUp"
/>
),
},
{
path: '/forgot-password',
element: renderForgotPassword ? (
renderForgotPassword(home)
) : (
<ForgotPasswordRoute
<AuthRoute
home={home}
moduleProps={authModuleProps?.forgotPassword}
route={'forgotPassword'}
/>
),
},
Expand All @@ -117,9 +123,10 @@ const Router = ({
element: renderResetPassword ? (
renderResetPassword(home)
) : (
<ResetPasswordRoute
<AuthRoute
home={home}
moduleProps={authModuleProps?.resetPassword}
route="resetPassword"
/>
),
},
Expand Down
Loading

0 comments on commit cbee0ff

Please sign in to comment.