From 675a9206f92154dd17cf379b018ef4020bae5087 Mon Sep 17 00:00:00 2001 From: Bereket Engida Date: Mon, 23 Sep 2024 23:19:22 +0300 Subject: [PATCH] feat: change social provider config to flat config --- demo/astro/.gitignore | 24 ---- demo/astro/.vscode/extensions.json | 4 - demo/astro/.vscode/launch.json | 11 -- demo/astro/README.md | 54 -------- demo/astro/astro.config.mjs | 10 -- demo/astro/package.json | 20 --- demo/astro/public/favicon.svg | 9 -- demo/astro/src/components/Card.astro | 61 --------- demo/astro/src/env.d.ts | 1 - demo/astro/src/layouts/Layout.astro | 50 ------- demo/astro/src/lib/auth-client.ts | 0 demo/astro/src/lib/auth.ts | 8 -- demo/astro/src/pages/api/auth/[...id].ts | 6 - demo/astro/src/pages/index.astro | 123 ------------------ demo/astro/tsconfig.json | 3 - demo/nextjs/app/layout.tsx | 7 +- demo/nextjs/app/page.tsx | 32 ++++- demo/nextjs/lib/auth.ts | 12 +- docs/content/docs/authentication/apple.mdx | 14 +- docs/content/docs/authentication/discord.mdx | 14 +- docs/content/docs/authentication/facebook.mdx | 14 +- docs/content/docs/authentication/github.mdx | 10 +- docs/content/docs/authentication/google.mdx | 8 +- docs/content/docs/authentication/spotify.mdx | 14 +- docs/content/docs/authentication/twitch.mdx | 14 +- docs/content/docs/authentication/twitter.mdx | 14 +- docs/content/docs/basic-usage.mdx | 12 +- docs/content/docs/installation.mdx | 12 +- .../better-auth/src/api/routes/callback.ts | 2 +- .../better-auth/src/api/routes/sign-in.ts | 2 +- packages/better-auth/src/init.ts | 15 ++- .../better-auth/src/social-providers/index.ts | 9 ++ .../src/test-utils/test-instance.ts | 16 +-- packages/better-auth/src/types/options.ts | 7 +- 34 files changed, 111 insertions(+), 501 deletions(-) delete mode 100644 demo/astro/.gitignore delete mode 100644 demo/astro/.vscode/extensions.json delete mode 100644 demo/astro/.vscode/launch.json delete mode 100644 demo/astro/README.md delete mode 100644 demo/astro/astro.config.mjs delete mode 100644 demo/astro/package.json delete mode 100644 demo/astro/public/favicon.svg delete mode 100644 demo/astro/src/components/Card.astro delete mode 100644 demo/astro/src/env.d.ts delete mode 100644 demo/astro/src/layouts/Layout.astro delete mode 100644 demo/astro/src/lib/auth-client.ts delete mode 100644 demo/astro/src/lib/auth.ts delete mode 100644 demo/astro/src/pages/api/auth/[...id].ts delete mode 100644 demo/astro/src/pages/index.astro delete mode 100644 demo/astro/tsconfig.json diff --git a/demo/astro/.gitignore b/demo/astro/.gitignore deleted file mode 100644 index 016b59ea..00000000 --- a/demo/astro/.gitignore +++ /dev/null @@ -1,24 +0,0 @@ -# build output -dist/ - -# generated types -.astro/ - -# dependencies -node_modules/ - -# logs -npm-debug.log* -yarn-debug.log* -yarn-error.log* -pnpm-debug.log* - -# environment variables -.env -.env.production - -# macOS-specific files -.DS_Store - -# jetbrains setting folder -.idea/ diff --git a/demo/astro/.vscode/extensions.json b/demo/astro/.vscode/extensions.json deleted file mode 100644 index 22a15055..00000000 --- a/demo/astro/.vscode/extensions.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "recommendations": ["astro-build.astro-vscode"], - "unwantedRecommendations": [] -} diff --git a/demo/astro/.vscode/launch.json b/demo/astro/.vscode/launch.json deleted file mode 100644 index d6422097..00000000 --- a/demo/astro/.vscode/launch.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - "command": "./node_modules/.bin/astro dev", - "name": "Development server", - "request": "launch", - "type": "node-terminal" - } - ] -} diff --git a/demo/astro/README.md b/demo/astro/README.md deleted file mode 100644 index 1db3fb39..00000000 --- a/demo/astro/README.md +++ /dev/null @@ -1,54 +0,0 @@ -# Astro Starter Kit: Basics - -```sh -npm create astro@latest -- --template basics -``` - -[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/withastro/astro/tree/latest/examples/basics) -[![Open with CodeSandbox](https://assets.codesandbox.io/github/button-edit-lime.svg)](https://codesandbox.io/p/sandbox/github/withastro/astro/tree/latest/examples/basics) -[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/withastro/astro?devcontainer_path=.devcontainer/basics/devcontainer.json) - -> 🧑‍🚀 **Seasoned astronaut?** Delete this file. Have fun! - -![just-the-basics](https://github.com/withastro/astro/assets/2244813/a0a5533c-a856-4198-8470-2d67b1d7c554) - -## 🚀 Project Structure - -Inside of your Astro project, you'll see the following folders and files: - -```text -/ -├── public/ -│ └── favicon.svg -├── src/ -│ ├── components/ -│ │ └── Card.astro -│ ├── layouts/ -│ │ └── Layout.astro -│ └── pages/ -│ └── index.astro -└── package.json -``` - -Astro looks for `.astro` or `.md` files in the `src/pages/` directory. Each page is exposed as a route based on its file name. - -There's nothing special about `src/components/`, but that's where we like to put any Astro/React/Vue/Svelte/Preact components. - -Any static assets, like images, can be placed in the `public/` directory. - -## 🧞 Commands - -All commands are run from the root of the project, from a terminal: - -| Command | Action | -| :------------------------ | :----------------------------------------------- | -| `npm install` | Installs dependencies | -| `npm run dev` | Starts local dev server at `localhost:4321` | -| `npm run build` | Build your production site to `./dist/` | -| `npm run preview` | Preview your build locally, before deploying | -| `npm run astro ...` | Run CLI commands like `astro add`, `astro check` | -| `npm run astro -- --help` | Get help using the Astro CLI | - -## 👀 Want to learn more? - -Feel free to check [our documentation](https://docs.astro.build) or jump into our [Discord server](https://astro.build/chat). diff --git a/demo/astro/astro.config.mjs b/demo/astro/astro.config.mjs deleted file mode 100644 index 29818ed6..00000000 --- a/demo/astro/astro.config.mjs +++ /dev/null @@ -1,10 +0,0 @@ -// @ts-check -import { defineConfig } from 'astro/config'; - -import vercel from '@astrojs/vercel/serverless'; - -// https://astro.build/config -export default defineConfig({ - output: 'server', - adapter: vercel() -}); \ No newline at end of file diff --git a/demo/astro/package.json b/demo/astro/package.json deleted file mode 100644 index a93c6312..00000000 --- a/demo/astro/package.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "name": "@dev/astro", - "private": true, - "type": "module", - "version": "0.0.1", - "scripts": { - "dev": "astro dev", - "start": "astro dev", - "build": "astro check && astro build", - "preview": "astro preview", - "astro": "astro" - }, - "dependencies": { - "@astrojs/check": "^0.9.3", - "@astrojs/vercel": "^7.8.1", - "astro": "^4.15.8", - "better-auth": "workspace:*", - "typescript": "^5.6.2" - } -} \ No newline at end of file diff --git a/demo/astro/public/favicon.svg b/demo/astro/public/favicon.svg deleted file mode 100644 index f157bd1c..00000000 --- a/demo/astro/public/favicon.svg +++ /dev/null @@ -1,9 +0,0 @@ - - - - diff --git a/demo/astro/src/components/Card.astro b/demo/astro/src/components/Card.astro deleted file mode 100644 index bd6d5971..00000000 --- a/demo/astro/src/components/Card.astro +++ /dev/null @@ -1,61 +0,0 @@ ---- -interface Props { - title: string; - body: string; - href: string; -} - -const { href, title, body } = Astro.props; ---- - - - diff --git a/demo/astro/src/env.d.ts b/demo/astro/src/env.d.ts deleted file mode 100644 index 9bc5cb41..00000000 --- a/demo/astro/src/env.d.ts +++ /dev/null @@ -1 +0,0 @@ -/// \ No newline at end of file diff --git a/demo/astro/src/layouts/Layout.astro b/demo/astro/src/layouts/Layout.astro deleted file mode 100644 index 18109712..00000000 --- a/demo/astro/src/layouts/Layout.astro +++ /dev/null @@ -1,50 +0,0 @@ ---- -interface Props { - title: string; -} - -const { title } = Astro.props; ---- - - - - - - - - - - {title} - - - - - - diff --git a/demo/astro/src/lib/auth-client.ts b/demo/astro/src/lib/auth-client.ts deleted file mode 100644 index e69de29b..00000000 diff --git a/demo/astro/src/lib/auth.ts b/demo/astro/src/lib/auth.ts deleted file mode 100644 index d1dc86c2..00000000 --- a/demo/astro/src/lib/auth.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { betterAuth } from "better-auth"; - -export const auth = betterAuth({ - database: { - provider: "sqlite", - url: "./sqlite.db", - }, -}); diff --git a/demo/astro/src/pages/api/auth/[...id].ts b/demo/astro/src/pages/api/auth/[...id].ts deleted file mode 100644 index 2e0fd61f..00000000 --- a/demo/astro/src/pages/api/auth/[...id].ts +++ /dev/null @@ -1,6 +0,0 @@ -import type { APIRoute } from "astro"; -import { auth } from "../../../lib/auth"; - -export const ALL: APIRoute = ({ request }) => { - return auth.handler(request); -}; diff --git a/demo/astro/src/pages/index.astro b/demo/astro/src/pages/index.astro deleted file mode 100644 index fb626287..00000000 --- a/demo/astro/src/pages/index.astro +++ /dev/null @@ -1,123 +0,0 @@ ---- -import Layout from '../layouts/Layout.astro'; -import Card from '../components/Card.astro'; ---- - - -
- -

Welcome to Astro

-

- To get started, open the directory src/pages in your project.
- Code Challenge: Tweak the "Welcome to Astro" message above. -

- -
-
- - diff --git a/demo/astro/tsconfig.json b/demo/astro/tsconfig.json deleted file mode 100644 index 77da9dd0..00000000 --- a/demo/astro/tsconfig.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "extends": "astro/tsconfigs/strict" -} \ No newline at end of file diff --git a/demo/nextjs/app/layout.tsx b/demo/nextjs/app/layout.tsx index 69bd62ea..1e33d1f4 100644 --- a/demo/nextjs/app/layout.tsx +++ b/demo/nextjs/app/layout.tsx @@ -8,14 +8,13 @@ import { GeistSans } from "geist/font/sans"; import { Wrapper, WrapperWithQuery } from "@/components/wrapper"; import { createMetadata } from "@/lib/metadata"; - export const metadata = createMetadata({ title: { template: "%s | Better Auth", default: "Better Auth", }, description: "The most comprehensive authentication library for typescript", - metadataBase: new URL("https://demo.better-auth.com",) + metadataBase: new URL("https://demo.better-auth.com"), }); export default function RootLayout({ @@ -31,9 +30,7 @@ export default function RootLayout({ - - {children} - + {children} diff --git a/demo/nextjs/app/page.tsx b/demo/nextjs/app/page.tsx index cb190076..4f0e7c99 100644 --- a/demo/nextjs/app/page.tsx +++ b/demo/nextjs/app/page.tsx @@ -4,8 +4,15 @@ import { Suspense } from "react"; export default async function Home() { const features = [ - "Email & Password", "Organization | Teams", "Passkeys", "Multi Factor", "Password Reset", "Roles & Permissions", "Rate Limiting", "Session Management", - ] + "Email & Password", + "Organization | Teams", + "Passkeys", + "Multi Factor", + "Password Reset", + "Roles & Permissions", + "Rate Limiting", + "Session Management", + ]; return (
@@ -14,7 +21,15 @@ export default async function Home() { Better Auth.

- Official demo to showcase better-auth. features and capabilities.
+ Official demo to showcase{" "} + + better-auth. + {" "} + features and capabilities.

@@ -22,14 +37,17 @@ export default async function Home() {
- All features on this demo are Implemented with better auth without any custom backend code + All features on this demo are Implemented with better auth + without any custom backend code
- - {features.map(feature => ( - + {features.map((feature) => ( + {feature}. ))} diff --git a/demo/nextjs/lib/auth.ts b/demo/nextjs/lib/auth.ts index 106e6ad3..646312d6 100644 --- a/demo/nextjs/lib/auth.ts +++ b/demo/nextjs/lib/auth.ts @@ -75,14 +75,14 @@ export const auth = betterAuth({ }), passkey(), ], - socialProvider: [ - github({ + socialProviders: { + github: { clientId: process.env.GITHUB_CLIENT_ID || "", clientSecret: process.env.GITHUB_CLIENT_SECRET || "", - }), - google({ + }, + google: { clientId: process.env.GOOGLE_CLIENT_ID || "", clientSecret: process.env.GOOGLE_CLIENT_SECRET || "", - }), - ], + }, + }, }); diff --git a/docs/content/docs/authentication/apple.mdx b/docs/content/docs/authentication/apple.mdx index 11a306ba..bd286952 100644 --- a/docs/content/docs/authentication/apple.mdx +++ b/docs/content/docs/authentication/apple.mdx @@ -33,18 +33,12 @@ description: Apple provider: "sqlite", url: "./db.sqlite", }, - socialProvider: [ // [!code highlight] - apple({ // [!code highlight] + socialProvider: { // [!code highlight] + apple: { // [!code highlight] clientId: process.env.APPLE_CLIENT_ID as string, // [!code highlight] clientSecret: process.env.APPLE_CLIENT_SECRET as string, // [!code highlight] - /** // [!code highlight] - * this is optional. // [!code highlight] - * only needed if you passed a custom basePath from `/api/auth` to `betterAuth` // [!code highlight] - * or if you're using a custom domain. // [!code highlight] - */ // [!code highlight] - redirectURI: "https://example.com/api/auth/callback/apple", // [!code highlight] - }), // [!code highlight] - ], // [!code highlight] + } // [!code highlight] + }, // [!code highlight] }) ``` diff --git a/docs/content/docs/authentication/discord.mdx b/docs/content/docs/authentication/discord.mdx index bc75ceb7..668fdf60 100644 --- a/docs/content/docs/authentication/discord.mdx +++ b/docs/content/docs/authentication/discord.mdx @@ -29,18 +29,12 @@ description: Discord Provider provider: "sqlite", url: "./db.sqlite", }, - socialProvider: [ // [!code highlight] - discord({ // [!code highlight] + socialProvider: { // [!code highlight] + discord: { // [!code highlight] clientId: process.env.DISCORD_CLIENT_ID as string, // [!code highlight] clientSecret: process.env.DISCORD_CLIENT_SECRET as string, // [!code highlight] - /** // [!code highlight] - * this is optional. // [!code highlight] - * only needed if you passed a custom basePath from `/api/auth` to `betterAuth` // [!code highlight] - * or if you're using a custom domain. // [!code highlight] - */ // [!code highlight] - redirectURI: "https://example.com/api/auth/callback/apple", // [!code highlight] - }), // [!code highlight] - ], // [!code highlight] + }, // [!code highlight] + }, // [!code highlight] }) ``` diff --git a/docs/content/docs/authentication/facebook.mdx b/docs/content/docs/authentication/facebook.mdx index 16a3501b..004110cf 100644 --- a/docs/content/docs/authentication/facebook.mdx +++ b/docs/content/docs/authentication/facebook.mdx @@ -29,18 +29,12 @@ description: Facebook Provider provider: "sqlite", url: "./db.sqlite", }, - socialProvider: [ // [!code highlight] - facebook({ // [!code highlight] + socialProvider: { // [!code highlight] + facebook: { // [!code highlight] clientId: process.env.FACEBOOK_CLIENT_ID as string, // [!code highlight] clientSecret: process.env.FACEBOOK_CLIENT_SECRET as string, // [!code highlight] - /** // [!code highlight] - * this is optional. // [!code highlight] - * only needed if you passed a custom basePath from `/api/auth` to `betterAuth` // [!code highlight] - * or if you're using a custom domain. // [!code highlight] - */ // [!code highlight] - redirectURI: "https://example.com/api/auth/callback/apple", // [!code highlight] - }), // [!code highlight] - ], // [!code highlight] + }, // [!code highlight] + }, // [!code highlight] }) ``` diff --git a/docs/content/docs/authentication/github.mdx b/docs/content/docs/authentication/github.mdx index 5b223efd..9056656c 100644 --- a/docs/content/docs/authentication/github.mdx +++ b/docs/content/docs/authentication/github.mdx @@ -30,16 +30,10 @@ description: Github Provider url: "./db.sqlite", }, socialProvider: [ // [!code highlight] - github({ // [!code highlight] + github: { // [!code highlight] clientId: process.env.GITHUB_CLIENT_ID as string, // [!code highlight] clientSecret: process.env.GITHUB_CLIENT_SECRET as string, // [!code highlight] - /** // [!code highlight] - * this is optional. // [!code highlight] - * only needed if you passed a custom basePath from `/api/auth` to `betterAuth` // [!code highlight] - * or if you're using a custom domain. // [!code highlight] - */ // [!code highlight] - redirectURI: "https://example.com/api/auth/callback/apple", // [!code highlight] - }), // [!code highlight] + }, // [!code highlight] ], // [!code highlight] }) ``` diff --git a/docs/content/docs/authentication/google.mdx b/docs/content/docs/authentication/google.mdx index 065b980f..90eae9bd 100644 --- a/docs/content/docs/authentication/google.mdx +++ b/docs/content/docs/authentication/google.mdx @@ -29,8 +29,8 @@ description: Google Provider provider: "sqlite", url: "./db.sqlite", }, - socialProvider: [ // [!code highlight] - google({ // [!code highlight] + socialProvider: { // [!code highlight] + google: { // [!code highlight] clientId: process.env.GOOGLE_CLIENT_ID as string, // [!code highlight] clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, // [!code highlight] /** // [!code highlight] @@ -39,8 +39,8 @@ description: Google Provider * or if you're using a custom domain. // [!code highlight] */ // [!code highlight] redirectURI: "https://example.com/api/auth/callback/apple", // [!code highlight] - }), // [!code highlight] - ], // [!code highlight] + }, // [!code highlight] + }, // [!code highlight] }) ``` diff --git a/docs/content/docs/authentication/spotify.mdx b/docs/content/docs/authentication/spotify.mdx index 6f93b4b9..52380b4a 100644 --- a/docs/content/docs/authentication/spotify.mdx +++ b/docs/content/docs/authentication/spotify.mdx @@ -29,18 +29,12 @@ description: Spotify Provider provider: "sqlite", url: "./db.sqlite", }, - socialProvider: [ // [!code highlight] - spotify({ // [!code highlight] + socialProvider: { // [!code highlight] + spotify: { // [!code highlight] clientId: process.env.SPOTIFY_CLIENT_ID as string, // [!code highlight] clientSecret: process.env.SPOTIFY_CLIENT_SECRET as string, // [!code highlight] - /** // [!code highlight] - * this is optional. // [!code highlight] - * only needed if you passed a custom basePath from `/api/auth` to `betterAuth` // [!code highlight] - * or if you're using a custom domain. // [!code highlight] - */ // [!code highlight] - redirectURI: "https://example.com/api/auth/callback/apple", // [!code highlight] - }), // [!code highlight] - ], // [!code highlight] + }, // [!code highlight] + }, // [!code highlight] }) ``` diff --git a/docs/content/docs/authentication/twitch.mdx b/docs/content/docs/authentication/twitch.mdx index 8714f719..061c8724 100644 --- a/docs/content/docs/authentication/twitch.mdx +++ b/docs/content/docs/authentication/twitch.mdx @@ -29,18 +29,12 @@ description: Twitch Provider provider: "sqlite", url: "./db.sqlite", }, - socialProvider: [ // [!code highlight] - twitch({ // [!code highlight] + socialProvider: { // [!code highlight] + twitch: { // [!code highlight] clientId: process.env.TWITCH_CLIENT_ID as string, // [!code highlight] clientSecret: process.env.TWITCH_CLIENT_SECRET as string, // [!code highlight] - /** // [!code highlight] - * this is optional. // [!code highlight] - * only needed if you passed a custom basePath from `/api/auth` to `betterAuth` // [!code highlight] - * or if you're using a custom domain. // [!code highlight] - */ // [!code highlight] - redirectURI: "https://example.com/api/auth/callback/apple", // [!code highlight] - }), // [!code highlight] - ], // [!code highlight] + }, // [!code highlight] + } // [!code highlight] }) ``` diff --git a/docs/content/docs/authentication/twitter.mdx b/docs/content/docs/authentication/twitter.mdx index 398d5fd9..5d585ab7 100644 --- a/docs/content/docs/authentication/twitter.mdx +++ b/docs/content/docs/authentication/twitter.mdx @@ -29,18 +29,12 @@ description: Twitter Provider provider: "sqlite", url: "./db.sqlite", }, - ocialProvider: [ // [!code highlight] - twitter({ // [!code highlight] + socialProvider: {// [!code highlight] + twitter: { // [!code highlight] clientId: process.env.TWITTER_CLIENT_ID, // [!code highlight] clientSecret: process.env.TWITTER_CLIENT_SECRET, // [!code highlight] - /** // [!code highlight] - * this is optional. // [!code highlight] - * only needed if you passed a custom basePath from `/api/auth` to `betterAuth` // [!code highlight] - * or if you're using a custom domain. // [!code highlight] - */ // [!code highlight] - redirectURI: "https://example.com/api/auth/callback/apple", // [!code highlight] - }), // [!code highlight] - ], // [!code highlight] + }, // [!code highlight] + }, // [!code highlight] }) ``` diff --git a/docs/content/docs/basic-usage.mdx b/docs/content/docs/basic-usage.mdx index 77523703..3b6baa40 100644 --- a/docs/content/docs/basic-usage.mdx +++ b/docs/content/docs/basic-usage.mdx @@ -97,12 +97,12 @@ export const auth = betterAuth({ provider: "sqlite", url: "./db.sqlite", }, - socialProvider: [ // [!code highlight] - github({ // [!code highlight] - clientId: GITHUB_CLIENT_ID, // [!code highlight] - clientSecret: GITHUB_CLIENT_SECRET, // [!code highlight] - }), // [!code highlight] - ], // [!code highlight] + socialProvider: { // [!code highlight] + github: { // [!code highlight] + clientId: GITHUB_CLIENT_ID, // [!code highlight] + clientSecret: GITHUB_CLIENT_SECRET, // [!code highlight] + } // [!code highlight] + }, // [!code highlight] }) ``` diff --git a/docs/content/docs/installation.mdx b/docs/content/docs/installation.mdx index 14105644..61958889 100644 --- a/docs/content/docs/installation.mdx +++ b/docs/content/docs/installation.mdx @@ -123,12 +123,12 @@ export const auth = betterAuth({ provider: "sqlite", url: "./db.sqlite", }, - socialProvider: [ // [!code highlight] - github({ // [!code highlight] - clientId: process.env.GITHUB_CLIENT_ID as string, // [!code highlight] - clientSecret: process.env.GITHUB_CLIENT_SECRET as string, // [!code highlight] - }), // [!code highlight] - ], // [!code highlight] + socialProvider: { // [!code highlight] + github: { // [!code highlight] + clientId: GITHUB_CLIENT_ID, // [!code highlight] + clientSecret: GITHUB_CLIENT_SECRET, // [!code highlight] + } // [!code highlight] + }, // [!code highlight] emailAndPassword: { // [!code highlight] enabled: true // [!code highlight] }// [!code highlight] diff --git a/packages/better-auth/src/api/routes/callback.ts b/packages/better-auth/src/api/routes/callback.ts index 15c03d20..020e20a8 100644 --- a/packages/better-auth/src/api/routes/callback.ts +++ b/packages/better-auth/src/api/routes/callback.ts @@ -31,7 +31,7 @@ export const callbackOAuth = createAuthEndpoint( ); } - const provider = c.context.options.socialProvider?.find( + const provider = c.context.socialProviders.find( (p) => p.id === c.params.id, ); if (!provider) { diff --git a/packages/better-auth/src/api/routes/sign-in.ts b/packages/better-auth/src/api/routes/sign-in.ts index 06c739ca..730282e2 100644 --- a/packages/better-auth/src/api/routes/sign-in.ts +++ b/packages/better-auth/src/api/routes/sign-in.ts @@ -37,7 +37,7 @@ export const signInOAuth = createAuthEndpoint( }), }, async (c) => { - const provider = c.context.options.socialProvider?.find( + const provider = c.context.socialProviders.find( (p) => p.id === c.body.provider, ); if (!provider) { diff --git a/packages/better-auth/src/init.ts b/packages/better-auth/src/init.ts index 245aaa22..8284b4fd 100644 --- a/packages/better-auth/src/init.ts +++ b/packages/better-auth/src/init.ts @@ -4,7 +4,7 @@ import { createKyselyAdapter } from "./adapters/kysely"; import { getAdapter } from "./adapters/utils"; import { hashPassword, verifyPassword } from "./crypto/password"; import { createInternalAdapter } from "./db"; -import type { BetterAuthOptions } from "./types"; +import type { BetterAuthOptions, OAuthProvider } from "./types"; import { getBaseURL } from "./utils/base-url"; import { DEFAULT_SECRET } from "./utils/constants"; import { @@ -13,6 +13,7 @@ import { getCookies, } from "./utils/cookies"; import { createLogger } from "./utils/logger"; +import { oAuthProviderList, oAuthProviders } from "./social-providers"; export const init = (options: BetterAuthOptions) => { const adapter = getAdapter(options); @@ -30,8 +31,19 @@ export const init = (options: BetterAuthOptions) => { const cookies = getCookies(options); const tables = getAuthTables(options); + + const socialProviders = Object.keys(options.socialProviders || {}) + .map((key) => { + const value = options.socialProviders?.[key as "github"]!; + if (value.enabled === false) { + return null; + } + return oAuthProviders[key as (typeof oAuthProviderList)[number]](value); + }) + .filter((x) => x !== null); return { appName: options.appName || "Better Auth", + socialProviders, options: { ...options, baseURL: baseURL ? new URL(baseURL).origin : "", @@ -63,6 +75,7 @@ export type AuthContext = { options: BetterAuthOptions; appName: string; baseURL: string; + socialProviders: OAuthProvider[]; authCookies: BetterAuthCookies; logger: ReturnType; db: Kysely; diff --git a/packages/better-auth/src/social-providers/index.ts b/packages/better-auth/src/social-providers/index.ts index 594f176f..54037c12 100644 --- a/packages/better-auth/src/social-providers/index.ts +++ b/packages/better-auth/src/social-providers/index.ts @@ -1,3 +1,4 @@ +import type { Prettify } from "../types"; import { apple } from "./apple"; import { discord } from "./discord"; import { facebook } from "./facebook"; @@ -23,6 +24,14 @@ export const oAuthProviderList = Object.keys(oAuthProviders) as [ ...(keyof typeof oAuthProviders)[], ]; +export type SocialProviders = typeof oAuthProviders extends { + [key in infer K]: infer V; +} + ? V extends (options: infer V) => any + ? Partial>> + : never + : never; + export * from "./github"; export * from "./google"; export * from "./apple"; diff --git a/packages/better-auth/src/test-utils/test-instance.ts b/packages/better-auth/src/test-utils/test-instance.ts index a8a1523e..7f981e81 100644 --- a/packages/better-auth/src/test-utils/test-instance.ts +++ b/packages/better-auth/src/test-utils/test-instance.ts @@ -1,14 +1,12 @@ import fs from "fs/promises"; import { alphabet, generateRandomString } from "oslo/crypto"; -import { afterAll, beforeAll } from "vitest"; +import { afterAll } from "vitest"; import { betterAuth } from "../auth"; import { createAuthClient } from "../client/vanilla"; -import { github, google } from "../social-providers"; import type { BetterAuthOptions } from "../types"; import { getMigrations } from "../cli/utils/get-migration"; import { parseSetCookieHeader } from "../utils/cookies"; import type { SuccessContext } from "@better-fetch/fetch"; -import { createKyselyAdapter } from "../adapters/kysely"; import { getAdapter } from "../adapters/utils"; export async function getTestInstance>( @@ -22,16 +20,16 @@ export async function getTestInstance>( const randomStr = generateRandomString(4, alphabet("a-z")); const dbName = `./.db/test-${randomStr}.db`; const opts = { - socialProvider: [ - github({ + socialProviders: { + github: { clientId: "test", clientSecret: "test", - }), - google({ + }, + google: { clientId: "test", clientSecret: "test", - }), - ], + }, + }, secret: "better-auth.secret", database: { provider: "sqlite", diff --git a/packages/better-auth/src/types/options.ts b/packages/better-auth/src/types/options.ts index 495b6bc0..d47376cc 100644 --- a/packages/better-auth/src/types/options.ts +++ b/packages/better-auth/src/types/options.ts @@ -2,7 +2,8 @@ import type { Dialect } from "kysely"; import type { User } from "../adapters/schema"; import type { FieldAttribute } from "../db/field"; import type { BetterAuthPlugin } from "./plugins"; -import type { OAuthProvider, OAuthProviderList } from "./provider"; +import type { OAuthProviderList } from "./provider"; +import type { SocialProviders } from "../social-providers"; export interface BetterAuthOptions { /** @@ -57,9 +58,9 @@ export interface BetterAuthOptions { /** * list of social providers */ - socialProvider?: OAuthProvider[]; + socialProviders?: SocialProviders; /** - * Plugins + * List of Better Auth plugins */ plugins?: BetterAuthPlugin[]; /**