metamigo: do nextauth v3 -> v4 upgrades

This commit is contained in:
Abel Luck 2023-06-06 10:28:29 +00:00
parent a33f80c497
commit 45f8cb1234
13 changed files with 158 additions and 123 deletions

View file

@ -1 +0,0 @@
METAMIGO_CONFIG=.metamigo.local.json

View file

@ -1,8 +1,5 @@
/* eslint-disable unicorn/no-null */
/* eslint-disable max-params */
import type { Adapter } from "next-auth/adapters";
// @ts-expect-error: Missing export
import type { AppOptions } from "next-auth";
import type { Adapter, AdapterAccount, AdapterSession, AdapterUser } from "next-auth/adapters";
import * as Wreck from "@hapi/wreck";
import * as Boom from "@hapi/boom";
@ -70,7 +67,7 @@ export const MetamigoAdapter = (config: IAppConfig): Adapter => {
json: "force",
});
async function getAdapter(_appOptions: AppOptions) {
function getAdapter(): Adapter {
async function createUser(profile: Profile) {
try {
if (!profile.createdBy) profile = { ...profile, createdBy: "nextauth" };
@ -106,19 +103,17 @@ export const MetamigoAdapter = (config: IAppConfig): Adapter => {
}
}
async function getUserByProviderAccountId(
providerId: string,
providerAccountId: string
) {
async function getUserByAccount({ providerAccountId, provider }: { providerAccountId: string, provider: string }) {
try {
const { payload } = await wreck.get(
`getUserByProviderAccountId/${providerId}/${providerAccountId}`
`getUserByAccount/${provider}/${providerAccountId}`
);
return payload;
} catch (error) {
if (Boom.isBoom(error, 404)) return null;
throw new Error("GET_USER_BY_PROVIDER_ACCOUNT_ID");
console.log(error)
throw new Error("GET_USER_BY_ACCOUNT");
}
}
@ -135,51 +130,39 @@ export const MetamigoAdapter = (config: IAppConfig): Adapter => {
}
async function linkAccount(
userId: string,
providerId: string,
providerType: string,
providerAccountId: string,
refreshToken: string,
accessToken: string,
accessTokenExpires: number
account: AdapterAccount
) {
try {
const payload = {
userId,
providerId,
providerType,
providerAccountId: `${providerAccountId}`, // must be a string
refreshToken,
accessToken,
accessTokenExpires,
};
await wreck.put("linkAccount", {
payload,
});
} catch {
await wreck.put("linkAccount", {payload: account} as any );
} catch(error) {
console.log(error);
throw new Error("LINK_ACCOUNT_ERROR");
}
}
async function createSession(user: User) {
try {
const { payload } = await wreck.post("createSession", {
const { payload }: {payload: AdapterSession} = await wreck.post("createSession", {
payload: user,
});
payload.expires = new Date(payload.expires)
return payload;
} catch {
} catch(error) {
console.log(error)
throw new Error("CREATE_SESSION_ERROR");
}
}
async function getSession(sessionToken: string) {
async function getSessionAndUser(sessionToken: string) {
try {
const { payload } = await wreck.get(`getSession/${sessionToken}`);
return payload;
const {payload}: {payload: any} = await wreck.get(`getSessionAndUser/${sessionToken}`);
const { session, user }: {session: AdapterSession, user: AdapterUser} = payload;
session.expires = new Date(session.expires)
return {session, user}
} catch (error) {
console.log(error)
if (Boom.isBoom(error, 404)) return null;
throw new Error("GET_SESSION_ERROR");
throw new Error("GET_SESSION_AND_USER_ERROR");
}
}
@ -213,21 +196,18 @@ export const MetamigoAdapter = (config: IAppConfig): Adapter => {
createUser,
getUser,
getUserByEmail,
getUserByProviderAccountId,
getUserByAccount,
updateUser,
// deleteUser,
linkAccount,
// unlinkAccount,
createSession,
getSession,
getSessionAndUser,
updateSession,
deleteSession,
// @ts-expect-error: Type error
} as AdapterInstance<Profile, User, Session, unknown>;
}
return {
// @ts-expect-error: non-existent property
getAdapter,
};
return getAdapter();
};

View file

@ -7,6 +7,7 @@ import Cognito from "next-auth/providers/cognito";
import { loadConfig, IAppConfig } from "@digiresilience/metamigo-config";
import { MetamigoAdapter } from "../../../lib/nextauth-adapter";
import { CloudflareAccessProvider } from "../../../lib/cloudflare";
import { AdapterSession, AdapterUser } from "next-auth/adapters";
const nextAuthOptions = (config: IAppConfig, req: NextApiRequest) => {
const { nextAuth, cfaccess } = config;
@ -72,11 +73,6 @@ const nextAuthOptions = (config: IAppConfig, req: NextApiRequest) => {
providers,
adapter,
callbacks: {
async session(session: any, token: any) {
// make the user id available in the react client
session.user.id = token.userId;
return session;
},
async jwt(token: any, user: any) {
const isSignIn = Boolean(user);
// Add auth_time to token on signin in