Skip to content

Commit

Permalink
fix: signout properly (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
HazimAr authored Nov 19, 2024
1 parent d9f599f commit 74ffded
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions ui/src/app/api/auth/[...nextauth]/authOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ export const authOptions: NextAuthOptions = {

callbacks: {
async jwt({ token, account }: any) {
const nowTimeStamp = Math.floor(Date.now() / 1000);

if (account) {
token.decoded = jwtDecode(account.access_token);
token.access_token = account.access_token;
Expand All @@ -29,6 +27,8 @@ export const authOptions: NextAuthOptions = {

return token;
}

const nowTimeStamp = Math.floor(Date.now() / 1000);
if (nowTimeStamp < token.expires_at) {
return token;
}
Expand Down
2 changes: 1 addition & 1 deletion ui/src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { auth } from "../app/api/auth/[...nextauth]/authOptions";
export async function getClient(tenantId: string) {
const session = await auth();

if (!session) redirect("/api/auth/signout");
if (!session) redirect(`/api/auth/signin?callbackUrl=/`);

return new LittleHorseUserTasksApiClient({
baseUrl: process.env.LHUT_API_URL!,
Expand Down
17 changes: 17 additions & 0 deletions ui/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ const withAuth = nextAuth(async (req) => {
`${baseUrl}/api/auth/signin?callbackUrl=${currentPath}`,
);
}

// Call keycloak to check if token is valid
const keycloakResponse = await fetch(
`${process.env.KEYCLOAK_HOST}/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/userinfo`,
{
headers: {
Authorization: `Bearer ${token.access_token}`,
},
},
);

if (!keycloakResponse.ok) {
return NextResponse.redirect(
`${baseUrl}/api/auth/signin?callbackUrl=${currentPath}`,
);
}

// Redirect to tenant after login
if (currentPath === "/" && token.decoded.allowed_tenant) {
if (token.decoded.realm_access.roles.includes("lh-user-tasks-admin"))
Expand Down

0 comments on commit 74ffded

Please sign in to comment.