import type * as Hapi from "@hapi/hapi";
import NextAuthPlugin from "@digiresilience/hapi-nextauth";
import { NextAuthAdapter } from "@digiresilience/metamigo-common";
import { IAppConfig } from "@digiresilience/metamigo-config";

export const registerNextAuth = async (
  server: Hapi.Server,
  config: IAppConfig
): Promise<void> => {
  const nextAuthAdapterFactory: any = (request: Hapi.Request) =>
    new NextAuthAdapter(request.db());

  await server.register({
    plugin: NextAuthPlugin,
    options: {
      nextAuthAdapterFactory,
      sharedSecret: config.nextAuth.secret,
    },
  });
};
