| 1 | import { AUTH_STRATEGY_OPTION } from "../config/options"; |
| 2 | |
| 3 | export const generateAuthFileContent = (options: AUTH_STRATEGY_OPTION[]) => { |
| 4 | const authStrategies = options.map((option) => ` ${option.key.toUpperCase()}: "${option.key}"`).join(",\n"); |
| 5 | const authStrategyImports = options |
| 6 | .map((option) => `import { ${option.key}Strategy } from "./auth_strategies/${option.key}.strategy";`) |
| 7 | .join("\n"); |
| 8 | const authStrategyUsage = options |
| 9 | .map((option) => `authenticator.use(${option.key}Strategy, AuthStrategies.${option.key.toUpperCase()});`) |
| 10 | .join("\n"); |
| 11 | |
| 12 | const authStrategiesOutput = ["export const AuthStrategies = {", authStrategies, "} as const;"]; |
| 13 | return { |
| 14 | content: [ |
| 15 | 'import { Authenticator } from "remix-auth";', |
| 16 | 'import { sessionStorage } from "~/services/session.server";', |
| 17 | 'import { AuthStrategies } from "~/services/auth_strategies";', |
| 18 | authStrategyImports, |
| 19 | "", |
| 20 | "export interface User {", |
| 21 | " // Add your own user properties here or extend with a type from your database", |
| 22 | "}", |
| 23 | "", |
| 24 | "export type AuthStrategy = typeof AuthStrategies[keyof typeof AuthStrategies];", |
| 25 | "", |
| 26 | "// Create an instance of the authenticator, pass a generic with what", |
| 27 | "// strategies will return and will store in the session", |
| 28 | "export const authenticator = new Authenticator<User>(sessionStorage);", |
| 29 | "", |
| 30 | "// Register your strategies below", |
| 31 | authStrategyUsage, |
| 32 | ].join("\n"), |
| 33 | authStrategiesOutput: authStrategiesOutput.join("\n"), |
| 34 | }; |
| 35 | }; |