Sronds
Sronds
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
My end result looks something like this
async createOrUpdateUser(ctx, args) {
if (args.existingSessionId && args.provider.id === "tiktok") {
const session: Doc<"authSessions"> | undefined | null =
await ctx.db.get(args.existingSessionId);
if (session) {
const user: Doc<"users"> | undefined | null = await ctx.db.get(
session.userId,
);
if (user) {
const creator = await ctx.db
.query("creators")
// @ts-ignore
.withIndex("by_user", (q) => q.eq("userId", user._id))
.first();
if (creator) {
await ctx.db.patch(creator._id, {
tiktokHandle: args.profile.email,
});
return user._id;
} else {
await ctx.db.insert("creators", {
userId: user._id,
tiktokHandle: args.profile.email,
});
return user._id as any;
}
}
}
} else {
// returning undefined will trigger the default implementation
return undefined;
}
}
async createOrUpdateUser(ctx, args) {
if (args.existingSessionId && args.provider.id === "tiktok") {
const session: Doc<"authSessions"> | undefined | null =
await ctx.db.get(args.existingSessionId);
if (session) {
const user: Doc<"users"> | undefined | null = await ctx.db.get(
session.userId,
);
if (user) {
const creator = await ctx.db
.query("creators")
// @ts-ignore
.withIndex("by_user", (q) => q.eq("userId", user._id))
.first();
if (creator) {
await ctx.db.patch(creator._id, {
tiktokHandle: args.profile.email,
});
return user._id;
} else {
await ctx.db.insert("creators", {
userId: user._id,
tiktokHandle: args.profile.email,
});
return user._id as any;
}
}
}
} else {
// returning undefined will trigger the default implementation
return undefined;
}
}
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
I managed to solve it by patching the users.js convex implementation.
if (config.callbacks?.createOrUpdateUser !== undefined) {
logWithLevel(LOG_LEVELS.DEBUG, "Using custom createOrUpdateUser callback");
const res = await config.callbacks.createOrUpdateUser(ctx, {
existingUserId,
--> existingSessionId,
...args,
});
---> if (res) {
---> return res;
---> }
}
if (config.callbacks?.createOrUpdateUser !== undefined) {
logWithLevel(LOG_LEVELS.DEBUG, "Using custom createOrUpdateUser callback");
const res = await config.callbacks.createOrUpdateUser(ctx, {
existingUserId,
--> existingSessionId,
...args,
});
---> if (res) {
---> return res;
---> }
}
I now pass the existingSessionId in the callback async createOrUpdateUser(ctx, args) which i then use to check if there's an existing session and use that to link the accounts together. If there is no existing session and the provider is not one of my linking ones, then i just return undefined in the callback so that the defaultCreateOrUpdateUser function continues the logic.
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
I get the following log after signing in
'defaultCreateOrUpdateUser args:' {
existingAccountId: undefined,
existingSessionId: 'jh7261gqn4nherc0vy4r24nmqx7fh8qt',
...
'defaultCreateOrUpdateUser args:' {
existingAccountId: undefined,
existingSessionId: 'jh7261gqn4nherc0vy4r24nmqx7fh8qt',
...
is there perhaps a way to connect to use that existingSessionId to link the accounts?
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
okay just a quick follow up, this flow creates a new user to the database with tiktok as the provider but that's not necessarily what i'm trying to do. My goal is to 'link' the tiktok account to the existing user that had already signed in similar to what OP was trying to do i believe. How different do you reckon the flow should be compared to what we have right now? @erquhart
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
if anyone in the future is struggling with this, the fix is to go to node_modules/@convex-dev/auth/dist/server/oauth/callback.js and update
let codeGrantResponse = await o.authorizationCodeGrantRequest(as, client, clientAuth, codeGrantParams, redirect_uri, codeVerifier ?? "decoy", {
// TODO: move away from allowing insecure HTTP requests
[o.allowInsecureRequests]: true,
[o.customFetch]: (...args) => {
if (!provider.checks.includes("pkce")) {
args[1].body.delete("code_verifier");
}
return fetchOpt(provider)[o.customFetch](...args);
},
});
let codeGrantResponse = await o.authorizationCodeGrantRequest(as, client, clientAuth, codeGrantParams, redirect_uri, codeVerifier ?? "decoy", {
// TODO: move away from allowing insecure HTTP requests
[o.allowInsecureRequests]: true,
[o.customFetch]: (...args) => {
if (!provider.checks.includes("pkce")) {
args[1].body.delete("code_verifier");
}
return fetchOpt(provider)[o.customFetch](...args);
},
});
to
let codeGrantResponse = await o.authorizationCodeGrantRequest(as, client, clientAuth, codeGrantParams, redirect_uri, codeVerifier ?? "decoy", {
// TODO: move away from allowing insecure HTTP requests
[o.allowInsecureRequests]: true,
[o.customFetch]: (...args) => {
if (!provider.checks.includes("pkce")) {
args[1].body.delete("code_verifier");
}
return fetchOpt(provider)[o.customFetch](...args);
},
additionalParameters: {
client_key: options.provider.clientId,
client_secret: options.provider.clientSecret ?? ""
}
});
let codeGrantResponse = await o.authorizationCodeGrantRequest(as, client, clientAuth, codeGrantParams, redirect_uri, codeVerifier ?? "decoy", {
// TODO: move away from allowing insecure HTTP requests
[o.allowInsecureRequests]: true,
[o.customFetch]: (...args) => {
if (!provider.checks.includes("pkce")) {
args[1].body.delete("code_verifier");
}
return fetchOpt(provider)[o.customFetch](...args);
},
additionalParameters: {
client_key: options.provider.clientId,
client_secret: options.provider.clientSecret ?? ""
}
});
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
❤️ ❤️ ❤️
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
that. worked. tysm @erquhart, i was going mad
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
i've traced it to this error:
'OAuth Token Response Body:' '{"error":"invalid_request","error_description":"The request parameters are malformed.","log_id":"202505081903505897F995CEADA71635F2"}'
'OAuth Token Response Body:' '{"error":"invalid_request","error_description":"The request parameters are malformed.","log_id":"202505081903505897F995CEADA71635F2"}'
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
this is my patch file
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
yeah still getting the same error even when returning false
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
imma try again real quick
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
but didn't get much luck with getting a different error message although it could've been because i didn't patch it properly
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
but i tried patching it myself
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
your pr hasn't been merged in yet
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
@erquhart
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
the reason i think this error is related to op's message is because i get this as the response after going through the process of confirming the login on tiktok. https://bright-shark-383.convex.site/api/auth/callback/tiktok?code=6Be6A-lAdB9_CRbJ5HvibBKpM44QmFKWLY6cAmmb9hLj1jviZCMzK4iAsy8yVNLQCXXidcRutCH-wS6p2PHynC18hH7dwkfKjx4lPcwL7ndRcg9kba2-nuTes-gvE0zloF0ZvLzOB4bYbBKOkaXujrotbLW-qqZR688A8aYVYEayg0zqcSG7iZUALAss6lmQ2JsS5QkYfWVbZ2WdqFmsyML5F4MHVofQT_5KSFvf04w%2A2%215730.e1&scopes=user.info.basic%2Cuser.info.profile but then for some reason it fails and returns the error '"response" body "access_token" property must be a string and the full flow never completes and i thought it could be related to the code param
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
i'm just doing
const { signIn } = useAuthActions();
...
<Button
onClick={async () => {
await signIn("tiktok")
}}
const { signIn } = useAuthActions();
...
<Button
onClick={async () => {
await signIn("tiktok")
}}
and my auth.ts file looks like this
import Google from "@auth/core/providers/google";
import { ResendOTP } from "./otp/ResendOTP";
import TikTok from "@auth/core/providers/tiktok";
import { convexAuth } from "@convex-dev/auth/server";
export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
providers: [
ResendOTP,
Google,
TikTok({
clientId: process.env.AUTH_TIKTOK_ID,
clientSecret: process.env.AUTH_TIKTOK_SECRET,
}),
],
});
import Google from "@auth/core/providers/google";
import { ResendOTP } from "./otp/ResendOTP";
import TikTok from "@auth/core/providers/tiktok";
import { convexAuth } from "@convex-dev/auth/server";
export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
providers: [
ResendOTP,
Google,
TikTok({
clientId: process.env.AUTH_TIKTOK_ID,
clientSecret: process.env.AUTH_TIKTOK_SECRET,
}),
],
});
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
I wonder if the issue stems from already being logged in and trying to sign in again with TikTok? it's not
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
I already have the user logging in through Gmail this was just me trying to get them to “connect”/“link” their account so that I could verify that they indeed owned the account
60 replies
CCConvex Community
Created by uma on 3/29/2025 in #support-community
callback, 3rd party authentication, convexAuth
But I’m just using it cause I thought it would be easy to implement, my actual goal is just to get the user to verify they own the TikTok account (was planning to do Instagram & YouTube later too the same way)
60 replies