Skip to content
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
13 changes: 1 addition & 12 deletions frontend/__tests__/Navbar/Navbar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,9 @@ describe("GlobalNavbar", () => {
AuthStatus.Authorized,
);

const mock = vi.fn().mockImplementation(authService.getUser);
mock.mockImplementationOnce(() => {
return {
id: 1,
username: "test",
refreshToken: "test",
profileImage: "",
};
});

render(<GlobalNavbar />);
const avatar = screen.getByAltText("Profile") as HTMLImageElement;
const avatar = document.querySelector(".bi-file-person-fill");
expect(avatar).toBeInTheDocument();
expect(avatar.src).toContain("/img_avatar.png");
});

it("renders user avatar when authorized and profileImage exists", () => {
Expand Down
39 changes: 12 additions & 27 deletions frontend/components/Navbar/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,11 @@ import Export from "./Export";
import Image from "next/image";
import Searchbar from "./Searchbar";
import navbarView from "styles/navbar.module.scss";
import { useEffect, useState } from "react";
import AccountModal from "./AccountModal";

const GlobalNavbar: React.FC = () => {
const userAuth = useAuth();
const user = authService.getUser();
let [isMobile, setIsMobile] = useState<boolean | null>(null);

useEffect(() => {
setIsMobile(window.innerWidth <= 767.98);

const checkWindowWidth = () => {
setIsMobile(window.innerWidth <= 767.98);
};

window.addEventListener("resize", checkWindowWidth);
}, []);

const router = useRouter();
function authButton() {
if (userAuth == AuthStatus.Unauthorized || userAuth === undefined) {
Expand Down Expand Up @@ -75,20 +62,18 @@ const GlobalNavbar: React.FC = () => {
className="bg-body-tertiary"
>
<Container className={navbarView.navContainer}>
{isMobile ? (
<Navbar.Brand
onClick={() => router.push("/")}
className={` ${navbarView.navBrand}`}
>
<Image
src="/basic-f-v2-dark-mode-v2-fav.png"
width="38"
height="30"
className="d-inline-block align-top"
alt="FindFirst Logo"
/>
</Navbar.Brand>
) : null}
<Navbar.Brand
onClick={() => router.push("/")}
className={` ${navbarView.navBrand}`}
>
<Image
src="/basic-f-v2-dark-mode-v2-fav.png"
width="38"
height="30"
className="d-inline-block align-top"
alt="FindFirst Logo"
/>
</Navbar.Brand>

{/* Search bar stays visible always */}
{userAuth === AuthStatus.Authorized ? <Searchbar /> : null}
Expand Down
5 changes: 5 additions & 0 deletions frontend/vitestSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap-icons/font/bootstrap-icons.min.css";
loadEnvConfig(process.cwd());

vi.mock("next/font/google", () => ({
Libre_Baskerville: vi.fn(() => ({ className: "mock-font" })),
Inter: vi.fn(() => ({ className: "mock-font" })),
}));

vi.mock("next/navigation", () => {
const actual = vi.importActual("next/navigation");
return {
Expand Down
5 changes: 3 additions & 2 deletions server/src/main/java/dev/findfirst/FindFirstApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public FilterRegistrationBean<CorsFilter> simpleCorsFilter() {
config.setAllowCredentials(true);
// *** URL below needs to match the Vue client URL and port ***
// Local host and 127.0.0.1 are the same
config.setAllowedOrigins(Arrays.asList("https://localhost:3000", "http://localhost:3000",
"https://findfirst.dev", "http://localhost", "http://127.0.0.1"));
config.setAllowedOriginPatterns(Arrays.asList("https://localhost:3000", "http://localhost:3000",
"https://findfirst.dev", "http://localhost", "http://127.0.0.1",
"chrome-extension://*", "moz-extension://*"));
config.setAllowedMethods(Collections.singletonList("*"));
config.setAllowedHeaders(Collections.singletonList("*"));
source.registerCorsConfiguration("/**", config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@

import dev.findfirst.security.jwt.UserAuthenticationToken;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.stereotype.Component;

@Component
public class UserContext {

public int getUserId() {
return ((UserAuthenticationToken) SecurityContextHolder.getContext().getAuthentication())
.getUserId();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth instanceof UserAuthenticationToken uat) {
return uat.getUserId();
}
if (auth instanceof JwtAuthenticationToken jat) {
Number userId = jat.getToken().getClaim("userId");
return userId.intValue();
}
throw new IllegalStateException("Unexpected authentication type: " + auth.getClass());
}
}
Loading