[#370] Automatically add members to KH Discord server#373
Merged
DVidal1205 merged 7 commits intomainfrom Feb 17, 2026
Merged
Conversation
📝 WalkthroughWalkthroughAdds Discord OAuth handling: two new utility functions to process OAuth callbacks and add users to a guild, plus a post-session database hook that triggers this flow. The Discord social provider scope now includes Changes
Sequence DiagramsequenceDiagram
participant Auth as Auth System
participant DB as Database
participant Util as API Utils
participant Discord as Discord API
Auth->>DB: create session for user
Auth->>Util: handleDiscordOAuthCallback(discordUserId)
Util->>DB: query User and Account (latest Discord account)
alt account exists & has guilds.join scope
Util->>Util: extract access token
Util->>Discord: POST /users/@me/guilds/{guildId} (add member) with token
Discord-->>Util: response (success/failure)
Util-->>Auth: log success/failure
else missing account or scope
Util-->>Auth: log no-op / error
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~40 minutes Suggested labels
🚥 Pre-merge checks | ✅ 5 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@apps/blade/src/app/api/auth/callback/discord/route.ts`:
- Around line 12-26: Don't query the globally most-recent Discord account (the
db.query.Account.findFirst call with Account.provider === "discord"); instead
import betterAuthInstance from your auth config, extract the session tied to
this callback by reading the response's Set-Cookie header, use that session to
look up the authenticated user/account, and pass that user's discordUserId into
handleDiscordOAuthCallback so the callback runs for the current OAuth session
rather than the latest DB record.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/auth/src/config.ts`:
- Around line 63-75: The post-session hook (databaseHooks.session.create.after)
can throw during the DB lookup (db.query.User.findFirst using User.id and
session.userId) and should be made resilient: wrap the entire async hook body in
a try/catch, call handleDiscordOAuthCallback(discordUserId) as before but catch
and log any errors (using the existing logger) and ensure the function
returns/ends without re-throwing so session creation is not blocked by failures.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/auth/tsconfig.json`:
- Line 5: Update the tsconfig "lib" entry to match the project standard: replace
"ES2021", "DOM", "DOM.Iterable" with "ES2022", "dom", "dom.iterable" so the
packages/auth tsconfig aligns with other configs; edit the "lib" array in
packages/auth's tsconfig.json accordingly to use ES2022 and lowercase dom
entries.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Users aren't guaranteed to be in the Knight Hacks discord server while interacting with Blade. This can cause issues i.e., users not properly being assigned roles upon submitting a form.
What
Issue(s): #370
addMemberToServerhandleDiscordOAuthCallbackdatabaseHooksto trigger Discord auto joinsession.create.afterhook that readsuser.discordUserIdand invokeshandleDiscordOAuthCallbackso users are automatically added to the Discord server after logging inguilds.joinTest Plan
370.mp4
Checklist
db:pushbefore mergingSummary by CodeRabbit