Skip to content

Commit

Permalink
fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardo-forina committed Nov 21, 2023
1 parent 0614ae8 commit f47cd1e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 40 deletions.
4 changes: 4 additions & 0 deletions ui/app/[locale]/SessionRefresher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ export function SessionRefresher() {
useInterval(() => {
void doRefresh();
}, 1000 * 60);

// immediately trigger a check to deal with expired sessions when opening the app after a long time
void doRefresh();

return null;
}
3 changes: 2 additions & 1 deletion ui/app/[locale]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { AppLayoutProvider } from "@/app/[locale]/AppLayoutProvider";
import { AppSessionProvider } from "@/app/[locale]/AppSessionProvider";
import NextIntlProvider from "@/app/[locale]/NextIntlProvider";
import { SessionRefresher } from "@/app/[locale]/SessionRefresher";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";

import { authOptions } from "@/utils/authOptions";
import { getServerSession } from "next-auth";
import { getTranslations } from "next-intl/server";
import { notFound } from "next/navigation";
Expand Down
40 changes: 2 additions & 38 deletions ui/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,5 @@
import { logger } from "@/utils/logger";
import NextAuth, { AuthOptions } from "next-auth";
import { keycloak, refreshToken } from "./keycloak";

const log = logger.child({ module: "auth-route" });

export const authOptions: AuthOptions = {
providers: [keycloak],
callbacks: {
async jwt({ token, account }) {
// Persist the OAuth access_token and or the user id to the token right after signin
if (account) {
log.trace("account present, saving new token");
// Save the access token and refresh token in the JWT on the initial login
return {
access_token: account.access_token,
expires_at: account.expires_at,
refresh_token: account.refresh_token,
email: token.email,
name: token.name,
picture: token.picture,
sub: token.sub,
};
}

return refreshToken(token);
},
async session({ session, token }) {
// Send properties to the client, like an access_token from a provider.
log.trace(token, "Creating session from token");
return {
...session,
error: token.error,
accessToken: token.access_token,
};
},
},
};
import { authOptions } from "@/utils/authOptions";
import NextAuth from "next-auth";

const handler = NextAuth(authOptions);

Expand Down
38 changes: 38 additions & 0 deletions ui/utils/authOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { keycloak, refreshToken } from "@/app/api/auth/[...nextauth]/keycloak";

import { logger } from "@/utils/logger";
import { AuthOptions } from "next-auth";

const log = logger.child({ module: "auth" });
export const authOptions: AuthOptions = {
providers: [keycloak],
callbacks: {
async jwt({ token, account }) {
// Persist the OAuth access_token and or the user id to the token right after signin
if (account) {
log.trace("account present, saving new token");
// Save the access token and refresh token in the JWT on the initial login
return {
access_token: account.access_token,
expires_at: account.expires_at,
refresh_token: account.refresh_token,
email: token.email,
name: token.name,
picture: token.picture,
sub: token.sub,
};
}

return refreshToken(token);
},
async session({ session, token }) {
// Send properties to the client, like an access_token from a provider.
log.trace(token, "Creating session from token");
return {
...session,
error: token.error,
accessToken: token.access_token,
};
},
},
};
3 changes: 2 additions & 1 deletion ui/utils/session.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use server";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";

import { authOptions } from "@/utils/authOptions";
import { sealData, unsealData } from "iron-session";
import { getServerSession } from "next-auth";
import { cookies } from "next/headers";
Expand Down

0 comments on commit f47cd1e

Please sign in to comment.