Skip to content

Commit

Permalink
Merge pull request #6 from peter6866/landing_page
Browse files Browse the repository at this point in the history
Created landing page
  • Loading branch information
peter6866 authored Aug 15, 2024
2 parents e1d9927 + 25a8fc9 commit 8b0860f
Show file tree
Hide file tree
Showing 13 changed files with 352 additions and 14 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/kubernetes/backend-config.yaml
/kubernetes/tls-secret.yaml
/secrets/*.pem
/secrets/*.sh
/secrets/*.sh
.DS_Store
8 changes: 8 additions & 0 deletions backend/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"semi": true,
"singleQuote": false,
"printWidth": 80,
"tabWidth": 2,
"endOfLine": "auto",
"trailingComma": "es5"
}
21 changes: 20 additions & 1 deletion backend/controller/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,20 @@ exports.login = catchAsync(async (req, res, next) => {
createSendToken(user, res);
});

// check if the user is logged in
exports.isLoggedIn = catchAsync(async (req, res, next) => {
if (req.user) {
res.status(200).json({
status: "success",
data: {
message: "User is logged in",
},
});
} else {
return next(new AppError("Please log in to access this page", 401));
}
});

// middleware to protect routes
exports.protect = catchAsync(async (req, res, next) => {
let token;
Expand All @@ -283,7 +297,12 @@ exports.protect = catchAsync(async (req, res, next) => {
);
}

const decoded = await promisify(jwt.verify)(token, process.env.JWT_SECRET);
let decoded;
try {
decoded = await promisify(jwt.verify)(token, process.env.JWT_SECRET);
} catch (error) {
return next(new AppError("Invalid token. Please log in again.", 401));
}

const currentUser = await User.findByPk(decoded.id);
// check if the user is banned
Expand Down
2 changes: 2 additions & 0 deletions backend/routes/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ router.post("/signup-verify", authController.signUpVerify);
router.post("/resend-code", authController.resendCode);
// login route
router.post("/login", authController.login);
// isLoggedin route
router.get("/is-loggedin", authController.protect, authController.isLoggedIn);

//update notification settings route
router
Expand Down
Binary file added frontend/public/bid-page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frontend/public/main-page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frontend/public/market-info.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frontend/public/match-page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import AdminPage from "./pages/AdminPage/AdminPage";
import Profile from "./pages/ProfilePage/Profile";
import PrivateRoute from "./PrivateRoute";
import { useTheme, ToggleThemeProvider } from "./context/ThemeContext";
import LandingPage from "./pages/LandingPage/LandingPage";

function AppContent() {
const { darkMode } = useTheme();
Expand Down Expand Up @@ -76,6 +77,7 @@ function AppContent() {
/>
</Route>
<Route path="/auth" element={<Auth />} />
<Route path="/home" element={<LandingPage />} />
</Routes>
</AuthProvider>
</ConfigProvider>
Expand Down
56 changes: 45 additions & 11 deletions frontend/src/PrivateRoute.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,57 @@ import { useAuth } from "./context/AuthContext";
import { useConfig } from "./context/ConfigContext";
import { useDispatch } from "react-redux";
import { fetchInitialData } from "./features/bidSlice";
import axios from "axios";

const PrivateRoute = ({ children, adminPage = false }) => {
const { config, loading } = useConfig();
const { isLoggedIn, isLoading, authToken, role } = useAuth();
const { config, loading: configLoading } = useConfig();
const {
isLoggedIn,
isLoading: authLoading,
authToken,
role,
logout,
} = useAuth();
const location = useLocation();
const dispatch = useDispatch();

useEffect(() => {
if (isLoggedIn && !isLoading && !loading) {
dispatch(fetchInitialData({ authToken, config }));
}
}, [isLoggedIn, isLoading, dispatch, authToken, config, loading]);

if (!isLoggedIn && !isLoading && !loading) {
// Redirect to auth, but store original location for later
return <Navigate to="/auth" state={{ from: location }} replace />;
} else if (isLoggedIn && adminPage && role !== "admin" && !loading) {
const checkToken = async () => {
if (isLoggedIn && !authLoading && !configLoading) {
try {
await axios.get(`${config.REACT_APP_API_URL}/v1/users/is-loggedin`, {
headers: { Authorization: `Bearer ${authToken}` },
});
// Token is valid, fetch initial data
dispatch(fetchInitialData({ authToken, config }));
} catch (error) {
// Token is invalid, call logout
logout();
}
}
};

checkToken();
}, [
isLoggedIn,
authLoading,
configLoading,
config,
authToken,
dispatch,
logout,
]);

if (authLoading || configLoading) {
return null; // Render nothing while loading
}

if (!isLoggedIn) {
// Redirect to landing page, but store original location for later
return <Navigate to="/home" state={{ from: location }} replace />;
}

if (adminPage && role !== "admin") {
return <Navigate to="/" />;
}

Expand Down
Loading

0 comments on commit 8b0860f

Please sign in to comment.