Skip to content
Open
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
7 changes: 6 additions & 1 deletion packages/react/src/hooks/useRCAuth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext } from 'react';
import { useContext, useState } from 'react';
import { useToastBarDispatch } from '@embeddedchat/ui-elements';
import RCContext from '../context/RCInstance';
import {
Expand All @@ -10,6 +10,7 @@ import {

export const useRCAuth = () => {
const { RCInstance } = useContext(RCContext);
const [loading, setLoading] = useState(false);
const setIsTotpModalOpen = totpModalStore(
(state) => state.setIsTotpModalOpen
);
Expand All @@ -34,6 +35,7 @@ export const useRCAuth = () => {
const dispatchToastMessage = useToastBarDispatch();

const handleLogin = async (userOrEmail, password, code) => {
setLoading(true);
try {
const res = await RCInstance.login(userOrEmail, password, code);
const permissions = await RCInstance.permissionInfo();
Expand Down Expand Up @@ -78,10 +80,13 @@ export const useRCAuth = () => {
}
} catch (e) {
console.error('A error occurred while setting up user', e);
} finally {
setLoading(false);
}
};

return {
handleLogin,
loading,
};
};
4 changes: 1 addition & 3 deletions packages/react/src/views/ChatBody/ChatBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
} from '../../store';
import MessageList from '../MessageList';
import TotpModal from '../TotpModal/TwoFactorTotpModal';
import { useRCAuth } from '../../hooks/useRCAuth';
import LoginForm from '../LoginForm/LoginForm';
import ThreadMessageList from '../Thread/ThreadMessageList';
import RecentMessageButton from './RecentMessageButton';
Expand Down Expand Up @@ -75,7 +74,6 @@ const ChatBody = ({
uiKitModalData: state.uiKitModalData,
}));

const { handleLogin } = useRCAuth();
const { handleServerInteraction } = useUiKitActionManager();

const isUserAuthenticated = useUserStore(
Expand Down Expand Up @@ -365,7 +363,7 @@ const ChatBody = ({
/>
)}

<TotpModal handleLogin={handleLogin} />
<TotpModal />
<LoginForm />

{uiKitModalOpen && (
Expand Down
6 changes: 4 additions & 2 deletions packages/react/src/views/LoginForm/LoginForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Input,
Icon,
useTheme,
Throbber,
} from '@embeddedchat/ui-elements';
import { useLoginStore } from '../../store';
import { useRCAuth } from '../../hooks/useRCAuth';
Expand All @@ -22,7 +23,7 @@ export default function LoginForm() {
const setIsLoginModalOpen = useLoginStore(
(state) => state.setIsLoginModalOpen
);
const { handleLogin } = useRCAuth();
const { handleLogin, loading } = useRCAuth();

const { theme } = useTheme();

Expand Down Expand Up @@ -132,11 +133,12 @@ export default function LoginForm() {
<Button
type="primary"
onClick={handleSubmit}
disabled={loading}
css={css`
margin: 10px 0;
`}
>
Login
{loading ? <Throbber /> : 'Login'}
</Button>
</Box>
</GenericModal>
Expand Down
14 changes: 11 additions & 3 deletions packages/react/src/views/TotpModal/TwoFactorTotpModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import {
Modal,
Input,
Button,
Throbber,
} from '@embeddedchat/ui-elements';
import { totpModalStore, useUserStore } from '../../store';
import { useRCAuth } from '../../hooks/useRCAuth';

export default function TotpModal({ handleLogin }) {
export default function TotpModal() {
let { handleLogin, loading } = useRCAuth();
const [accessCode, setAccessCode] = useState(null);
const isTotpModalOpen = totpModalStore((state) => state.isTotpModalOpen);
const setIsTotpModalOpen = totpModalStore(
Expand Down Expand Up @@ -55,8 +58,13 @@ export default function TotpModal({ handleLogin }) {
<Button type="secondary" onClick={handleClose}>
Cancel
</Button>
<Button type="primary" onClick={handleSubmit}>
Submit
<Button
type="primary"
onClick={handleSubmit}
disabled={loading}
style={{ height: '36px' }}
>
{loading ? <Throbber /> : "Submit"}
</Button>
</Modal.Footer>
</Box>
Expand Down