Readd access token to session
This commit is contained in:
parent
857b44d810
commit
aa06655c6a
2 changed files with 29 additions and 26 deletions
8
auth.ts
8
auth.ts
|
@ -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 }
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -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>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue