Skip to content

Commit

Permalink
987879879987789897
Browse files Browse the repository at this point in the history
  • Loading branch information
Yadzzz committed Jan 11, 2024
1 parent cadbb67 commit 5c65ac6
Show file tree
Hide file tree
Showing 16 changed files with 403 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<string> AuthenticateAsync(string username, string password)
{
// Here you should implement your user authentication logic.
// For this example, we'll assume a simple username/password check.
if (username == "yad" && password == "123")
if (username == "yadmarzan@gmail.com" && password == "123")
{
// Generate a token for the authenticated user
var userId = "2246a6c9-b7ac-46a1-bc54-a52270a0668d"; // Replace with the actual user ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,22 @@ public async Task<IActionResult> AuthenticateAsync([FromBody] UserAuthentication
[HttpGet("validatetoken")]
public async Task<IActionResult> ValidateToken()
{
await Console.Out.WriteLineAsync("123");
var userAuthenticationResponse = await _authenticationProviderService.ValidateToken();
if (userAuthenticationResponse == null)
{
await Console.Out.WriteLineAsync("1");
return Unauthorized(new { message = "Error" });
}

if (!userAuthenticationResponse.Success)
{
await Console.Out.WriteLineAsync("2");
return Unauthorized(new { message = userAuthenticationResponse.Error });
}

await Console.Out.WriteLineAsync("Valid");

//return Ok(new { message = "Token is valid" });
return Ok(userAuthenticationResponse);
}
Expand All @@ -80,7 +85,6 @@ public async Task<IActionResult> RefreshToken()
});
}

[Authorize]
[HttpGet("logout")]
public async Task<IActionResult> Logout()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@ namespace Template_Web.Server.Controllers.Users
public class UserContextController : BaseController
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ApplicationSettings _applicationSettings;

public UserContextController(IHttpContextAccessor httpContextAccessor, ApplicationSettings applicationSettings)
public UserContextController(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
_applicationSettings = applicationSettings;
}

[HttpGet]
public async Task<IActionResult> Get()

Check warning on line 21 in Template_Web.Server/Controllers/Users/UserContextController.cs

View workflow job for this annotation

GitHub Actions / build_backend

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 21 in Template_Web.Server/Controllers/Users/UserContextController.cs

View workflow job for this annotation

GitHub Actions / build_backend

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{


return Ok(new { Id = "123" , Name = UserContext.Name, ApplicationSettings = _applicationSettings });
return Ok(new { Id = "123" , Name = UserContext.Name });
}
}
}
4 changes: 4 additions & 0 deletions Template_Web.Server/Template_Web.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@
</ProjectReference>
</ItemGroup>

<ItemGroup>
<Folder Include="Controllers\BusService\" />
</ItemGroup>

</Project>
73 changes: 44 additions & 29 deletions template_web.client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
Navigate,
} from "react-router-dom";
import Home from "./Pages/Home";
import ClientHome from "./Pages/Client/Home";
import JQueryComponents from "./Components/Shared/JQueryComponents";
import Login from "./Pages/Login";
import ApiClient from "./Infrastructure/API/apiClient";
Expand All @@ -19,6 +18,10 @@ import Header from "./Components/Shared/Header";
import Features from "./Pages/Features";
import Pricing from "./Pages/Pricing";
import Contact from "./Pages/Contact";
import Loader from "./Components/Shared/Loader";

import ClientHome from "./Pages/Client/Home";
import Buses from "./Pages/Client/Buses";

function App() {
const [isAuthenticated, setIsAuthenticated] = useState<boolean | null>(null);
Expand All @@ -27,42 +30,44 @@ function App() {
const { user, setUser } = useCurrentUser();

useEffect(() => {
const checkAuthentication = async () => {
try {
const user = await AuthService(setIsAuthenticated);
checkAuthentication();
}, []);

if (user) {
console.log(user);
setUser(user);

// Use the callback version of setIsAuthenticated to avoid the loop
setIsAuthenticated((currentState) => {
if (currentState !== true) {
return true;
}
return currentState;
});
} else {
setIsAuthenticated(false);
}
} catch (error) {
console.error("Check authentication error:", error);
const checkAuthentication = async () => {
try {
const user = await AuthService(setIsAuthenticated);

if (user) {
setUser(user);

// Use the callback version of setIsAuthenticated to avoid the loop
setIsAuthenticated((currentState) => {
if (currentState !== true) {
return true;
}
return currentState;
});
} else {
setIsAuthenticated(false);
} finally {
setTokenValidationAttempted(true);
}
};

if (!tokenValidationAttempted) {
checkAuthentication();
} catch (error) {
console.error("Check authentication error:", error);
setIsAuthenticated(false);
} finally {
setTokenValidationAttempted(true);
}
}, [tokenValidationAttempted]); // Use tokenValidationAttempted as a dependency
};

function ProtectedRoute({ element }: { element: React.ReactNode }) {
if (isAuthenticated === null) {
return <Loader />; // Show loader while checking authentication
}

if (isAuthenticated) {
return element;
} else {
return <Navigate to="/login" />; // Redirect to the login page
return <Navigate to="/login" />;
}
}

Expand All @@ -72,23 +77,33 @@ function App() {

return (
<Router>
<Suspense fallback={<div className="container">Loading...</div>}>
<Suspense fallback={<Loader />}>
<div className="bg-white">
<Routes>
{/*Public Routes*/}
<Route path="/" element={<Home />} />
<Route path="/features" element={<Features />} />
<Route path="/pricing" element={<Pricing />} />
<Route path="/contact" element={<Contact />} />

{/*Client Routes*/}
<Route
path="/client"
path="/client/dashboard"
element={<ProtectedRoute element={<ClientHome />} />}
/>
<Route
path="/client/buses"
element={<ProtectedRoute element={<Buses />} />}
/>

{/*Authentication Routes*/}
<Route
path="/login"
element={<Login setIsAuthenticated={setIsAuthenticated} />}
/>
<Route path="/logout" element={<Logout />} />

{/*Catch-all route*/}
<Route path="*" element={<Navigate to="/" />} />
</Routes>
</div>
Expand Down
199 changes: 199 additions & 0 deletions template_web.client/src/Components/Client/Header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { useState } from "react";
import logo from "../../assets/icon.png";
import profileIcon from "../../assets/profile.png";

interface headerProps {
currentPage: string;
}

const Header = (props: headerProps) => {
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
const [isProfileDropdownOpen, setIsProfileDropdownOpen] = useState(false);

return (
<nav className="bg-gray-800">
<div className="mx-auto max-w-7xl px-2 sm:px-6 lg:px-8">
<div className="relative flex h-16 items-center justify-between">
{/* Mobile menu button */}
<div className="absolute inset-y-0 left-0 flex items-center sm:hidden">
<button
type="button"
className="inline-flex items-center justify-center p-2 rounded-md text-gray-400 hover:text-white hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-white"
aria-controls="mobile-menu"
aria-expanded="false"
onClick={() => setIsMobileMenuOpen(!isMobileMenuOpen)}
>
<span className="sr-only">Open main menu</span>
{/* Icon for menu open/close */}
<svg
className={`${isMobileMenuOpen ? "hidden" : "block"} h-6 w-6`}
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M4 6h16M4 12h16m-7 6h7"
/>
</svg>
<svg
className={`${isMobileMenuOpen ? "block" : "hidden"} h-6 w-6`}
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</button>
</div>

{/* Logo and Navigation Menu */}
<div className="flex-1 flex items-center justify-center sm:items-stretch sm:justify-start">
<div className="flex-shrink-0 flex items-center">
<img className="h-8 w-auto" src={logo} alt="Your Company" />
</div>
<div className="hidden sm:block sm:ml-6">
<div className="flex space-x-4">
{/* Navigation Links */}
{/*<a href="/" className="bg-gray-900 text-white px-3 py-2 rounded-md text-sm font-medium">Dashboard</a>*/}
<a
href="/client/dashboard"
className={`${
props.currentPage === "dashboard"
? "bg-gray-900 text-white"
: "text-gray-200 hover:bg-gray-700 hover:text-white"
} px-3 py-2 rounded-md text-sm font-medium`}
>
Dashboard
</a>
<a
href="/client/buses"
className={`${
props.currentPage === "busservices"
? "bg-gray-900 text-white"
: "text-gray-200 hover:bg-gray-700 hover:text-white"
} px-3 py-2 rounded-md text-sm font-medium`}
>
Bus Services
</a>
<a
href="/client/access"
className={`${
props.currentPage === "access"
? "bg-gray-900 text-white"
: "text-gray-200 hover:bg-gray-700 hover:text-white"
} px-3 py-2 rounded-md text-sm font-medium`}
>
Access
</a>
</div>
</div>
</div>

{/* Right-aligned items */}
<div className="absolute inset-y-0 right-0 flex items-center pr-2 sm:static sm:inset-auto sm:ml-6 sm:pr-0" style={{ zIndex: 9999 }}>
{/* Profile dropdown */}
<div className="relative ml-3">
<div>
<button
type="button"
className="flex rounded-full bg-gray-800 text-sm focus:outline-none focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-gray-800"
id="user-menu-button"
aria-haspopup="true"
onClick={() =>
setIsProfileDropdownOpen(!isProfileDropdownOpen)
}
>
<span className="sr-only">Open user menu</span>
<img
className="h-8 w-8 rounded-full"
src={profileIcon}
alt="User profile"
/>
</button>
</div>
{/* Dropdown menu */}
<div
className={`${
isProfileDropdownOpen ? "block" : "hidden"
} absolute right-0 mt-2 w-48 origin-top-right rounded-md bg-white py-1 shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none`}
role="menu"
aria-orientation="vertical"
aria-labelledby="user-menu-button"
>
<a
href="#"
className="block px-4 py-2 text-sm text-gray-700"
role="menuitem"
>
Your Profile
</a>
<a
href="#"
className="block px-4 py-2 text-sm text-gray-700"
role="menuitem"
>
Settings
</a>
<a
href="/logout"
className="block px-4 py-2 text-sm text-gray-700"
role="menuitem"
>
Sign out
</a>
</div>
</div>
</div>
</div>
</div>

{/* Mobile Menu */}
<div
className={`${isMobileMenuOpen ? "block" : "hidden"} sm:hidden`}
id="mobile-menu"
>
<div className="px-2 pt-2 pb-3 space-y-1">
{/* Mobile Navigation Links */}
<a
href="/"
className={`${
props.currentPage === "dashboard"
? "bg-gray-900 text-white"
: "text-gray-200 hover:bg-gray-700 hover:text-white"
} block px-3 py-2 rounded-md text-base font-medium`}
>
Dashboard
</a>
<a
href="/client/buses"
className={`${
props.currentPage === "busservices"
? "bg-gray-900 text-white"
: "text-gray-200 hover:bg-gray-700 hover:text-white"
} block px-3 py-2 rounded-md text-base font-medium`}
>
Bus Services
</a>
<a
href="/client/access"
className={`${
props.currentPage === "access"
? "bg-gray-900 text-white"
: "text-gray-200 hover:bg-gray-700 hover:text-white"
} block px-3 py-2 rounded-md text-base font-medium`}
>
Access
</a>
</div>
</div>
</nav>
);
};
export default Header;
Loading

0 comments on commit 5c65ac6

Please sign in to comment.