Skip to content

Commit

Permalink
fix(ts): make expires serializable externally
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsorban44 committed Jul 12, 2021
1 parent 59b4026 commit 7b4a3e0
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/server/routes/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default async function session(req, res) {
image: user.image,
},
accessToken: session.accessToken,
expires: session.expires,
expires: session.expires.toISOString(),
}

// Pass Session through to the session callback
Expand Down
13 changes: 10 additions & 3 deletions types/adapters.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export interface AdapterUser extends User {
emailVerified?: Date | null
}

export interface AdapterSession extends Omit<Session, "expires"> {
expires: Date
}

/**
* Using a custom adapter you can connect to any database backend or even several different databases.
* Custom adapters created and maintained by our community can be found in the adapters repository.
Expand Down Expand Up @@ -70,9 +74,12 @@ export interface Adapter {
providerId: string,
providerAccountId: string
): Awaitable<void>
createSession(user: AdapterUser): Awaitable<Session>
getSession(sessionId: string): Awaitable<Session | null>
updateSession(session: Session, force?: boolean): Awaitable<Session | null>
createSession(user: AdapterUser): Awaitable<AdapterSession>
getSession(sessionId: string): Awaitable<AdapterSession | null>
updateSession(
session: AdapterSession,
force?: boolean
): Awaitable<AdapterSession | null>
deleteSession(sessionId: string): Awaitable<void>
createVerificationRequest?(
identifier: string,
Expand Down
8 changes: 3 additions & 5 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// Minimum TypeScript Version: 3.6

/// <reference types="node" />

import { Adapter } from "./adapters"
import { JWTOptions, JWT } from "./jwt"
import { AppProviders, Credentials } from "./providers"
Expand Down Expand Up @@ -431,13 +427,15 @@ export interface PagesOptions {
newUser: string
}

export type ISODateString = string

export interface DefaultSession extends Record<string, unknown> {
user?: {
name?: string | null
email?: string | null
image?: string | null
}
expires: Date
expires: ISODateString
}

/**
Expand Down
4 changes: 2 additions & 2 deletions types/tests/react.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const clientSession = {
image: "path/to/img",
},
accessToken: "123z",
expires: new Date(),
expires: "1234",
}

/**
Expand Down Expand Up @@ -90,7 +90,7 @@ client.SessionProvider({
client.SessionProvider({
children: null,
session: {
expires: new Date(),
expires: "1234",
},
baseUrl: "https://foo.com",
basePath: "/",
Expand Down
6 changes: 3 additions & 3 deletions types/tests/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Providers, { OAuthConfig } from "next-auth/providers"
import { Adapter } from "next-auth/adapters"
import { Adapter, AdapterSession, AdapterUser } from "next-auth/adapters"
import NextAuth, * as NextAuthTypes from "next-auth"
import { IncomingMessage, ServerResponse } from "http"
import { Socket } from "net"
Expand Down Expand Up @@ -40,13 +40,13 @@ const simpleConfig = {
],
}

const exampleUser: NextAuthTypes.User = {
const exampleUser: AdapterUser = {
name: "",
image: "",
email: "",
}

const exampleSession: NextAuthTypes.Session = {
const exampleSession: AdapterSession = {
expires: new Date(),
}

Expand Down

0 comments on commit 7b4a3e0

Please sign in to comment.