Skip to content
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
1 change: 1 addition & 0 deletions src/components/TopNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function TopNav({ isMaintenance, loggedIn = false }) {
appState.update((s) => {
s.auth = false;
});
setPersistedUser({ theme });
}
navigate('/');
};
Expand Down
27 changes: 10 additions & 17 deletions src/components/auth/SignIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import usePersistedUser from '../../functions/state/persistedUser';
function SignIn() {
const { search } = useLocation();
const { user, token } = queryString.parse(search, { decode: false });
const auth = useStoreState(appState, (s) => s.auth);
const theme = useStoreState(appState, (s) => s.theme);
const [formState, setFormState] = useState({});
const [formData, setFormData] = useState({});
const [persistedUser, setPersistedUser] = usePersistedUser({});

const submit = () => {
const submit = async () => {
setFormState({ submitted: true });
const { email, pass } = formData;
if (!isEmail(email)) {
Expand All @@ -31,23 +30,17 @@ function SignIn() {
setFormState({ error: 'portal access denied' });
} else {
setFormState({ processing: true });
setPersistedUser({ ...persistedUser, email, pass });
getUser({ email, pass, loggingIn: true });
}
};

useEffect(() => {
if (auth?.error) {
setFormState({ error: ['Unauthorized', 'User does not exist'].includes(auth.message) ? 'Login Failed' : auth.message });
setTimeout(() => setFormState({}), 3000);
}
}, [auth]);
const newAuth = await getUser({ email, pass, loggingIn: true });

useEffect(
() => !formState.submitted && setFormState({}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[formData],
);
if (!newAuth || newAuth?.error) {
setFormState({ error: ['Unauthorized', 'User does not exist'].includes(newAuth?.message) ? 'Login Failed' : newAuth?.message || 'Login Failed' });
setTimeout(() => setFormState({}), 5000);
} else {
setPersistedUser({ ...persistedUser, email, pass });
}
}
};

useEffect(() => {
if (user && token) {
Expand Down
7 changes: 5 additions & 2 deletions src/functions/api/lms/getUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ export default async ({ email, pass, loggingIn = false, signal }) => {
response.orgs = [response.orgs];
}

return appState.update((s) => {
s.auth = { email, pass, ...response };
const newAuth = { email, pass, ...response };
appState.update((s) => {
s.auth = newAuth;
});

return newAuth;
} catch (e) {
if (!loggingIn) {
addError({
Expand Down
Loading