Skip to content

Commit

Permalink
Show registration page if no admin account exists (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
niamu authored Nov 29, 2023
1 parent aa0743b commit 5bf5b3b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ui/src/Pages/Auth/Register.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function Register(props) {
credentials={[username, pass, invite]}
error={[setUsernameErr, setPassErr, setInviteErr]}
/>
<Link to="/login">I have an account</Link>
{auth.admin_exists && <Link to="/login">I have an account</Link>}
</footer>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion ui/src/Pages/Preferences/Account/DelAccountBtn.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function DelAccountBtn() {
(async () => {
if (deleteAccount.deleted && !deleteAccount.error) {
await dispatch(logout());
history.push("/login");
window.location.href = "/";
}
})();
}, [deleteAccount, dispatch, history]);
Expand Down
18 changes: 14 additions & 4 deletions ui/src/Routes/Private.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect } from "react";
import { Route, useHistory } from "react-router-dom";
import { useDispatch, useSelector } from "react-redux";

import { updateAuthToken } from "../actions/auth.js";
import { checkAdminExists, updateAuthToken } from "../actions/auth.js";
import { fetchUser } from "../actions/user.js";

function PrivateRoute(props) {
Expand All @@ -21,7 +21,7 @@ function PrivateRoute(props) {
?.split("=")[1];

const { logged_in, error } = auth.login;
const { token } = auth;
const { token, admin_exists } = auth;

useEffect(() => {
if (tokenInCookie && !token) {
Expand All @@ -40,9 +40,18 @@ function PrivateRoute(props) {
}

if (!token && !tokenInCookie) {
history.push("/login");
switch (admin_exists) {
case true:
history.push("/login");
return;
case false:
history.push("/register");
return;
default:
return;
}
}
}, [error, history, logged_in, token, tokenInCookie, dispatch]);
}, [error, history, logged_in, token, tokenInCookie, admin_exists, dispatch]);

// auto logout when logged out in another tab
useEffect(() => {
Expand Down Expand Up @@ -88,6 +97,7 @@ function PrivateRoute(props) {
}, [history.location.pathname]);

useEffect(() => {
dispatch(checkAdminExists());
dispatch(fetchUser());
}, [dispatch]);

Expand Down
2 changes: 1 addition & 1 deletion ui/src/reducers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const deleteAccount = {

const initialState = {
token: null,
admin_exists: false,
admin_exists: null,
login,
register,
createNewInvite,
Expand Down

0 comments on commit 5bf5b3b

Please sign in to comment.