diff --git a/packages/studiocms_auth/src/components/StaticAuthCheck.astro b/packages/studiocms_auth/src/components/StaticAuthCheck.astro index 49495385..6efa8d99 100644 --- a/packages/studiocms_auth/src/components/StaticAuthCheck.astro +++ b/packages/studiocms_auth/src/components/StaticAuthCheck.astro @@ -7,14 +7,14 @@ const { isLoggedIn } = await getUserData(Astro); \ No newline at end of file diff --git a/packages/studiocms_auth/src/routes/api/auth0/callback.ts b/packages/studiocms_auth/src/routes/api/auth0/callback.ts index eabd6ae7..74ade2dd 100644 --- a/packages/studiocms_auth/src/routes/api/auth0/callback.ts +++ b/packages/studiocms_auth/src/routes/api/auth0/callback.ts @@ -1,18 +1,12 @@ import { logger } from '@it-astro:logger:studiocms-auth'; -import { and, db, eq } from 'astro:db'; import { createUserSession } from 'studiocms:auth/lib/session'; import { LinkNewOAuthCookieName, createOAuthUser, getUserData } from 'studiocms:auth/lib/user'; import { StudioCMSRoutes } from 'studiocms:lib'; -import { tsOAuthAccounts, tsUsers } from '@studiocms/core/sdk-utils/tables'; +import studioCMS_SDK from 'studiocms:sdk'; import { OAuth2RequestError, type OAuth2Tokens } from 'arctic'; import type { APIContext, APIRoute } from 'astro'; import { type Auth0User, ProviderCookieName, ProviderID, auth0, getClientDomain } from './shared'; -const { - authLinks: { loginURL }, - mainLinks: { dashboardIndex }, -} = StudioCMSRoutes; - export const GET: APIRoute = async (context: APIContext): Promise => { const { url, cookies, redirect } = context; @@ -23,7 +17,7 @@ export const GET: APIRoute = async (context: APIContext): Promise => { const CLIENT_DOMAIN = getClientDomain(); if (!code || !state || !storedState || state !== storedState) { - return redirect(loginURL); + return redirect(StudioCMSRoutes.authLinks.loginURL); } let tokens: OAuth2Tokens; @@ -46,23 +40,13 @@ export const GET: APIRoute = async (context: APIContext): Promise => { // // TODO: Add first-time setup logic here // } - const existingoAuthAccount = await db - .select() - .from(tsOAuthAccounts) - .where( - and( - eq(tsOAuthAccounts.provider, ProviderID), - eq(tsOAuthAccounts.providerUserId, auth0UserId) - ) - ) - .get(); - - if (existingoAuthAccount) { - const user = await db - .select() - .from(tsUsers) - .where(eq(tsUsers.id, existingoAuthAccount.userId)) - .get(); + const existingOAuthAccount = await studioCMS_SDK.AUTH.oAuth.searchProvidersForId( + ProviderID, + auth0UserId + ); + + if (existingOAuthAccount) { + const user = await studioCMS_SDK.GET.databaseEntry.users.byId(existingOAuthAccount.userId); if (!user) { return new Response('User not found', { @@ -72,29 +56,25 @@ export const GET: APIRoute = async (context: APIContext): Promise => { await createUserSession(user.id, context); - return redirect(dashboardIndex); + return redirect(StudioCMSRoutes.mainLinks.dashboardIndex); } const loggedInUser = await getUserData(context); const linkNewOAuth = !!cookies.get(LinkNewOAuthCookieName)?.value; if (loggedInUser.user && linkNewOAuth) { - const exisitingUser = await db - .select() - .from(tsUsers) - .where(eq(tsUsers.id, loggedInUser.user.id)) - .get(); - - if (exisitingUser) { - await db.insert(tsOAuthAccounts).values({ + const existingUser = await studioCMS_SDK.GET.databaseEntry.users.byId(loggedInUser.user.id); + + if (existingUser) { + await studioCMS_SDK.AUTH.oAuth.create({ + userId: existingUser.id, provider: ProviderID, providerUserId: auth0UserId, - userId: exisitingUser.id, }); - await createUserSession(exisitingUser.id, context); + await createUserSession(existingUser.id, context); - return redirect(dashboardIndex); + return redirect(StudioCMSRoutes.mainLinks.dashboardIndex); } } @@ -116,7 +96,7 @@ export const GET: APIRoute = async (context: APIContext): Promise => { await createUserSession(newUser.id, context); - return redirect(dashboardIndex); + return redirect(StudioCMSRoutes.mainLinks.dashboardIndex); } catch (e) { // the specific error message depends on the provider if (e instanceof OAuth2RequestError) { diff --git a/packages/studiocms_auth/src/routes/api/auth0/shared.ts b/packages/studiocms_auth/src/routes/api/auth0/shared.ts index 4f1e8310..b77de9b9 100644 --- a/packages/studiocms_auth/src/routes/api/auth0/shared.ts +++ b/packages/studiocms_auth/src/routes/api/auth0/shared.ts @@ -1,32 +1,23 @@ import { authEnvCheck } from 'studiocms:auth/utils/authEnvCheck'; -import Config from 'studiocms:config'; +import { AuthConfig } from 'studiocms:config'; import { Auth0 } from 'arctic'; export const { - AUTH0: { CLIENT_ID, CLIENT_SECRET, DOMAIN, REDIRECT_URI }, -} = await authEnvCheck(Config.dashboardConfig.AuthConfig.providers); + AUTH0: { CLIENT_ID = '', CLIENT_SECRET = '', DOMAIN, REDIRECT_URI = '' }, +} = await authEnvCheck(AuthConfig.providers); export const getClientDomain = () => { - const cleanDomainslash = DOMAIN ? DOMAIN.replace(/^\//, '') : ''; + const cleanDomainSlash = DOMAIN ? DOMAIN.replace(/^\//, '') : ''; - const NoHTTPDOMAIN = cleanDomainslash.replace(/http:\/\//, '').replace(/https:\/\//, ''); + const NoHttpDomain = cleanDomainSlash.replace(/http:\/\//, '').replace(/https:\/\//, ''); - return `https://${NoHTTPDOMAIN}`; -}; - -const CLIENT = () => { - return { - DOMAIN: getClientDomain(), - ID: CLIENT_ID || '', - SECRET: CLIENT_SECRET || '', - URI: REDIRECT_URI || '', - }; + return `https://${NoHttpDomain}`; }; export const ProviderID = 'auth0'; export const ProviderCookieName = 'auth0_oauth_state'; -export const auth0 = new Auth0(CLIENT().DOMAIN, CLIENT().ID, CLIENT().SECRET, CLIENT().URI); +export const auth0 = new Auth0(getClientDomain(), CLIENT_ID, CLIENT_SECRET, REDIRECT_URI); export interface Auth0User { sub: string; diff --git a/packages/studiocms_auth/src/routes/api/discord/callback.ts b/packages/studiocms_auth/src/routes/api/discord/callback.ts index 7dbeef06..79303517 100644 --- a/packages/studiocms_auth/src/routes/api/discord/callback.ts +++ b/packages/studiocms_auth/src/routes/api/discord/callback.ts @@ -1,18 +1,12 @@ import { logger } from '@it-astro:logger:studiocms-auth'; -import { and, db, eq } from 'astro:db'; import { createUserSession } from 'studiocms:auth/lib/session'; import { LinkNewOAuthCookieName, createOAuthUser, getUserData } from 'studiocms:auth/lib/user'; import { StudioCMSRoutes } from 'studiocms:lib'; -import { tsOAuthAccounts, tsUsers } from '@studiocms/core/sdk-utils/tables'; +import studioCMS_SDK from 'studiocms:sdk'; import { OAuth2RequestError, type OAuth2Tokens } from 'arctic'; import type { APIContext, APIRoute } from 'astro'; import { type DiscordUser, ProviderCookieName, ProviderID, discord } from './shared'; -const { - authLinks: { loginURL }, - mainLinks: { dashboardIndex }, -} = StudioCMSRoutes; - export const GET: APIRoute = async (context: APIContext): Promise => { const { url, cookies, redirect } = context; @@ -21,7 +15,7 @@ export const GET: APIRoute = async (context: APIContext): Promise => { const storedState = cookies.get(ProviderCookieName)?.value ?? null; if (!code || !state || !storedState || state !== storedState) { - return redirect(loginURL); + return redirect(StudioCMSRoutes.authLinks.loginURL); } let tokens: OAuth2Tokens; @@ -44,23 +38,13 @@ export const GET: APIRoute = async (context: APIContext): Promise => { // // TODO: Add first-time setup logic here // } - const existingoAuthAccount = await db - .select() - .from(tsOAuthAccounts) - .where( - and( - eq(tsOAuthAccounts.provider, ProviderID), - eq(tsOAuthAccounts.providerUserId, discordUserId) - ) - ) - .get(); - - if (existingoAuthAccount) { - const user = await db - .select() - .from(tsUsers) - .where(eq(tsUsers.id, existingoAuthAccount.userId)) - .get(); + const existingOAuthAccount = await studioCMS_SDK.AUTH.oAuth.searchProvidersForId( + ProviderID, + discordUserId + ); + + if (existingOAuthAccount) { + const user = await studioCMS_SDK.GET.databaseEntry.users.byId(existingOAuthAccount.userId); if (!user) { return new Response('User not found', { @@ -70,29 +54,25 @@ export const GET: APIRoute = async (context: APIContext): Promise => { await createUserSession(user.id, context); - return redirect(dashboardIndex); + return redirect(StudioCMSRoutes.mainLinks.dashboardIndex); } const loggedInUser = await getUserData(context); const linkNewOAuth = !!cookies.get(LinkNewOAuthCookieName)?.value; if (loggedInUser.user && linkNewOAuth) { - const existingUser = await db - .select() - .from(tsUsers) - .where(eq(tsUsers.id, loggedInUser.user.id)) - .get(); + const existingUser = await studioCMS_SDK.GET.databaseEntry.users.byId(loggedInUser.user.id); if (existingUser) { - await db.insert(tsOAuthAccounts).values({ + await studioCMS_SDK.AUTH.oAuth.create({ + userId: existingUser.id, provider: ProviderID, providerUserId: discordUserId, - userId: existingUser.id, }); await createUserSession(existingUser.id, context); - return redirect(dashboardIndex); + return redirect(StudioCMSRoutes.mainLinks.dashboardIndex); } } @@ -116,7 +96,7 @@ export const GET: APIRoute = async (context: APIContext): Promise => { await createUserSession(newUser.id, context); - return redirect(dashboardIndex); + return redirect(StudioCMSRoutes.mainLinks.dashboardIndex); } catch (e) { // the specific error message depends on the provider if (e instanceof OAuth2RequestError) { diff --git a/packages/studiocms_auth/src/routes/api/discord/shared.ts b/packages/studiocms_auth/src/routes/api/discord/shared.ts index b46aeb7c..c0e1f643 100644 --- a/packages/studiocms_auth/src/routes/api/discord/shared.ts +++ b/packages/studiocms_auth/src/routes/api/discord/shared.ts @@ -1,23 +1,15 @@ import { authEnvCheck } from 'studiocms:auth/utils/authEnvCheck'; -import Config from 'studiocms:config'; +import { AuthConfig } from 'studiocms:config'; import { Discord } from 'arctic'; export const { - DISCORD: { CLIENT_ID, CLIENT_SECRET, REDIRECT_URI }, -} = await authEnvCheck(Config.dashboardConfig.AuthConfig.providers); - -const CLIENT = () => { - return { - ID: CLIENT_ID || '', - SECRET: CLIENT_SECRET || '', - URI: REDIRECT_URI || '', - }; -}; + DISCORD: { CLIENT_ID = '', CLIENT_SECRET = '', REDIRECT_URI = '' }, +} = await authEnvCheck(AuthConfig.providers); export const ProviderID = 'discord'; export const ProviderCookieName = 'discord_oauth_state'; -export const discord = new Discord(CLIENT().ID, CLIENT().SECRET, CLIENT().URI); +export const discord = new Discord(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI); export interface DiscordUser { id: string; diff --git a/packages/studiocms_auth/src/routes/api/github/callback.ts b/packages/studiocms_auth/src/routes/api/github/callback.ts index 4d46fbe8..01b06a7a 100644 --- a/packages/studiocms_auth/src/routes/api/github/callback.ts +++ b/packages/studiocms_auth/src/routes/api/github/callback.ts @@ -1,18 +1,12 @@ import { logger } from '@it-astro:logger:studiocms-auth'; -import { and, db, eq } from 'astro:db'; import { createUserSession } from 'studiocms:auth/lib/session'; import { LinkNewOAuthCookieName, createOAuthUser, getUserData } from 'studiocms:auth/lib/user'; import { StudioCMSRoutes } from 'studiocms:lib'; -import { tsOAuthAccounts, tsUsers } from '@studiocms/core/sdk-utils/tables'; +import studioCMS_SDK from 'studiocms:sdk'; import { OAuth2RequestError, type OAuth2Tokens } from 'arctic'; import type { APIContext, APIRoute } from 'astro'; import { type GitHubUser, ProviderCookieName, ProviderID, github } from './shared'; -const { - authLinks: { loginURL }, - mainLinks: { dashboardIndex }, -} = StudioCMSRoutes; - export const GET: APIRoute = async (context: APIContext): Promise => { const { url, cookies, redirect } = context; @@ -21,7 +15,7 @@ export const GET: APIRoute = async (context: APIContext): Promise => { const storedState = cookies.get(ProviderCookieName)?.value ?? null; if (!code || !state || !storedState || state !== storedState) { - return redirect(loginURL); + return redirect(StudioCMSRoutes.authLinks.loginURL); } let tokens: OAuth2Tokens; @@ -44,23 +38,13 @@ export const GET: APIRoute = async (context: APIContext): Promise => { // // TODO: Add first-time setup logic here // } - const existingoAuthAccount = await db - .select() - .from(tsOAuthAccounts) - .where( - and( - eq(tsOAuthAccounts.provider, ProviderID), - eq(tsOAuthAccounts.providerUserId, `${githubUserId}`) - ) - ) - .get(); - - if (existingoAuthAccount) { - const user = await db - .select() - .from(tsUsers) - .where(eq(tsUsers.id, existingoAuthAccount.userId)) - .get(); + const existingOAuthAccount = await studioCMS_SDK.AUTH.oAuth.searchProvidersForId( + ProviderID, + `${githubUserId}` + ); + + if (existingOAuthAccount) { + const user = await studioCMS_SDK.GET.databaseEntry.users.byId(existingOAuthAccount.userId); if (!user) { return new Response('User not found', { @@ -70,29 +54,25 @@ export const GET: APIRoute = async (context: APIContext): Promise => { await createUserSession(user.id, context); - return redirect(dashboardIndex); + return redirect(StudioCMSRoutes.mainLinks.dashboardIndex); } const loggedInUser = await getUserData(context); const linkNewOAuth = !!cookies.get(LinkNewOAuthCookieName)?.value; if (loggedInUser.user && linkNewOAuth) { - const existingUser = await db - .select() - .from(tsUsers) - .where(eq(tsUsers.id, loggedInUser.user.id)) - .get(); + const existingUser = await studioCMS_SDK.GET.databaseEntry.users.byId(loggedInUser.user.id); if (existingUser) { - await db.insert(tsOAuthAccounts).values({ + await studioCMS_SDK.AUTH.oAuth.create({ + userId: existingUser.id, provider: ProviderID, providerUserId: `${githubUserId}`, - userId: existingUser.id, }); await createUserSession(existingUser.id, context); - return redirect(dashboardIndex); + return redirect(StudioCMSRoutes.mainLinks.dashboardIndex); } } @@ -115,7 +95,7 @@ export const GET: APIRoute = async (context: APIContext): Promise => { await createUserSession(newUser.id, context); - return redirect(dashboardIndex); + return redirect(StudioCMSRoutes.mainLinks.dashboardIndex); } catch (e) { if (e instanceof OAuth2RequestError) { const code = e.code; diff --git a/packages/studiocms_auth/src/routes/api/github/shared.ts b/packages/studiocms_auth/src/routes/api/github/shared.ts index d8acdd42..a0761d23 100644 --- a/packages/studiocms_auth/src/routes/api/github/shared.ts +++ b/packages/studiocms_auth/src/routes/api/github/shared.ts @@ -1,23 +1,15 @@ import { authEnvCheck } from 'studiocms:auth/utils/authEnvCheck'; -import Config from 'studiocms:config'; +import { AuthConfig } from 'studiocms:config'; import { GitHub } from 'arctic'; const { - GITHUB: { CLIENT_ID, CLIENT_SECRET, REDIRECT_URI }, -} = await authEnvCheck(Config.dashboardConfig.AuthConfig.providers); - -const CLIENT = () => { - return { - ID: CLIENT_ID || '', - SECRET: CLIENT_SECRET || '', - URI: REDIRECT_URI || null, - }; -}; + GITHUB: { CLIENT_ID = '', CLIENT_SECRET = '', REDIRECT_URI = null }, +} = await authEnvCheck(AuthConfig.providers); export const ProviderID = 'github'; export const ProviderCookieName = 'github_oauth_state'; -export const github = new GitHub(CLIENT().ID, CLIENT().SECRET, CLIENT().URI); +export const github = new GitHub(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI); export interface GitHubUser { id: number; diff --git a/packages/studiocms_auth/src/routes/api/google/callback.ts b/packages/studiocms_auth/src/routes/api/google/callback.ts index bc25d08c..773450bb 100644 --- a/packages/studiocms_auth/src/routes/api/google/callback.ts +++ b/packages/studiocms_auth/src/routes/api/google/callback.ts @@ -1,9 +1,8 @@ import { logger } from '@it-astro:logger:studiocms-auth'; -import { and, db, eq } from 'astro:db'; import { createUserSession } from 'studiocms:auth/lib/session'; import { LinkNewOAuthCookieName, createOAuthUser, getUserData } from 'studiocms:auth/lib/user'; import { StudioCMSRoutes } from 'studiocms:lib'; -import { tsOAuthAccounts, tsUsers } from '@studiocms/core/sdk-utils/tables'; +import studioCMS_SDK from 'studiocms:sdk'; import { OAuth2RequestError, type OAuth2Tokens } from 'arctic'; import type { APIContext, APIRoute } from 'astro'; import { @@ -14,11 +13,6 @@ import { google, } from './shared'; -const { - authLinks: { loginURL }, - mainLinks: { dashboardIndex }, -} = StudioCMSRoutes; - export const GET: APIRoute = async (context: APIContext): Promise => { const { url, cookies, redirect } = context; @@ -28,7 +22,7 @@ export const GET: APIRoute = async (context: APIContext): Promise => { const storedState = cookies.get(ProviderCookieName)?.value ?? null; if (!code || !storedState || !codeVerifier || state !== storedState) { - return redirect(loginURL); + return redirect(StudioCMSRoutes.authLinks.loginURL); } let tokens: OAuth2Tokens; @@ -51,23 +45,13 @@ export const GET: APIRoute = async (context: APIContext): Promise => { // // TODO: Add first-time setup logic // } - const existingoAuthAccount = await db - .select() - .from(tsOAuthAccounts) - .where( - and( - eq(tsOAuthAccounts.provider, ProviderID), - eq(tsOAuthAccounts.providerUserId, googleUserId) - ) - ) - .get(); - - if (existingoAuthAccount) { - const user = await db - .select() - .from(tsUsers) - .where(eq(tsUsers.id, existingoAuthAccount.userId)) - .get(); + const existingOAuthAccount = await studioCMS_SDK.AUTH.oAuth.searchProvidersForId( + ProviderID, + googleUserId + ); + + if (existingOAuthAccount) { + const user = await studioCMS_SDK.GET.databaseEntry.users.byId(existingOAuthAccount.userId); if (!user) { return new Response('User not found', { status: 404 }); @@ -75,21 +59,17 @@ export const GET: APIRoute = async (context: APIContext): Promise => { await createUserSession(user.id, context); - return redirect(dashboardIndex); + return redirect(StudioCMSRoutes.mainLinks.dashboardIndex); } const loggedInUser = await getUserData(context); const linkNewOAuth = !!cookies.get(LinkNewOAuthCookieName)?.value; if (loggedInUser.user && linkNewOAuth) { - const existingUser = await db - .select() - .from(tsUsers) - .where(eq(tsUsers.id, loggedInUser.user.id)) - .get(); + const existingUser = await studioCMS_SDK.GET.databaseEntry.users.byId(loggedInUser.user.id); if (existingUser) { - await db.insert(tsOAuthAccounts).values({ + await studioCMS_SDK.AUTH.oAuth.create({ userId: existingUser.id, provider: ProviderID, providerUserId: googleUserId, @@ -97,7 +77,7 @@ export const GET: APIRoute = async (context: APIContext): Promise => { await createUserSession(existingUser.id, context); - return redirect(dashboardIndex); + return redirect(StudioCMSRoutes.mainLinks.dashboardIndex); } } @@ -119,7 +99,7 @@ export const GET: APIRoute = async (context: APIContext): Promise => { await createUserSession(newUser.id, context); - return redirect(dashboardIndex); + return redirect(StudioCMSRoutes.mainLinks.dashboardIndex); } catch (e) { // the specific error message depends on the provider if (e instanceof OAuth2RequestError) { diff --git a/packages/studiocms_auth/src/routes/api/google/shared.ts b/packages/studiocms_auth/src/routes/api/google/shared.ts index 69017ae0..e6dd955b 100644 --- a/packages/studiocms_auth/src/routes/api/google/shared.ts +++ b/packages/studiocms_auth/src/routes/api/google/shared.ts @@ -1,24 +1,16 @@ import { authEnvCheck } from 'studiocms:auth/utils/authEnvCheck'; -import Config from 'studiocms:config'; +import { AuthConfig } from 'studiocms:config'; import { Google } from 'arctic'; export const { - GOOGLE: { CLIENT_ID, CLIENT_SECRET, REDIRECT_URI }, -} = await authEnvCheck(Config.dashboardConfig.AuthConfig.providers); - -const CLIENT = () => { - return { - ID: CLIENT_ID || '', - SECRET: CLIENT_SECRET || '', - URI: REDIRECT_URI || '', - }; -}; + GOOGLE: { CLIENT_ID = '', CLIENT_SECRET = '', REDIRECT_URI = '' }, +} = await authEnvCheck(AuthConfig.providers); export const ProviderID = 'google'; export const ProviderCookieName = 'google_oauth_state'; export const ProviderCodeVerifier = 'google_oauth_code_verifier'; -export const google = new Google(CLIENT().ID, CLIENT().SECRET, CLIENT().URI); +export const google = new Google(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI); export interface GoogleUser { sub: string; diff --git a/packages/studiocms_auth/src/routes/api/logout.ts b/packages/studiocms_auth/src/routes/api/logout.ts index 1505d794..b82030ce 100644 --- a/packages/studiocms_auth/src/routes/api/logout.ts +++ b/packages/studiocms_auth/src/routes/api/logout.ts @@ -7,11 +7,6 @@ import { import { StudioCMSRoutes } from 'studiocms:lib'; import type { APIContext, APIRoute } from 'astro'; -const { - authLinks: { loginURL }, - mainLinks: { baseSiteURL }, -} = StudioCMSRoutes; - export const GET: APIRoute = async (context: APIContext): Promise => { return POST(context); }; @@ -24,7 +19,7 @@ export const POST: APIRoute = async (context: APIContext): Promise => // If there is no session token, redirect to the login page if (!sessionToken) { - return redirect(loginURL); + return redirect(StudioCMSRoutes.authLinks.loginURL); } const { session, user } = await validateSessionToken(sessionToken); @@ -32,12 +27,12 @@ export const POST: APIRoute = async (context: APIContext): Promise => // If there is no session, redirect to the login page if (session === null) { deleteSessionTokenCookie(context); - return redirect(loginURL); + return redirect(StudioCMSRoutes.authLinks.loginURL); } // If there is no user, redirect to the login page if (!user || user === null) { - return redirect(loginURL); + return redirect(StudioCMSRoutes.authLinks.loginURL); } // Invalidate the session and delete the session token cookie @@ -45,7 +40,7 @@ export const POST: APIRoute = async (context: APIContext): Promise => deleteSessionTokenCookie(context); // Redirect to the base site URL - return redirect(baseSiteURL); + return redirect(StudioCMSRoutes.mainLinks.baseSiteURL); }; export const OPTIONS: APIRoute = async () => { diff --git a/packages/studiocms_core/src/sdk-utils/auth/oAuth.ts b/packages/studiocms_core/src/sdk-utils/auth/oAuth.ts index 8bbd0c22..9a3cb378 100644 --- a/packages/studiocms_core/src/sdk-utils/auth/oAuth.ts +++ b/packages/studiocms_core/src/sdk-utils/auth/oAuth.ts @@ -61,6 +61,28 @@ export const authOAuth: STUDIOCMS_SDK['AUTH']['oAuth'] = { ); } }, + searchProvidersForId: async (providerId, userId) => { + try { + return await db + .select() + .from(tsOAuthAccounts) + .where( + and(eq(tsOAuthAccounts.providerUserId, providerId), eq(tsOAuthAccounts.userId, userId)) + ) + .get(); + } catch (error) { + if (error instanceof Error) { + throw new StudioCMS_SDK_Error( + `Error searching for OAuth account: ${error.message}`, + error.stack + ); + } + throw new StudioCMS_SDK_Error( + 'Error searching for OAuth account: An unknown error occurred.', + `${error}` + ); + } + }, }; export default authOAuth; diff --git a/packages/studiocms_core/src/sdk-utils/types/index.ts b/packages/studiocms_core/src/sdk-utils/types/index.ts index d04cd0aa..a89b3e9f 100644 --- a/packages/studiocms_core/src/sdk-utils/types/index.ts +++ b/packages/studiocms_core/src/sdk-utils/types/index.ts @@ -697,6 +697,11 @@ export interface STUDIOCMS_SDK { * @returns A promise that resolves to an object containing the status and message of the deletion operation. */ delete: (userId: string, provider: string) => Promise; + + searchProvidersForId: ( + provider: string, + userId: string + ) => Promise; }; /**