Skip to content

Commit

Permalink
fix: next-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon committed Apr 19, 2024
1 parent 85ef423 commit 0d7fcf7
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 41 deletions.
15 changes: 0 additions & 15 deletions pwa/app/auth.tsx
@@ -1,5 +1,4 @@
import { type TokenSet } from "@auth/core/types";
import { signOut as logout, type SignOutParams } from "next-auth/react";
import NextAuth, { type Session as DefaultSession, type User } from "next-auth";
import KeycloakProvider from "next-auth/providers/keycloak";

Expand Down Expand Up @@ -27,20 +26,6 @@ interface Account {
refresh_token: string
}

interface SignOutResponse {
url: string
}

export async function signOut<R extends boolean = true>(
session: DefaultSession,
options?: SignOutParams<R>
): Promise<R extends true ? undefined : SignOutResponse> {
return await logout({
// @ts-ignore
callbackUrl: `${OIDC_SERVER_URL}/protocol/openid-connect/logout?id_token_hint=${session.idToken}&post_logout_redirect_uri=${options?.callbackUrl ?? window.location.origin}`,
});
}

export const { handlers: { GET, POST }, auth } = NextAuth({
callbacks: {
// @ts-ignore
Expand Down
2 changes: 2 additions & 0 deletions pwa/components/admin/Admin.tsx
@@ -1,3 +1,5 @@
"use client";

import Head from "next/head";
import { useContext, useRef, useState } from "react";
import { type DataProvider, Layout, type LayoutProps, localStorageStore, resolveBrowserLocale } from "react-admin";
Expand Down
10 changes: 7 additions & 3 deletions pwa/components/admin/authProvider.tsx
@@ -1,7 +1,8 @@
import { AuthProvider } from "react-admin";
import { signIn } from "next-auth/react";
import { signIn, signOut } from "next-auth/react";

import { auth, signOut } from "../../app/auth";
import { auth } from "../../app/auth";
import { OIDC_SERVER_URL } from "../../config/keycloak";

const authProvider: AuthProvider = {
// Nothing to do here, this function will never be called
Expand All @@ -12,7 +13,10 @@ const authProvider: AuthProvider = {
return;
}

await signOut(session, {callbackUrl: window.location.origin});
await signOut(/*{
// @ts-ignore
callbackUrl: `${OIDC_SERVER_URL}/protocol/openid-connect/logout?id_token_hint=${session.idToken}&post_logout_redirect_uri=${window.location.origin}`,
}*/);
},
checkError: async (error) => {
const session = await auth();
Expand Down
9 changes: 6 additions & 3 deletions pwa/components/common/Header.tsx
@@ -1,12 +1,12 @@
"use client";

import { signIn, useSession } from "next-auth/react";
import { signIn, signOut, useSession } from "next-auth/react";
import { usePathname } from "next/navigation";
import Link from "next/link";
import PersonOutlineIcon from "@mui/icons-material/PersonOutline";
import FavoriteBorderIcon from "@mui/icons-material/FavoriteBorder";

import { signOut } from "../../app/auth";
import { OIDC_SERVER_URL } from "../../config/keycloak";

export const Header = () => {
const pathname = usePathname();
Expand All @@ -31,7 +31,10 @@ export const Header = () => {
{status === "authenticated" && (
<a href="#" className="font-semibold text-gray-900" role="menuitem" onClick={(e) => {
e.preventDefault();
signOut(session, {callbackUrl: `${window.location.origin}/books`});
signOut({
// @ts-ignore
callbackUrl: `${OIDC_SERVER_URL}/protocol/openid-connect/logout?id_token_hint=${session.idToken}&post_logout_redirect_uri=${window.location.origin}/books`,
});
}}>
Sign out
</a>
Expand Down
1 change: 1 addition & 0 deletions pwa/package.json
Expand Up @@ -24,6 +24,7 @@
"formik": "^2.4.5",
"next": "^14.2.2",
"next-auth": "5.0.0-beta.16",
"picocolors": "^1.0.0",
"postcss": "^8.4.38",
"ra-i18n-polyglot": "^4.16.15",
"ra-language-english": "^4.16.15",
Expand Down
3 changes: 3 additions & 0 deletions pwa/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 24 additions & 20 deletions pwa/utils/review.ts
Expand Up @@ -17,27 +17,31 @@ export const usePermission = (review: Review, session: Session|null): boolean =>
}

(async () => {
const response = await fetch(`${OIDC_SERVER_URL}/protocol/openid-connect/token`, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: `Bearer ${session?.accessToken}`,
},
body: new URLSearchParams({
grant_type: "urn:ietf:params:oauth:grant-type:uma-ticket",
audience: OIDC_AUTHORIZATION_CLIENT_ID,
response_mode: "decision",
permission_resource_format: "uri",
permission_resource_matching_uri: "true",
// @ts-ignore
permission: review["@id"].toString(),
}),
method: "POST",
});
const permission: Permission = await response.json();
console.log(permission);
try {
const response = await fetch(`${OIDC_SERVER_URL}/protocol/openid-connect/token`, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: `Bearer ${session?.accessToken}`,
},
body: new URLSearchParams({
grant_type: "urn:ietf:params:oauth:grant-type:uma-ticket",
audience: OIDC_AUTHORIZATION_CLIENT_ID,
response_mode: "decision",
permission_resource_format: "uri",
permission_resource_matching_uri: "true",
// @ts-ignore
permission: review["@id"].toString(),
}),
method: "POST",
});
const permission: Permission = await response.json();

if (permission.result) {
grant(true);
if (permission.result) {
grant(true);
}
} catch (error) {
console.error(error);
grant(false);
}
})();
}, [review]);
Expand Down

0 comments on commit 0d7fcf7

Please sign in to comment.