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

Rename signin code to cvxAuthCode #146

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion src/nextjs/server/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export async function proxyAuthActionToConvex(
// Do not require auth when refreshing tokens or validating a code since they
// are steps in the auth flow.
const fetchActionAuthOptions =
args.refreshToken !== undefined || args.params?.code !== undefined
args.refreshToken !== undefined || args.params?.cvxAuthCode !== undefined
? {}
: { token };
try {
Expand Down
8 changes: 4 additions & 4 deletions src/nextjs/server/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ export async function handleAuthenticationInRequest(
const refreshTokens = await getRefreshedTokens(verbose);

// Handle code exchange for OAuth and magic links via server-side redirect
const code = requestUrl.searchParams.get("code");
const cvxAuthCode = requestUrl.searchParams.get("cvxAuthCode");
if (
code &&
cvxAuthCode &&
request.method === "GET" &&
request.headers.get("accept")?.includes("text/html")
) {
logVerbose(`Handling code exchange for OAuth or magic link`, verbose);
const verifier = (await getRequestCookies()).verifier ?? undefined;
const redirectUrl = new URL(requestUrl);
redirectUrl.searchParams.delete("code");
redirectUrl.searchParams.delete("cvxAuthCode");
try {
const result = await fetchAction(
"auth:signIn" as unknown as SignInAction,
{ params: { code }, verifier },
{ params: { cvxAuthCode }, verifier },
);
if (result.tokens === undefined) {
throw new Error("Invalid `signIn` action result for code exchange");
Expand Down
23 changes: 14 additions & 9 deletions src/react/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,17 @@ export function AuthProvider({

const verifyCodeAndSetToken = useCallback(
async (
args: { code: string; verifier?: string } | { refreshToken: string },
args:
| { cvxAuthCode: string; verifier?: string }
| { refreshToken: string },
) => {
const { tokens } = await client.unauthenticatedCall(
"auth:signIn" as unknown as SignInAction,
"code" in args
? { params: { code: args.code }, verifier: args.verifier }
"cvxAuthCode" in args
? {
params: { cvxAuthCode: args.cvxAuthCode },
verifier: args.verifier,
}
: args,
);
logVerbose(`retrieved tokens, is null: ${tokens === null}`);
Expand Down Expand Up @@ -320,20 +325,20 @@ export function AuthProvider({

return;
}
const code =
const cvxAuthCode =
typeof window?.location !== "undefined"
? new URLSearchParams(window.location.search).get("code")
? new URLSearchParams(window.location.search).get("cvxAuthCode")
: null;
// code from URL is only consumed initially,
// ref avoids racing in Strict mode
if (signingInWithCodeFromURL.current || code) {
if (code && !signingInWithCodeFromURL.current) {
if (signingInWithCodeFromURL.current || cvxAuthCode) {
if (cvxAuthCode && !signingInWithCodeFromURL.current) {
signingInWithCodeFromURL.current = true;
const url = new URL(window.location.href);
url.searchParams.delete("code");
url.searchParams.delete("cvxAuthCode");
void (async () => {
await replaceURL(url.pathname + url.search + url.hash);
await signIn(undefined, { code });
await signIn(undefined, { cvxAuthCode });
signingInWithCodeFromURL.current = false;
})();
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/implementation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ export function convexAuth(config_: ConvexAuthConfig) {
headers: {
Location: setURLSearchParam(
destinationUrl,
"code",
"cvxAuthCode",
verificationCode,
),
"Cache-Control": "must-revalidate",
Expand Down
2 changes: 1 addition & 1 deletion src/server/implementation/mutations/verifyCodeAndSignIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ async function verifyCodeOnly(
sessionId: GenericId<"authSessions"> | null,
) {
const { params, verifier } = args;
const codeHash = await sha256(params.code);
const codeHash = await sha256(params.cvxAuthCode);
const verificationCode = await ctx.db
.query("authVerificationCodes")
.withIndex("code", (q) => q.eq("code", codeHash))
Expand Down
7 changes: 4 additions & 3 deletions src/server/implementation/signIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function signInImpl(
}))!;
return { kind: "refreshTokens", signedIn: { tokens } };
}
if (provider === null && args.params?.code !== undefined) {
if (provider === null && args.params?.cvxAuthCode !== undefined) {
const result = await callVerifyCodeAndSignIn(ctx, {
params: args.params,
verifier: args.verifier,
Expand All @@ -72,7 +72,7 @@ export async function signInImpl(

if (provider === null) {
throw new Error(
"Cannot sign in: Missing `provider`, `params.code` or `refreshToken`",
"Cannot sign in: Missing `provider`, `params.cvxAuthCode` or `refreshToken`",
);
}
if (provider.type === "email" || provider.type === "phone") {
Expand Down Expand Up @@ -105,7 +105,8 @@ async function handleEmailAndPhoneProvider(
| { kind: "started"; started: true }
| { kind: "signedIn"; signedIn: SessionInfoWithTokens }
> {
if (args.params?.code !== undefined) {
// TODO this this flow
if (args.params?.cvxAuthCode !== undefined) {
const result = await callVerifyCodeAndSignIn(ctx, {
params: args.params,
provider: provider.id,
Expand Down
1 change: 1 addition & 0 deletions src/server/implementation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export const authTables = {
* - OTP tokens
* - magic link tokens
* - OAuth codes
* Let's call all of these "cvxAuthCodes" to distinguish them from oauth code= param.
*/
authVerificationCodes: defineTable({
accountId: v.id("authAccounts"),
Expand Down
Loading