Skip to content

Commit eea7797

Browse files
committed
Admin dashboard med redirect på login
1 parent 3660e84 commit eea7797

File tree

11 files changed

+72
-31
lines changed

11 files changed

+72
-31
lines changed

backend/src/main/kotlin/no/nais/cloud/testnais/sandbox/bachelorurlforkorter/UrlForkorterApi.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ fun startAppServer(config: Config) {
3636
path("api") {
3737
post("sjekk", UrlForkorterController::sjekk, Rolle.Alle)
3838
post("forkort", UrlForkorterController::forkort, Rolle.InternNavInnlogget)
39-
post("slett", UrlForkorterController::slett, Rolle.Alle)
40-
get("hentalle", UrlForkorterController::hentAlleMedMetadata, Rolle.Alle)
39+
post("slett", UrlForkorterController::slett, Rolle.InternNavInnlogget)
40+
get("hentalle", UrlForkorterController::hentAlleMedMetadata, Rolle.InternNavInnlogget)
4141
post("logginn", Auth::loggInn, Rolle.Alle)
4242
get("bruker", Auth::hentInnloggetBruker, Rolle.Alle)
4343
post("loggut", Auth::loggUt, Rolle.Alle)
4444
}
4545
get("{korturl}") { ctx ->
46-
if (ctx.pathParam("korturl") == "index.html") {
46+
if (ctx.pathParam("korturl") == "index.html" || ctx.pathParam("korturl") == "dashboard") {
4747
val asset =
4848
UrlForkorterController::class.java.getResourceAsStream("/public/index.html")
4949
if (asset != null) {

frontend/src/App.tsx

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {BrowserRouter, Navigate, Route, Routes} from "react-router-dom";
2-
import LandingPage from "./pages/LandingPage.tsx"
2+
import LandingPage from "./pages/LandingPage/LandingPage.tsx"
33
import "./App.css"
44
import Header from "./components/Header/Header.tsx";
55
import {useAuth} from "./util/hooks/useAuth.ts";
66
import {ReactNode} from "react";
7+
import DashboardPage from "./pages/DashboardPage/DashboardPage.tsx";
78

89
export default function App() {
910

@@ -12,9 +13,9 @@ export default function App() {
1213
<Header/>
1314
<Routes>
1415
<Route path={"/"} element={<LandingPage/>}/>
15-
<Route path={"/user/:id"} element={
16+
<Route path={"/dashboard"} element={
1617
<ProtectedRoute>
17-
<div></div>
18+
<DashboardPage/>
1819
</ProtectedRoute>
1920
}></Route>
2021
</Routes>
@@ -23,6 +24,9 @@ export default function App() {
2324
}
2425

2526
function ProtectedRoute({children}: { children: ReactNode }) {
26-
const {isLoggedIn} = useAuth();
27+
const {isLoggedIn, loading} = useAuth();
28+
if (loading) {
29+
return <div>Loading...</div>;
30+
}
2731
return isLoggedIn ? children : <Navigate to="/"/>;
2832
}

frontend/src/components/Header/Header.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import {useAuth} from "../../util/hooks/useAuth.ts";
77

88
export default function Header() {
99
const [showLogin, setShowLogin] = useState(false);
10-
const {isLoggedIn, user, logout} = useAuth();
10+
const {isLoggedIn, user, loading, logout} = useAuth();
1111

1212
return (
1313
<HeaderContainer>
1414
<LogoText>n.av</LogoText>
15-
{isLoggedIn ? (
15+
{loading ? (
16+
<DisplayUser>Loading...</DisplayUser>
17+
) : isLoggedIn ? (
1618
<div style={{display: "flex", alignItems: "center"}}>
1719
<DisplayUser>Velkommen, {user}!</DisplayUser>
1820
<Button text="Logg ut" onClick={logout}/>

frontend/src/components/LoginForm/LoginForm.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {FormEvent, useState} from "react";
22
import { LoginContainer, LoginInput, LoginButton } from "./loginform.style.ts";
33
import Link from "../shared/Link/Link.tsx";
44
import {apiRequest} from "../../util/api/apiRequest.ts";
5+
import {useNavigate} from "react-router-dom";
56

67
interface LoginFormProps {
78
onLogin: () => void;
@@ -11,6 +12,7 @@ export default function LoginForm({ onLogin }: LoginFormProps) {
1112
const [username, setUsername] = useState("");
1213
const [password, setPassword] = useState("");
1314
const [error, setError] = useState<string | null>(null);
15+
const navigate = useNavigate();
1416

1517
const handleSubmit = (e: FormEvent) => {
1618
e.preventDefault();
@@ -21,10 +23,11 @@ export default function LoginForm({ onLogin }: LoginFormProps) {
2123

2224
apiRequest<{ token: string }>("logginn", "POST", body).then(() => {
2325
onLogin();
26+
navigate("/dashboard");
2427
}).catch(error => {
2528
setError(error.message);
2629
console.error(error);
27-
});
30+
})
2831
};
2932

3033
return (

frontend/src/components/ShowAll/ShowAll.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ interface UrlData {
2424

2525
type SortColumn = "createdAt" | "clicks" | null;
2626

27-
export default function ShowAllUrls() {
27+
export default function ShowAll() {
2828
const [urls, setUrls] = useState<UrlData[]>([]);
2929
const [loading, setLoading] = useState(true);
3030
const [error, setError] = useState<string | null>(null);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {} from "./dashboardpage.style.ts";
2+
import {Main} from "../pages.style.ts";
3+
import SearchShortUrl from "../../components/SearchShortUrl.tsx";
4+
import CreateShortUrl from "../../components/CreateShortUrl.tsx";
5+
import ShowAll from "../../components/ShowAll/ShowAll.tsx";
6+
7+
export default function DashboardPage() {
8+
9+
return (
10+
<Main>
11+
<SearchShortUrl/>
12+
<CreateShortUrl/>
13+
<ShowAll/>
14+
</Main>
15+
)
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import styled from "styled-components";
2+
3+
export const WelcomeText = styled.h2`
4+
color: var(--theme-color);
5+
font-size: 60px;
6+
`

frontend/src/pages/LandingPage.tsx frontend/src/pages/LandingPage/LandingPage.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import {InfoText, LogoText, Main, WelcomeText} from "./pages.style.ts";
2-
import SearchShortUrl from "../components/SearchShortUrl.tsx";
1+
import {InfoText, LogoText, WelcomeText} from "./landingpage.style.ts";
2+
import {Main} from "../pages.style.ts";
3+
import SearchShortUrl from "../../components/SearchShortUrl.tsx";
34

45
export default function LandingPage() {
56

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import styled from "styled-components";
2+
3+
export const WelcomeText = styled.h2`
4+
color: var(--theme-color);
5+
font-size: 60px;
6+
`
7+
8+
export const InfoText = styled.p`
9+
color: #222;
10+
text-align: center;
11+
line-height: 1.4rem;
12+
`
13+
14+
export const LogoText = styled.span`
15+
font-weight: bold;
16+
color: var(--theme-color);
17+
`

frontend/src/pages/pages.style.ts

-16
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,4 @@ export const Main = styled.main`
77
flex-direction: column;
88
place-items: center;
99
place-content: center;
10-
`
11-
12-
export const WelcomeText = styled.h2`
13-
color: var(--theme-color);
14-
font-size: 60px;
15-
`
16-
17-
export const InfoText = styled.p`
18-
color: #222;
19-
text-align: center;
20-
line-height: 1.4rem;
21-
`
22-
23-
export const LogoText = styled.span`
24-
font-weight: bold;
25-
color: var(--theme-color);
2610
`

frontend/src/util/hooks/useAuth.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,45 @@
11
import {useState, useEffect} from "react";
22
import {apiRequest} from "../api/apiRequest.ts";
3+
import {useNavigate} from "react-router-dom";
34

45
export function useAuth() {
56
const [isLoggedIn, setIsLoggedIn] = useState<boolean>(false);
67
const [user, setUser] = useState<string | null>(null);
8+
const [loading, setLoading] = useState(true);
9+
const navigate = useNavigate();
710

811
useEffect(() => {
912
checkLoginStatus()
1013
}, []);
1114

1215
const checkLoginStatus = () => {
16+
setLoading(true);
1317
apiRequest<{ username: string }>("bruker", "GET").then((data) => {
1418
if (data && data.username) {
1519
setIsLoggedIn(true);
1620
setUser(data.username);
21+
console.log("Logget inn som: " + data.username + isLoggedIn);
1722
} else {
1823
setIsLoggedIn(false);
1924
setUser(null);
2025
}
2126
}).catch((error) => {
22-
console.error("Klarte ikke å hente innloggetstatus:", error.message);
27+
console.error("Innloggetstatus:", error.message);
2328
setIsLoggedIn(false);
2429
setUser(null);
30+
}).finally(() => {
31+
setLoading(false);
2532
});
2633
}
2734

2835
const logout = async () => {
2936
apiRequest<void>("loggut", "POST").then(() => {
3037
setIsLoggedIn(false);
3138
setUser(null);
39+
navigate("/")
3240
}).catch((error) => {
3341
console.error("Klarte ikke å logge ut:", error.message);
3442
});
3543
}
36-
return {isLoggedIn, user, logout};
44+
return {isLoggedIn, user, loading, checkLoginStatus, logout};
3745
}

0 commit comments

Comments
 (0)