Skip to content

Commit 9362a78

Browse files
committed
JWT tokens working with oidc somewhat working
1 parent 8d91c93 commit 9362a78

File tree

8 files changed

+95
-55
lines changed

8 files changed

+95
-55
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use client";
2+
import Skeleton from "@/components/Skeleton";
3+
import { userManager } from "@/lib/auth";
4+
import { useEffect } from "react";
5+
import { AuthProvider, AuthProviderProps, useAuth } from "react-oidc-context";
6+
import { permanentRedirect } from "next/navigation";
7+
8+
const config = {
9+
userManager,
10+
onSigninCallback: (user) => {
11+
console.log(user);
12+
permanentRedirect("/review");
13+
},
14+
} satisfies AuthProviderProps;
15+
16+
export default function AuthLayout({
17+
children,
18+
}: Readonly<{
19+
children: React.ReactNode;
20+
}>) {
21+
return <AuthProvider {...config}>{children}</AuthProvider>;
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use client";
2+
import Skeleton from "@/components/Skeleton";
3+
import { cn } from "@/lib/utils";
4+
import { useEffect } from "react";
5+
import { useAuth } from "react-oidc-context";
6+
7+
/*
8+
On error with fetching data
9+
try {
10+
userManager.signinSilent()
11+
} catch (error) {
12+
userManager.signinRedirect()
13+
}
14+
*/
15+
16+
export default function Review() {
17+
const auth = useAuth();
18+
19+
return (
20+
<div id="review" className={cn("mx-[20%]")}>
21+
{auth.isLoading ? <Skeleton /> : <ReviewPage />}
22+
</div>
23+
);
24+
}
25+
26+
function ReviewPage() {
27+
const auth = useAuth();
28+
29+
return (
30+
<p>
31+
Secret data that needs authentication {auth.isAuthenticated}{" "}
32+
{auth.user?.profile.email}
33+
</p>
34+
);
35+
}

frontend/review-nextjs/app/(header)/review/page.tsx

-8
This file was deleted.

frontend/review-nextjs/app/layout.tsx

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
import type { Metadata } from "next";
22
import "./globals.css";
33
import { cn } from "@/lib/utils";
4-
import { AuthProvider } from "react-oidc-context";
5-
import Footer from "../components/Footer";
4+
import Footer from "@/components/Footer";
65

76
export const metadata: Metadata = {
87
title: "Penn Course Review",
98
description: "Made by Penn Labs",
109
};
1110

12-
const oidcConfig = {
13-
authority: "<your authority>",
14-
clientId: "<your client id>",
15-
redirectUri: "<your redirect uri>",
16-
// ...
17-
};
18-
1911
export default function RootLayout({
2012
children,
2113
}: Readonly<{
@@ -24,10 +16,8 @@ export default function RootLayout({
2416
return (
2517
<html lang="en">
2618
<body className={cn("antialiased", "flex", "flex-col")}>
27-
<AuthProvider {...oidcConfig}>
28-
{children}
29-
<Footer />
30-
</AuthProvider>
19+
{children}
20+
<Footer />
3121
</body>
3222
</html>
3323
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function Skeleton() {
2+
return (
3+
<div>
4+
<h1>Skeleton data</h1>
5+
</div>
6+
);
7+
}

frontend/review-nextjs/lib/api.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
const HOST_URL = process.env.NODE_ENV === 'development'
1+
export const BASE_URL = process.env.NODE_ENV === 'development'
22
? 'http://localhost:3000'
3-
: process.env.NEXT_PUBLIC_HOST_URL;
3+
: process.env.NEXT_PUBLIC_BASE_URL;
4+
5+
export const CLIENT_ID = process.env.NEXT_PUBLIC_CLIENT_ID ?? "";
46

57
export const doAPIRequest = (path: string, options = {}): Promise<Response> =>
68
fetch(`/api${path}`, options);
79

810
export function getLogoutUrl(): string {
911
return `/accounts/logout/?next=${encodeURIComponent(
10-
`${HOST_URL}/logout`
12+
`${BASE_URL}/logout`
1113
)}`;
1214
}
1315

@@ -16,12 +18,3 @@ export function getLogoutUrl(): string {
1618
// window.location.pathname
1719
// )}`;
1820
// }
19-
20-
export async function checkAuth(): Promise<boolean> {
21-
const res = await fetch("/accounts/me/");
22-
if (res.status < 300 && res.status >= 200) {
23-
return true;
24-
} else {
25-
return false;
26-
}
27-
}

frontend/review-nextjs/lib/auth.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"use client";
2+
import { UserManager, WebStorageStateStore } from "oidc-client-ts";
3+
import { BASE_URL, CLIENT_ID } from "@/lib/api";
4+
5+
export const userManager = new UserManager({
6+
authority: "https://platform.pennlabs.org/",
7+
client_id: CLIENT_ID,
8+
redirect_uri: `${BASE_URL}/callback`,
9+
scope: "openid read",
10+
metadataUrl:
11+
"https://platform.pennlabs.org/accounts/.well-known/openid-configuration/",
12+
metadata: {
13+
authorization_endpoint:
14+
"https://platform.pennlabs.org/accounts/authorize/",
15+
token_endpoint: "https://platform.pennlabs.org/accounts/token/",
16+
jwks_uri:
17+
"https://platform.pennlabs.org/accounts/.well-known/jwks.json",
18+
},
19+
automaticSilentRenew: true,
20+
includeIdTokenInSilentRenew: true,
21+
userStore: new WebStorageStateStore({ store: window?.localStorage }),
22+
});
23+
console.log(userManager);

frontend/review-nextjs/middleware.ts

-22
This file was deleted.

0 commit comments

Comments
 (0)