Readd access token to session

This commit is contained in:
GHOSCHT 2024-05-19 22:00:24 +02:00
parent 857b44d810
commit aa06655c6a
Signed by: ghoscht
GPG key ID: 2C2C1C62A5388E82
2 changed files with 29 additions and 26 deletions

View file

@ -1,4 +1,4 @@
import NextAuth, { type DefaultSession } from "next-auth";
import NextAuth from "next-auth";
import Authentik from "next-auth/providers/authentik";
import { PrismaAdapter } from "@auth/prisma-adapter"
import { PrismaClient } from "@prisma/client"
@ -36,6 +36,10 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
where: { userId: user.id, provider: "authentik" },
})
if (!authentikAccount.expires_at || !authentikAccount.refresh_token) {
throw "Expiry time or refresh token not set";
}
if (authentikAccount.expires_at * 1000 < Date.now()) {
console.info("refreshing token");
try {
@ -73,7 +77,7 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
session.error = "RefreshAccessTokenError"
}
}
return session
return { ...session, access_token: authentikAccount.access_token }
}
}
});

View file

@ -1,28 +1,27 @@
import { signIn,signOut, auth } from "auth"
import { signIn, signOut, auth } from "auth"
export default async function Home() {
let session = await auth();
let session = await auth();
console.log(session?.access_token)
return (
<>
<h1>Hello {session?.user?.name}!</h1>
<form
action={async () => {
"use server"
await signIn("authentik")
}}
>
<button type="submit">Sign in</button>
</form>
<form
action={async () => {
"use server"
await signOut()
}}
>
<button type="submit">Sign out</button>
</form>
</>
);
return (
<>
<h1>Hello {session?.user?.name}!</h1>
<form
action={async () => {
"use server"
await signIn("authentik")
}}
>
<button type="submit">Sign in</button>
</form>
<form
action={async () => {
"use server"
await signOut()
}}
>
<button type="submit">Sign out</button>
</form>
</>
);
}