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 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
18 changes: 17 additions & 1 deletion client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ 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;

/* from feature-#441 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 65 will prevent the modal from rendering unless user is not 100Dever
const [rejectionModalOpen, setRejectionModalOpen] = useState(true);
const rejectionModalContext = {
isOpen: rejectionModalOpen,
Expand All @@ -34,12 +41,21 @@ function App() {
if (auth.user) setWelcomeUserModalOpen(auth.needsToBeWelcome());
}, [auth]);

/* from featute-#441 implementation of testing for user that has admin role 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 />} />
{/* From feature-#441 Not included now because testing needs to be implemented that logs in a user with admin role */}
{/* {isAdmin() && ( */}
<Route path="/adminDashboard" element={<AdminDashboard />} />
{/* )} */}
</Routes>
{isAuthenticated && (
<Modal context={welcomeUserModalContext}>
Expand Down
9 changes: 9 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,20 @@ const useProvideAuth = () => {
DataService.deleteNeedsToBeWelcome();
};

// Check if user is not null and user has role of admin.
const isAdmin = () => {
return user ? user.role === "admin" : 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,
"role": "admin"
}
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,
role: "user",
});

return cb(null, user);
Expand Down
5 changes: 5 additions & 0 deletions server/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const UserSchema = new mongoose.Schema({
socials: [{ type: String }],
bio: { type: String },
needsToBeWelcome: { type: String },
role: {
type: String,
enum: ["user", "moderator", "admin", "superadmin"],
default: "user",
},
});

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