Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue #441 #447

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
16 changes: 15 additions & 1 deletion client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ import { useAuthContext } from "contexts/AuthContext";
import LandingPage from "pages/LandingPage";
import CalendarPage from "pages/CalendarPage";
import { AdminDashboard } from "pages/AdminDashboard";
// eslint-disable-next-line
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

function App() {
const auth = useAuthContext();
const isAuthenticated = auth.isAuthenticated();
const isNot100Dever = auth.isNot100Dever();
const deleteNeedsToBeWelcome = auth.deleteNeedsToBeWelcome;
/* implementation of testing needed before isAdmin and isLoading can be used
const isAdmin = auth.isAdmin;
const isLoading = auth.loading; // Destructure loading from auth
*/
//Sets rejection modal to true because updating state is a pain
//Line 49 will prevent the modal from rendering unless user is not 100Dever
//Line 63 will prevent the modal from rendering unless user is not 100Dever
const [rejectionModalOpen, setRejectionModalOpen] = useState(true);
const rejectionModalContext = {
isOpen: rejectionModalOpen,
Expand All @@ -34,12 +39,21 @@ function App() {
if (auth.user) setWelcomeUserModalOpen(auth.needsToBeWelcome());
}, [auth]);

/* implementation of testing for user that has admin priveleges needed before use.
if (isLoading) {
return <div>Loading...</div>; // Render a loading indicator while fetching user data
}
*/

return (
<>
<Routes>
<Route path="/" element={<LandingPage />} />
<Route path="/calendar" element={<CalendarPage />} />
{/* Not included now because testing needs to be implemented that logs in a user with admin property */}
{/* {isAdmin() && ( */}
<Route path="/adminDashboard" element={<AdminDashboard />} />
{/* )} */}
</Routes>
{isAuthenticated && (
<Modal context={welcomeUserModalContext}>
Expand Down
10 changes: 10 additions & 0 deletions client/src/contexts/AuthContext/useProvideAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import { useNavigate } from "react-router-dom";
const useProvideAuth = () => {
const [user, setUser] = useState(null);
const navigate = useNavigate();
const [loading, setLoading] = useState(true);

// Check if there is already a user session
useEffect(() => {
// If so, save user's information to the context
DataService.getCurrentUser().then(response => {
console.log(response.data);
setUser(response.data);
setLoading(false); // set loading to false after fetching user data
});
}, []);

Expand Down Expand Up @@ -48,13 +50,21 @@ const useProvideAuth = () => {
DataService.deleteNeedsToBeWelcome();
};

// Check if user is not null and admin property is true.
// admin property is typed as Number as of initial declaration
const isAdmin = () => {
return user ? user.admin === 1 : false;
};

return {
user,
logout,
isAuthenticated,
isAdmin,
isNot100Dever,
needsToBeWelcome,
deleteNeedsToBeWelcome,
loading,
};
};

Expand Down
1 change: 1 addition & 0 deletions client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ModalProvider from "contexts/ModalContext";
import EventsProvider from "contexts/EventsContext";
import FormModalProvider from "contexts/FormModalContext";
import { createRoot } from "react-dom/client";
// eslint-disable-next-line no-unused-vars
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

const rootElement = document.getElementById("root");
Expand Down
3 changes: 2 additions & 1 deletion server/config/mockUser.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"discordId": "9999999999999999",
"avatar": "450a0b622a7c905c4c75e190cb21f2df",
"bio": "",
"__v": 0
"__v": 0,
"admin": 1
}
1 change: 1 addition & 0 deletions server/config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module.exports = function (passport) {
socials: [],
bio: "",
needsToBeWelcome: true,
admin: 0,
});

return cb(null, user);
Expand Down
1 change: 1 addition & 0 deletions server/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const UserSchema = new mongoose.Schema({
socials: [{ type: String }],
bio: { type: String },
needsToBeWelcome: { type: String },
admin: { type: Number, default: 0 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some requested changes from #441

});

UserSchema.virtual("avatarURL").get(function () {
Expand Down