Skip to content

Commit

Permalink
Merge pull request #315 from Saksham2k3s/bug/restrict-access-home-abo…
Browse files Browse the repository at this point in the history
…ut-304

Solved issue 304
  • Loading branch information
hustlerZzZ authored Jun 15, 2024
2 parents ecb3718 + 3bda3c0 commit e2c6c73
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 11 deletions.
1 change: 1 addition & 0 deletions server/controllers/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ exports.studentLogin = async (req, res) => {
user,
});
} else {

return res.status(403).json({
success: false,
message: "Pasword Incorrect",
Expand Down
3 changes: 3 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Loader from './components/Loader/Loader';
import ForgotPassword from './pages/ForgotPassword';
import ResetPassword from './pages/ResetPassword';
import { ThemeProvider } from './themeContext';
import { AuthProvider } from './authContext'
import EditProfile from './pages/EditProfile';

const Layout = ({ children }) => {
Expand All @@ -26,6 +27,7 @@ const Layout = ({ children }) => {

function App() {
return (
<AuthProvider>
<ThemeProvider>
<div className=''>
<Routes>
Expand All @@ -46,6 +48,7 @@ function App() {
</Routes>
</div>
</ThemeProvider>
</AuthProvider>
);
}

Expand Down
23 changes: 23 additions & 0 deletions src/authContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createContext, useContext, useState } from "react";

const authContext = createContext({
isAuthenticated: false
});

export const useAuth = () => useContext(authContext);

const AuthProvider = ({ children }) => {
const [isAuthenticated, setAuthenticated] = useState(false);

const checkAuthentication = (token) => {
setAuthenticated(!!token);
};

return (
<authContext.Provider value={{ isAuthenticated, checkAuthentication }}>
{children}
</authContext.Provider>
);
};

export { AuthProvider };
10 changes: 10 additions & 0 deletions src/pages/About.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import Loader from "../components/Loader/Loader";
import "aos/dist/aos.css";
import Footer from "../components/Footer";
import FloatBtn from "../components/FloatBtn/FloatBtn";
import { useAuth } from "../authContext";
import { useNavigate } from "react-router-dom";

const About = () => {
const navigate = useNavigate()
const { isAuthenticated } = useAuth();
const [loading,setLoading] = useState(false);

useEffect(() => {
Expand All @@ -16,6 +20,12 @@ const About = () => {
setLoading(false);
}, []);

useEffect(() => {
if(!isAuthenticated){
navigate('/')
}
}, [])

return (
<>
{
Expand Down
12 changes: 10 additions & 2 deletions src/pages/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ import CanteenList from "../components/CanteenList";
import Loader from "../components/Loader/Loader";
import Footer from "../components/Footer";
import FloatBtn from "../components/FloatBtn/FloatBtn";

import { useAuth } from "../authContext";
import { useNavigate } from "react-router-dom";
function Home() {

const navigate = useNavigate();
const { isAuthenticated } = useAuth();
const [canteenData , setCanteenData] = useState();
const [loading, setLoading] = useState(false);

useEffect(() => {
if(!isAuthenticated){
navigate('/')
}
}, [])

const getCanteenData = async () =>{
try{
setLoading(true);
Expand Down
18 changes: 11 additions & 7 deletions src/pages/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import React, { useState, useEffect } from "react";

import React, { useState, useContext } from "react";

import { AiOutlineEye, AiOutlineEyeInvisible } from "react-icons/ai";
import { Link, useNavigate } from "react-router-dom";
import { toast } from "react-hot-toast";
import axios from "axios";
import logo from "../assets/logo2.png";
import Loader from "../components/Loader/Loader";

import { useAuth } from "../authContext";
function Login() {
const [formData, setFormData] = useState({
email: "",
accountType: "",
password: "",
});

const { checkAuthentication } = useAuth();
const [showPassword, setShowPassword] = useState(false);
const [loading, setLoading] = useState(false);
const [rememberMe, setRememberMe] = useState(false);
Expand Down Expand Up @@ -44,17 +46,19 @@ function Login() {
event.preventDefault();
setLoading(true);

const apiUrl =
formData.accountType === "User"
? `${process.env.REACT_APP_BASE_URL}/studentLogin`
: `${process.env.REACT_APP_BASE_URL}/canteenLogin`;

const apiUrl = `http://localhost:8000/api/v1/studentLogin`;
// // const apiUrl = `${process.env.REACT_APP_BASE_URL}/studentLogin`;


try {
const response = await axios.post(apiUrl, formData);
const { token, cantId } = response.data;

if (formData.accountType === "User") {
console.log("This is our login response", response.data);
toast.success("User logged in successfully!");
checkAuthentication(token);
navigate("/home");
} else {
toast.success("User Logged in");
Expand Down
11 changes: 10 additions & 1 deletion src/pages/News.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ import NewsCard from "../components/NewsCard";
import Loader from "../components/Loader/Loader";
import Footer from "../components/Footer";
import FloatBtn from "../components/FloatBtn/FloatBtn";

import { useAuth } from "../authContext";
import { useNavigate } from "react-router-dom";
function News() {
const navigate = useNavigate()
const { isAuthenticated } = useAuth();
const [articles, setArticles] = useState([]);
const [loading, setLoading] = useState(false);

useEffect(() => {
if(!isAuthenticated){
navigate('/')
}
}, [])

const fetchNews = async (query) => {
try {
setLoading(true);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Signup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function Signup() {
const response = await axios.post(apiUrl, formData);

toast.success("Account Created Successfully!");
navigate("/home");
navigate("/");
} catch (error) {
const errorMessage =
error.response?.data?.message ||
Expand Down

0 comments on commit e2c6c73

Please sign in to comment.