Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoalbanese committed Nov 13, 2023
1 parent ac00576 commit cfb8755
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kirimase",
"version": "0.0.31",
"version": "0.0.32",
"description": "A Rails-like CLI for building full-stack Next.js apps faster",
"main": "index.js",
"type": "module",
Expand Down
117 changes: 114 additions & 3 deletions src/commands/add/auth/next-auth/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "../../../filePaths/index.js";

// 1. Create app/api/auth/[...nextauth].ts
export const apiAuthNextAuthTs = (
export const apiAuthNextAuthTsOld = (
providers: AuthProvider[],
dbType: DBType | null,
orm: ORMType
Expand Down Expand Up @@ -84,6 +84,29 @@ export { handler as GET, handler as POST };
`;
};

export const apiAuthNextAuthTs = () => {
const { shared } = getFilePaths();

return `import { DefaultSession } from "next-auth";
import NextAuth from "next-auth/next";
import { authOptions } from "${formatFilePath(shared.auth.authUtils, {
prefix: "alias",
removeExtension: true,
})}";
declare module "next-auth" {
interface Session {
user: DefaultSession["user"] & {
id: string;
};
}
}
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
`;
};

// 2. create lib/auth/Provider.tsx
export const libAuthProviderTsx = () => {
return `"use client";
Expand All @@ -100,7 +123,7 @@ export default function NextAuthProvider({ children }: Props) {
};

// 3. create lib/auth/utils.ts
export const libAuthUtilsTs = () => {
export const libAuthUtilsTsWithoutAuthOptions = () => {
const { "next-auth": nextAuth } = getFilePaths();
return `import { authOptions } from "${formatFilePath(
nextAuth.nextAuthApiRoute,
Expand All @@ -111,7 +134,7 @@ import { redirect } from "next/navigation";
export const getUserAuth = async () => {
const session = await getServerSession(authOptions);
return { session };
return { session } as AuthSession;
};
export const checkAuth = async () => {
Expand All @@ -121,6 +144,94 @@ export const checkAuth = async () => {
`;
};

export const libAuthUtilsTs = (
providers: AuthProvider[],
dbType: DBType | null,
orm: ORMType
) => {
const { shared } = getFilePaths();
const dbIndex = getDbIndexPath();
const providersToUse = providers.map((provider) => {
return {
name: provider,
providerKey: AuthProviders[provider].code,
website: AuthProviders[provider].website,
};
});

return `${
dbType !== null
? `import { db } from "${formatFilePath(dbIndex, {
prefix: "alias",
removeExtension: true,
})}";
${AuthDriver[orm].import}`
: ""
}
import { DefaultSession, getServerSession, NextAuthOptions } from "next-auth";
import { redirect } from "next/navigation";
import { env } from "${formatFilePath(shared.init.envMjs, {
prefix: "alias",
removeExtension: false,
})}"
${providersToUse
.map(
(provider) =>
`import ${capitalised(provider.name)}Provider from "next-auth/providers/${
provider.name
}";`
)
.join("\n")}
declare module "next-auth" {
interface Session {
user: DefaultSession["user"] & {
id: string;
};
}
}
export type AuthSession = {
session: {
user: {
id: string;
name?: string;
email?: string;
};
} | null;
};
export const authOptions: NextAuthOptions = {
${
dbType !== null
? `adapter: ${AuthDriver[orm].adapter}(db),`
: "// adapter: yourDBAdapterHere"
}
callbacks: {
session: ({ session, user }) => {
session.user.id = user.id;
return session;
},
},
providers: [
${providersToUse.map((provider) => provider.providerKey).join(",\n ")}
],
};
export const getUserAuth = async () => {
const session = await getServerSession(authOptions);
return { session } as AuthSession;
};
export const checkAuth = async () => {
const { session } = await getUserAuth();
if (!session) redirect("/api/auth/signin");
};
`;
};

// 4. create lib/db/schema/auth.ts
export const createDrizzleAuthSchema = (dbType: DBType) => {
const { provider } = readConfigFile();
Expand Down
4 changes: 2 additions & 2 deletions src/commands/add/auth/next-auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const addNextAuth = async (options?: InitOptions) => {
removeExtension: false,
prefix: "rootPath",
}),
apiAuthNextAuthTs(providers, driver, orm)
apiAuthNextAuthTs()
);

// 2. create lib/auth/Provider.tsx
Expand All @@ -76,7 +76,7 @@ export const addNextAuth = async (options?: InitOptions) => {
removeExtension: false,
prefix: "rootPath",
}),
libAuthUtilsTs()
libAuthUtilsTs(providers, driver, orm)
);

// 4. create lib/db/schema/auth.ts
Expand Down
4 changes: 2 additions & 2 deletions src/commands/add/misc/stripe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { AvailablePackage } from "../../../../types.js";
import { updateTRPCRouter } from "../../../generate/generators/trpcRoute.js";
import { createAccountPage } from "../../auth/shared/generators.js";
import { formatFilePath, getFilePaths } from "../../../filePaths/index.js";
import { libAuthUtilsTs } from "../../auth/next-auth/generators.js";
import { libAuthUtilsTsWithoutAuthOptions } from "../../auth/next-auth/generators.js";
import { updateRootSchema } from "../../../generate/generators/model/utils.js";
import { AuthSubTypeMapping } from "../../utils.js";

Expand Down Expand Up @@ -83,7 +83,7 @@ export const addStripe = async (packagesBeingInstalled: AvailablePackage[]) => {

const authUtilsExist = existsSync(authUtilsPath);
if (!authUtilsExist) {
createFile(authUtilsPath, libAuthUtilsTs());
createFile(authUtilsPath, libAuthUtilsTsWithoutAuthOptions());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { buildSchema } from "./commands/generate/index.js";
import { addPackage } from "./commands/add/index.js";

const program = new Command();
program.name("kirimase").description("Kirimase CLI").version("0.0.31");
program.name("kirimase").description("Kirimase CLI").version("0.0.32");

addCommonOptions(program.command("init"))
.description("initialise and configure kirimase within directory")
Expand Down

0 comments on commit cfb8755

Please sign in to comment.