Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose function reference types for return values of auth() #148

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions src/server/implementation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,36 @@ import {
import { signInImpl } from "./signIn.js";
import { redirectAbsoluteUrl, setURLSearchParam } from "./redirects.js";
import { getAuthorizationUrl } from "../oauth/authorizationUrl.js";
import { defaultCookiesOptions, oAuthConfigToInternalProvider } from "../oauth/convexAuth.js";
import {
defaultCookiesOptions,
oAuthConfigToInternalProvider,
} from "../oauth/convexAuth.js";
import { handleOAuth } from "../oauth/callback.js";
export { getAuthSessionId } from "./sessions.js";

/**
* @internal
* The type of the signIn Convex Action returned from the auth() helper.
*
* This type is exported for implementors of other client integrations.
* However it is not stable, and may change until this library reaches 1.0.
*/
export type SignInAction = FunctionReferenceFromExport<
ReturnType<typeof convexAuth>["signIn"]
>;
/**
* @internal
* The type of the signOut Convex Action returned from the auth() helper.
*
* This type is exported for implementors of other client integrations.
* However it is not stable, and may change until this library reaches 1.0.
*/
export type SignOutAction = FunctionReferenceFromExport<
ReturnType<typeof convexAuth>["signOut"]
>;
/**
* @internal
* The type of the isAuthenticated Convex Query returned from the auth() helper.
*
* This type is exported for implementors of other client integrations.
* However it is not stable, and may change until this library reaches 1.0.
*/
export type IsAuthenticatedQuery = FunctionReferenceFromExport<
ReturnType<typeof convexAuth>["isAuthenticated"]
Expand Down Expand Up @@ -240,12 +252,10 @@ export function convexAuth(config_: ConvexAuthConfig) {
providerId,
) as OAuthConfig<any>;
const { redirect, cookies, signature } =
await getAuthorizationUrl(
{
provider: await oAuthConfigToInternalProvider(provider),
cookies: defaultCookiesOptions(providerId),
},
);
await getAuthorizationUrl({
provider: await oAuthConfigToInternalProvider(provider),
cookies: defaultCookiesOptions(providerId),
});

await callVerifierSignature(ctx, {
verifier,
Expand Down Expand Up @@ -295,10 +305,11 @@ export function convexAuth(config_: ConvexAuthConfig) {
});

const params = url.searchParams;

// Handle OAuth providers that use formData (such as Apple)
if (
request.headers.get("Content-Type") === "application/x-www-form-urlencoded"
request.headers.get("Content-Type") ===
"application/x-www-form-urlencoded"
) {
const formData = await request.formData();
for (const [key, value] of formData.entries()) {
Expand Down Expand Up @@ -441,15 +452,18 @@ export function convexAuth(config_: ConvexAuthConfig) {
return storeImpl(ctx, args, getProviderOrThrow, config);
},
}),

/**
* Utility function for frameworks to use to get the current auth state
* based on credentials that they've supplied separately.
*/
isAuthenticated: queryGeneric({args: {}, handler: async (ctx, _args): Promise<boolean> => {
const ident = await ctx.auth.getUserIdentity();
return ident !== null;
}}),
isAuthenticated: queryGeneric({
args: {},
handler: async (ctx, _args): Promise<boolean> => {
const ident = await ctx.auth.getUserIdentity();
return ident !== null;
},
}),
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export {
modifyAccountCredentials,
Tokens,
Doc,
SignInAction,
SignOutAction,
IsAuthenticatedQuery,
} from "./implementation/index.js";
export type {
ConvexAuthConfig,
Expand Down
Loading