<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Quang Dang</title>
    <description>The latest articles on DEV Community by Quang Dang (@dakiquang).</description>
    <link>https://dev.to/dakiquang</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F378486%2Fc5e7fc0b-d0fd-4808-a6c3-4e9fabc630eb.jpeg</url>
      <title>DEV Community: Quang Dang</title>
      <link>https://dev.to/dakiquang</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dakiquang"/>
    <language>en</language>
    <item>
      <title>Understand enough to decide: keeping humans in charge of AI-written code</title>
      <dc:creator>Quang Dang</dc:creator>
      <pubDate>Sun, 29 Nov 2026 15:10:06 +0000</pubDate>
      <link>https://dev.to/dakiquang/understand-enough-to-decide-keeping-humans-in-charge-of-ai-written-code-2l8h</link>
      <guid>https://dev.to/dakiquang/understand-enough-to-decide-keeping-humans-in-charge-of-ai-written-code-2l8h</guid>
      <description>&lt;p&gt;AI coding agents now write code that runs. What they do not hand you is a clear view of what changed, what was checked, and what is still a guess, so you can decide whether to merge. I keep three rules for that: the one who makes a change is not its final checker, reports separate evidence from claims, and checking stops at "enough to decide".&lt;/p&gt;

&lt;p&gt;I built these rules into &lt;a href="https://github.com/dakiquang/qk" rel="noopener noreferrer"&gt;qk&lt;/a&gt;, a small set of open-source skills for Claude Code, Codex and omp. You do not need qk to use them. The rest of this post is the rules themselves.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem is not the code
&lt;/h2&gt;

&lt;p&gt;A few months ago my bottleneck was writing code. Now it is reading reports like this one:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Implemented the export feature. All tests pass. The edge cases are handled.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Which tests? Did the agent run them, or does it believe they pass? Which edge cases, and who decided they were the right ones? A report like that asks me to trust it. I would rather check it, in the few minutes I have.&lt;/p&gt;

&lt;p&gt;Most agent kits answer this by giving the AI more autonomy: longer pipelines, more subagents, more steps that run by themselves. That helps the agent do more. It does not help me decide.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rule 1: three roles, make, check, decide
&lt;/h2&gt;

&lt;p&gt;Whoever makes a change is not its final checker. Authority to commit, push, merge or publish stays with a person, and it does not carry over to the next session.&lt;/p&gt;

&lt;p&gt;In practice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A small, reversible change needs no second model. A command that runs, such as the test suite, is the checker.&lt;/li&gt;
&lt;li&gt;A larger change gets a reviewer from a &lt;strong&gt;different model family&lt;/strong&gt;. When one model writes the code, a model from another family reviews it. Independent checks earn their cost: in one of my runs the review came back clean, and a later cross-audit still found two important defects.&lt;/li&gt;
&lt;li&gt;A new decision, or a new kind of authority, means the agent stops and asks. "You approved the plan" does not mean "you approved the push".&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Rule 2: evidence, not claims
&lt;/h2&gt;

&lt;p&gt;Before starting, agree what "done" means. When reporting, separate three kinds of statement:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Seen:&lt;/strong&gt; commands that were run and their results, files that were read.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reviewed:&lt;/strong&gt; what another party checked.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Told:&lt;/strong&gt; what the agent believes but did not verify.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then say what was not checked. That last part is the one most reports leave out, and the one I need most.&lt;/p&gt;

&lt;p&gt;Every qk report ends with the same four lines, in this order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Done: what was produced or changed, with paths
Evidence: commands run and their results, sources read
Not verified: what was not checked, and why
Needs your decision: what waits on you
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last line is the only one you must read. If it says "none", you are done. You can paste these four labels into your own &lt;code&gt;CLAUDE.md&lt;/code&gt; or &lt;code&gt;AGENTS.md&lt;/code&gt; today and ask your agent to end with them; you do not need qk for that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rule 3: enough
&lt;/h2&gt;

&lt;p&gt;The first two rules have a failure mode. Separate roles and demand evidence without a limit, and you get agents that check and recheck, spending tokens without adding understanding.&lt;/p&gt;

&lt;p&gt;So the third rule is the brake:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check only what could change the decision, once, in the cheapest way that is sufficient.&lt;/li&gt;
&lt;li&gt;Reuse what the session already saw.&lt;/li&gt;
&lt;li&gt;At most one review round and one fix round. A disagreement that remains goes to you, not into a loop.&lt;/li&gt;
&lt;li&gt;List the rest as "not verified" and move on.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;"Enough" is not "skip the checks". What could change the decision must still be seen. It is permission to stop checking what could not.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this looks like in qk
&lt;/h2&gt;

&lt;p&gt;qk is thirteen skills, about 52 KB of instructions, and nothing loads until you call a skill by name. Each skill does one job and names the next command; skills never call each other, so you decide whether to run it.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Role&lt;/th&gt;
&lt;th&gt;Skills&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Offer options and evidence, not choose&lt;/td&gt;
&lt;td&gt;brainstorm, research&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Make&lt;/td&gt;
&lt;td&gt;cook, plan, release-notes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Check&lt;/td&gt;
&lt;td&gt;test, plan (audit), debug, dogfood&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hand over work, not authority&lt;/td&gt;
&lt;td&gt;handoff, agent-coord&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Carry out authority you granted&lt;/td&gt;
&lt;td&gt;commit, prefs, morning&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;When I want more automation, &lt;code&gt;qk run&lt;/code&gt; chains the roles for one task. One agent implements the change. The runner runs the plan's own checks. A model from another family reviews. At most one fix round follows, and a pull request opens. I approve the plan at the start and merge at the end. It can automate making and checking, never deciding.&lt;/p&gt;

&lt;p&gt;To try it with Node.js installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx skills add &lt;span class="s1"&gt;'dakiquang/qk#v0.12.0'&lt;/span&gt; -g -a claude-code codex qwen-code -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, in Claude Code: &lt;code&gt;/qk-brainstorm Should this service read its config from environment variables or a file?&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What I do not know yet
&lt;/h2&gt;

&lt;p&gt;I would be overselling if I stopped there. I have measured some of this and not all of it. On small eval tasks, qk did no better than using no kit at all, or using Superpowers. On one hard review task it was the only setup to report defects as blocker or important, where the others saw at most one of them and called it minor; it also used the most tokens. That comparison, with its limits, is the subject of the next post.&lt;/p&gt;

&lt;p&gt;qk is a personal project, published as is. It is maintained for my own work and versioned with tags, but I make no support commitment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Read next:&lt;/strong&gt; &lt;a href="https://github.com/dakiquang/qk/blob/v0.12.0/docs/en/philosophy.md" rel="noopener noreferrer"&gt;the philosophy page&lt;/a&gt; · &lt;a href="https://github.com/dakiquang/qk/blob/v0.12.0/docs/en/evidence.md#kit-comparison" rel="noopener noreferrer"&gt;the kit comparison data&lt;/a&gt; · &lt;a href="https://github.com/dakiquang/qk" rel="noopener noreferrer"&gt;the repository&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>claude</category>
      <category>codereview</category>
      <category>opensource</category>
    </item>
    <item>
      <title>How to run multiple Claude Desktop accounts on a Mac (free, MIT)</title>
      <dc:creator>Quang Dang</dc:creator>
      <pubDate>Fri, 27 Nov 2026 04:05:47 +0000</pubDate>
      <link>https://dev.to/dakiquang/how-to-run-multiple-claude-desktop-accounts-on-a-mac-free-mit-4cjo</link>
      <guid>https://dev.to/dakiquang/how-to-run-multiple-claude-desktop-accounts-on-a-mac-free-mit-4cjo</guid>
      <description>&lt;p&gt;Claude Desktop only allows one account at a time, so there's no built-in way to keep multiple Claude Desktop accounts signed in. If you have a work account and a personal one (or a Team seat + your own Pro), you have to sign out and sign back in every time you switch, and you lose your place and your context.&lt;/p&gt;

&lt;p&gt;I got tired of that, so I made &lt;a href="https://github.com/dakiquang/claude-desktop-twins" rel="noopener noreferrer"&gt;claude-desktop-twins&lt;/a&gt;, a small free CLI (MIT) that clones Claude Desktop once per account.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew install dakiquang/tap/claude-desktop-twins
cdt add work@example.com
cdt add personal@example.com
cdt open work
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it. Each account becomes its own app in &lt;code&gt;~/Applications/Claude Desktop Twins/&lt;/code&gt;. Separate Dock icon, separate Cmd-Tab entry, separate notifications. Open it once, sign in, done.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F02wzbpk7ru9jzfa1jo4n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F02wzbpk7ru9jzfa1jo4n.png" alt="cdt add creates one app per account" width="799" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why just using &lt;code&gt;--user-data-dir&lt;/code&gt; isn’t enough
&lt;/h2&gt;

&lt;p&gt;You can already launch a second instance like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;open -n -a &lt;span class="s2"&gt;"Claude"&lt;/span&gt; --args --user-data-dir=&lt;span class="s2"&gt;"$HOME/Library/Application Support/Claude-Work"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works for signing in, but macOS still treats both windows as the same app. Same Dock icon, same notification settings. When a notification comes in, clicking it opens whichever copy macOS feels like. With two accounts that’s how you accidentally reply from the wrong one.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the tool actually does
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;cdt add&lt;/code&gt; makes an APFS clone of &lt;code&gt;/Applications/Claude.app&lt;/code&gt; (almost no extra disk space) and changes a few things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Gives it a unique bundle ID and display name so macOS sees it as a different app&lt;/li&gt;
&lt;li&gt;Adds a small launcher that points to its own data folder&lt;/li&gt;
&lt;li&gt;Ad-hoc signs it so notifications work&lt;/li&gt;
&lt;li&gt;Turns on Claude’s “Block auto-updates” for that copy only (so the original can still update normally)&lt;/li&gt;
&lt;li&gt;Handles the notification permission prompt for you&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nothing from Anthropic is shipped with the tool. Every copy is made on your machine from the Claude.app you already have.&lt;/p&gt;

&lt;h2&gt;
  
  
  Daily use
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cdt open work        &lt;span class="c"&gt;# open or bring to front&lt;/span&gt;
cdt                  &lt;span class="c"&gt;# pick from a menu&lt;/span&gt;
cdt list             &lt;span class="c"&gt;# see what’s running and which clones are outdated&lt;/span&gt;
cdt doctor           &lt;span class="c"&gt;# check if everything is healthy&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdrp999457lsadynt3yxb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdrp999457lsadynt3yxb.png" alt="cdt list" width="799" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cdt doctor&lt;/code&gt; checks each copy’s signature, version, update block and notification permission:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fygk6pznii0dh3mj7uksg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fygk6pznii0dh3mj7uksg.png" alt="cdt doctor output for two accounts" width="800" height="565"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After Claude Desktop updates, open the original app once so it updates, then rebuild any account that &lt;code&gt;cdt list&lt;/code&gt; marks as stale:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cdt rebuild work@example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also set a custom icon per account if you want them easier to tell apart in the Dock:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cdt icon &lt;span class="nb"&gt;set&lt;/span&gt; work@example.com ~/Pictures/work.icns
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Claude Code note
&lt;/h2&gt;

&lt;p&gt;The Code tab still shares &lt;code&gt;~/.claude&lt;/code&gt; across all copies. If you also use Claude Code in the terminal with multiple accounts, just point each one to its own config:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;CLAUDE_CONFIG_DIR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;~/.claude-work claude
&lt;span class="nv"&gt;CLAUDE_CONFIG_DIR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;~/.claude-personal claude
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Limitations (being honest)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;macOS only (12+)&lt;/li&gt;
&lt;li&gt;Needs Claude Desktop in &lt;code&gt;/Applications&lt;/code&gt; and Xcode Command Line Tools&lt;/li&gt;
&lt;li&gt;Passkeys and Secure Enclave don’t work in the copies (they’re tied to Anthropic’s signature)&lt;/li&gt;
&lt;li&gt;macOS will ask for keychain access again after every rebuild&lt;/li&gt;
&lt;li&gt;Copies don’t auto-update — you update the original then run &lt;code&gt;cdt rebuild&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Unofficial and not affiliated with Anthropic. If Anthropic ever ships proper multi-account support, you may not need this&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prefer a menu bar app?
&lt;/h2&gt;

&lt;p&gt;There’s also a paid version that sits in the menu bar and lets you switch with ⌥1–⌥9, update all accounts in one click, and see usage limits. The CLI stays free and does everything above on its own:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://claudedesktoptwins.com/" rel="noopener noreferrer"&gt;Claude Desktop Twins Premium&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzvjqknrkpka48hwzk5uo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzvjqknrkpka48hwzk5uo.png" alt="The Premium menu bar app: every account with its plan limits left, a shortcut per account, and an update button (example accounts)" width="712" height="1270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Questions or something broke on your setup? Just open an issue:&lt;br&gt;&lt;br&gt;
&lt;a href="https://github.com/dakiquang/claude-desktop-twins/issues" rel="noopener noreferrer"&gt;github.com/dakiquang/claude-desktop-twins/issues&lt;/a&gt;&lt;/p&gt;

</description>
      <category>claude</category>
      <category>ai</category>
      <category>productivity</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Laravel OAuth refresh token race condition: fixing invalid_grant with QuickBooks, Xero, Salesforce, Microsoft and Google</title>
      <dc:creator>Quang Dang</dc:creator>
      <pubDate>Sat, 07 Nov 2026 23:57:05 +0000</pubDate>
      <link>https://dev.to/dakiquang/laravel-oauth-refresh-token-race-condition-fixing-invalidgrant-with-quickbooks-xero-salesforce-1ea1</link>
      <guid>https://dev.to/dakiquang/laravel-oauth-refresh-token-race-condition-fixing-invalidgrant-with-quickbooks-xero-salesforce-1ea1</guid>
      <description>&lt;p&gt;&lt;em&gt;By Quang Dang (&lt;a href="https://github.com/dakiquang" rel="noopener noreferrer"&gt;github.com/dakiquang&lt;/a&gt;), a product-minded tech lead with 15+ years building software, 10 of them in SaaS · tested 7 November 2026 · last updated 8 November 2026&lt;/em&gt; · &lt;a href="https://quangdk.com/writing/race-condition-refresh-token-oauth-laravel-invalid-grant" rel="noopener noreferrer"&gt;Bản tiếng Việt&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your Laravel queue has eight workers (&lt;code&gt;queue:work&lt;/code&gt; or Horizon), and a scheduled command runs at the same time. They all pick up work for the same connected account at the same moment, and the access token has just expired. Each one notices and calls the token endpoint with the same refresh token. That is the OAuth refresh token race condition, and it is behind many "random" &lt;code&gt;invalid_grant&lt;/code&gt; errors.&lt;/p&gt;

&lt;p&gt;What happens next depends entirely on the provider. With some, nothing visible. With others, the user loses the connection and has to reconnect. I built a small Laravel package for this, &lt;a href="https://github.com/dakiquang/laravel-oauth-refresh-lock" rel="noopener noreferrer"&gt;laravel-oauth-refresh-lock&lt;/a&gt;, and tested it against real QuickBooks Online, Xero and Salesforce accounts (Microsoft and Google not yet). This post goes through each provider, the other causes of &lt;code&gt;invalid_grant&lt;/code&gt;, and then the fix.&lt;/p&gt;

&lt;p&gt;This is about your app as an OAuth &lt;em&gt;client&lt;/em&gt; of someone else's API. If your app issues tokens itself (Passport, Sanctum or a JWT package), that is a different problem; Sanctum has no refresh tokens at all.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Short answer.&lt;/strong&gt; When several workers refresh one OAuth token at once, providers that rotate refresh tokens may reject or revoke the duplicate. QuickBooks Online and Salesforce can cost you the connection, Xero hides the race behind a 30-minute grace period, and Microsoft and Google just waste a call. The fix: one &lt;code&gt;Cache::lock&lt;/code&gt; per connection, a re-read of the token inside the lock, and a compare-and-swap that saves the new access and refresh token in one write.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the OAuth refresh token race condition?
&lt;/h2&gt;

&lt;p&gt;The OAuth refresh token race condition happens when two workers send the same refresh token at once to a provider that uses refresh token rotation. Most providers that rotate refresh tokens issue a new refresh token on every refresh and retire the old one. The first one wins. What the second one gets is the provider's choice: a second valid token, an &lt;code&gt;invalid_grant&lt;/code&gt; error, or the revocation of the whole connection.&lt;/p&gt;

&lt;p&gt;The harshest outcome comes from &lt;strong&gt;refresh token reuse detection&lt;/strong&gt;. &lt;a href="https://datatracker.ietf.org/doc/html/rfc9700" rel="noopener noreferrer"&gt;RFC 9700&lt;/a&gt; (OAuth 2.0 Security Best Current Practice) describes it: when an already-rotated refresh token is presented, the authorization server cannot tell the attacker from the legitimate client, so it revokes the active refresh token too (some providers call this revoking the token family). A second worker of your own looks exactly like an attacker.&lt;/p&gt;

&lt;h2&gt;
  
  
  Case studies: refresh token rotation by provider
&lt;/h2&gt;

&lt;p&gt;Provider behaviour comes from each provider's documentation, linked in each section. The "real run" results come from test accounts I created for this: a QuickBooks Online sandbox company, Xero's Demo Company and a Salesforce Developer Edition org, each with a fresh Laravel 12 app.&lt;/p&gt;

&lt;h3&gt;
  
  
  QuickBooks Online: a duplicate refresh gets &lt;code&gt;invalid_grant&lt;/code&gt; and can cost the connection
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What the documentation says.&lt;/strong&gt; Intuit changes the QuickBooks Online refresh token value every 24 hours, and when a new value is issued the previous one expires. If two refreshes use the same value, the first succeeds and the second gets &lt;code&gt;invalid_grant&lt;/code&gt;, and Intuit may treat it as a security issue and revoke the refresh tokens. Intuit asks you to refresh one at a time (&lt;a href="https://developer.intuit.com/app/developer/qbo/docs/develop/authentication-and-authorization/faq" rel="noopener noreferrer"&gt;Intuit OAuth 2.0 FAQ&lt;/a&gt;). The FAQ does not say whether a duplicate inside the same 24-hour window also fails; plan as if it does.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real run.&lt;/strong&gt; QuickBooks Online sandbox company. Six processes started at the same moment with an expired token, three rounds. In every round one process refreshed and the other five used the token it stored. No &lt;code&gt;invalid_grant&lt;/code&gt;; the final token was valid, and the refresh response reported &lt;code&gt;expires_in: 3600&lt;/code&gt;. One round, grouped by outcome:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   1 REFRESHED the token, company "Sandbox Company …"
   5 used the stored token, company "Sandbox Company …"
6 processes, 1 refresh.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No run without the lock: it could revoke the sandbox connection. That risk comes from Intuit's FAQ, not from my own test.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Takeaway.&lt;/strong&gt; This is the case the package is for: without serialisation, a busy queue can disconnect a customer's books.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt; &lt;a href="https://github.com/dakiquang/laravel-oauth-refresh-lock/tree/main/examples/quickbooks" rel="noopener noreferrer"&gt;examples/quickbooks&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Xero refresh token rotation: a 30-minute grace period hides the race
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What the documentation says.&lt;/strong&gt; Xero returns a new refresh token on every refresh. If your app does not receive or save the new one, it can retry with the previous refresh token for 30 minutes; after that the previous token expires and the user has to authorise again (&lt;a href="https://developer.xero.com/documentation/guides/oauth2/token-types" rel="noopener noreferrer"&gt;Xero token types&lt;/a&gt;). Xero does not name the error for a refresh token used after the grace period; OAuth 2.0 defines &lt;code&gt;invalid_grant&lt;/code&gt; for an invalid, expired or revoked refresh token.&lt;/p&gt;

&lt;p&gt;Two different 30-minute figures are easy to mix up: the &lt;em&gt;access token&lt;/em&gt; lasts 30 minutes, and the &lt;em&gt;previous refresh token&lt;/em&gt; stays usable for 30 minutes after a rotation. The refresh token itself lasts up to 60 days.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real run.&lt;/strong&gt; Xero Demo Company. With the package: six processes, four rounds, one refresh per round.&lt;/p&gt;

&lt;p&gt;Then a control experiment, without any lock: six processes refreshed at the same moment with the &lt;strong&gt;same&lt;/strong&gt; refresh token. All six succeeded, and Xero issued a different new refresh token to each of them. So with Xero the race does not show up as an error. Six workers now hold six different refresh tokens; an app that stores what each worker received ends up with whichever one was written last. Whether the other siblings stay valid after 30 minutes is not documented, and I have not measured it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Takeaway.&lt;/strong&gt; Xero hides the race, so an integration that ignores it looks fine in testing; the lock avoids creating sibling tokens whose fate after 30 minutes is unknown.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt; &lt;a href="https://github.com/dakiquang/laravel-oauth-refresh-lock/tree/main/examples/xero" rel="noopener noreferrer"&gt;examples/xero&lt;/a&gt; (HTTP Basic auth at the token endpoint, tenant from the connections endpoint)&lt;/p&gt;

&lt;h3&gt;
  
  
  Salesforce refresh token rotation: a short grace, then the connection is revoked
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What the documentation says.&lt;/strong&gt; With Salesforce Refresh Token Rotation on, every refresh returns a new refresh token and the previous one is invalidated at once. Salesforce treats refresh token reuse as a compromise: reusing an invalidated refresh token revokes the current refresh token and its access tokens too, so the user has to authenticate again (&lt;a href="https://help.salesforce.com/s/articleView?id=005316711&amp;amp;type=1" rel="noopener noreferrer"&gt;Refresh Token Rotation&lt;/a&gt;). Rotation is on by default, and cannot be turned off, for new External Client Apps since 22 October 2026 (&lt;a href="https://help.salesforce.com/s/articleView?id=005390912&amp;amp;type=1" rel="noopener noreferrer"&gt;External Client App defaults&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;The token response also has no &lt;code&gt;expires_in&lt;/code&gt;: the session timeout is configured in Salesforce, so an app learns that an access token is dead when an API call answers 401. That broke my first design, because a package that refreshes "shortly before expiry" never refreshes a token without an expiry. So the package gained &lt;code&gt;rejected()&lt;/code&gt;, for the usual "401, then refresh, then retry" flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$pending&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OAuthRefreshLock&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$accessToken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$pending&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;accessToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$refresher&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nv"&gt;$response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Http&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;withToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$accessToken&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$response&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;401&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nv"&gt;$response&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'0.errorCode'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'INVALID_SESSION_ID'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$accessToken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$pending&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;rejected&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$accessToken&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;accessToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$refresher&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nv"&gt;$response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Http&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;withToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$accessToken&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;rejected()&lt;/code&gt; refreshes while the stored access token is still the one that failed, under the same lock. Workers that get the same 401 cause one refresh: the first refreshes, and the others find a different access token stored and use it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real run.&lt;/strong&gt; A Salesforce Developer Edition org with a new External Client App. Setting it up taught two things the documentation does not stress: Refresh Token Rotation, PKCE and a 30-day idle refresh token limit are switched on and locked for new apps, so the authorisation request has to send PKCE; and &lt;code&gt;login.salesforce.com&lt;/code&gt; answered &lt;code&gt;invalid_client_id&lt;/code&gt; for the brand-new app while the org's My Domain worked at once.&lt;/p&gt;

&lt;p&gt;With the package: I replaced the stored access token with one Salesforce would reject, as after a session timeout, and started the processes: six in each of three rounds, then three in a fourth. In every round each process got a 401 and reported it with &lt;code&gt;rejected()&lt;/code&gt;; one refreshed and the others used its token. The connection was kept. The refresh responses had no &lt;code&gt;expires_in&lt;/code&gt; and a new refresh token every time, as documented.&lt;/p&gt;

&lt;p&gt;Then the control experiments, without the lock:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Six processes refreshed at the same moment with the same refresh token. Five succeeded, each with a different new refresh token, and one got &lt;code&gt;concurrent_request&lt;/code&gt;. All five access tokens worked, and the old refresh token still refreshed a few seconds later. So Salesforce tolerates a reuse for a short moment, which its article on rotation does not mention.&lt;/li&gt;
&lt;li&gt;I rotated a refresh token once and reused the old one 90 seconds later. &lt;code&gt;invalid_grant&lt;/code&gt;, and the current refresh token stopped working too: the whole connection was revoked, exactly as the article says.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How long the tolerance lasts I did not measure; somewhere between a few seconds and 90. A worker that is slow, retries late or holds a stale token falls outside it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Takeaway.&lt;/strong&gt; The strictest provider here. Concurrent refreshes may get away with it for a moment, but a late reuse ends the connection, and refreshes have to be driven by 401s (&lt;code&gt;INVALID_SESSION_ID&lt;/code&gt;) rather than by an expiry time. The package now also treats &lt;code&gt;concurrent_request&lt;/code&gt; as a temporary error; Salesforce's &lt;a href="https://help.salesforce.com/s/articleView?id=xcloud.remoteaccess_oauth_refresh_token_flow.htm&amp;amp;language=en_US&amp;amp;type=5" rel="noopener noreferrer"&gt;refresh token flow page&lt;/a&gt; warns that simultaneous requests with the same refresh token fail intermittently and suggests retrying them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt; &lt;a href="https://github.com/dakiquang/laravel-oauth-refresh-lock/tree/main/examples/salesforce" rel="noopener noreferrer"&gt;examples/salesforce&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Microsoft and Google refresh tokens: little to lose
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Microsoft.&lt;/strong&gt; Microsoft identity platform refresh tokens replace themselves with a new one on every use, but the old one is not revoked when used (&lt;a href="https://learn.microsoft.com/en-us/entra/identity-platform/refresh-tokens" rel="noopener noreferrer"&gt;Microsoft refresh tokens&lt;/a&gt;). Duplicate refreshes waste calls; they do not break the connection.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Google.&lt;/strong&gt; Google does not return a new refresh token when you refresh; you keep using the one you have (&lt;a href="https://developers.google.com/identity/protocols/oauth2/web-server" rel="noopener noreferrer"&gt;Google OAuth 2.0 for web server apps&lt;/a&gt;). There is no rotation race. Refresh tokens can still stop working for other reasons, listed in the next section.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Takeaway.&lt;/strong&gt; For these two, serialising refreshes saves calls rather than connections. When a Microsoft refresh token really is dead you get &lt;code&gt;invalid_grant&lt;/code&gt; with an &lt;code&gt;AADSTS&lt;/code&gt; code such as &lt;code&gt;AADSTS70008&lt;/code&gt; (&lt;a href="https://learn.microsoft.com/en-us/entra/identity-platform/reference-error-codes" rel="noopener noreferrer"&gt;error codes&lt;/a&gt;); with Google, &lt;code&gt;invalid_grant&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt; &lt;a href="https://github.com/dakiquang/laravel-oauth-refresh-lock/tree/main/examples/microsoft" rel="noopener noreferrer"&gt;examples/microsoft&lt;/a&gt; · &lt;a href="https://github.com/dakiquang/laravel-oauth-refresh-lock/tree/main/examples/google" rel="noopener noreferrer"&gt;examples/google&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary: what a duplicate refresh does, per provider
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Provider&lt;/th&gt;
&lt;th&gt;Old refresh token after a refresh&lt;/th&gt;
&lt;th&gt;A duplicate refresh&lt;/th&gt;
&lt;th&gt;Tested&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;QuickBooks Online&lt;/td&gt;
&lt;td&gt;expires&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;invalid_grant&lt;/code&gt;, may revoke the tokens&lt;/td&gt;
&lt;td&gt;real sandbox&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Salesforce (rotation on)&lt;/td&gt;
&lt;td&gt;usable for a few seconds, then invalid&lt;/td&gt;
&lt;td&gt;within seconds: succeeds or &lt;code&gt;concurrent_request&lt;/code&gt;; later: revokes the whole connection&lt;/td&gt;
&lt;td&gt;real Developer Edition org, plus no-lock controls&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Xero&lt;/td&gt;
&lt;td&gt;usable for 30 minutes&lt;/td&gt;
&lt;td&gt;succeeds and issues another token&lt;/td&gt;
&lt;td&gt;real Demo Company, plus a no-lock control&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Microsoft&lt;/td&gt;
&lt;td&gt;not revoked&lt;/td&gt;
&lt;td&gt;wasted call&lt;/td&gt;
&lt;td&gt;documentation only&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Google&lt;/td&gt;
&lt;td&gt;not rotated&lt;/td&gt;
&lt;td&gt;wasted call&lt;/td&gt;
&lt;td&gt;documentation only&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Is &lt;code&gt;invalid_grant&lt;/code&gt; always a race?
&lt;/h2&gt;

&lt;p&gt;No. OAuth 2.0 uses &lt;code&gt;invalid_grant&lt;/code&gt; for any refresh token that is invalid, expired, revoked or issued to another client (&lt;a href="https://datatracker.ietf.org/doc/html/rfc6749#section-5.2" rel="noopener noreferrer"&gt;RFC 6749, section 5.2&lt;/a&gt;). Before blaming concurrency, rule these out (all from the providers' documentation, linked above and in the repo's &lt;a href="https://github.com/dakiquang/laravel-oauth-refresh-lock/blob/main/docs/providers.md" rel="noopener noreferrer"&gt;providers page&lt;/a&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The new refresh token was not saved.&lt;/strong&gt; QuickBooks Online and Salesforce retire the previous value; Xero allows it for only 30 minutes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unused for too long.&lt;/strong&gt; QuickBooks Online: 100-day rolling expiry. Xero: up to 60 days. Salesforce: a 30-day idle limit on new External Client Apps. Microsoft: &lt;code&gt;AADSTS70008&lt;/code&gt; or &lt;code&gt;AADSTS700082&lt;/code&gt; after inactivity. Google: six months unused, or 7 days for apps still in "Testing" with external users.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Access was revoked.&lt;/strong&gt; The user disconnected the app, or (Microsoft, &lt;code&gt;AADSTS50173&lt;/code&gt;) the grant was revoked, for example after a password reset.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Too many tokens.&lt;/strong&gt; Google invalidates the oldest when one account has more than 100 refresh tokens for the same client.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Wrong client or environment.&lt;/strong&gt; A refresh token from one app's keys sent with another app's keys, such as sandbox keys against production.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Salesforce policy and restrictions.&lt;/strong&gt; An app's refresh token policy can expire refresh tokens immediately, after a fixed time or when unused (&lt;a href="https://help.salesforce.com/s/articleView?id=xcloud.connected_app_manage_oauth.htm&amp;amp;language=en_US&amp;amp;type=5" rel="noopener noreferrer"&gt;OAuth access policies&lt;/a&gt;), and Salesforce's &lt;a href="https://help.salesforce.com/s/articleView?id=sf.remoteaccess_oauth_flow_errors.htm&amp;amp;language=en_US&amp;amp;type=5" rel="noopener noreferrer"&gt;error list&lt;/a&gt; also gives "IP restricted or invalid login hours" as an &lt;code&gt;invalid_grant&lt;/code&gt; cause, without naming a flow. A deactivated user gets its own code, &lt;code&gt;inactive_user&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A late reuse after rotation.&lt;/strong&gt; Salesforce revokes the whole connection.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With Google, many write-ups quote the &lt;code&gt;error_description&lt;/code&gt; &lt;code&gt;Token has been expired or revoked.&lt;/code&gt;; Google's documentation only says the token "may have expired or has been invalidated".&lt;/p&gt;

&lt;h2&gt;
  
  
  Race or real revocation? Reading the logs
&lt;/h2&gt;

&lt;p&gt;The error alone does not tell you. These signals do:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Two refresh calls for the same connection within seconds.&lt;/strong&gt; Log the connection key and a timestamp on every refresh call. A race leaves two or more calls close together; a real expiry or revocation leaves one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The refresh token sent had already been rotated.&lt;/strong&gt; Log a short hash of the refresh token sent and received (never the token itself). If a failing call sent a value that an earlier call had already rotated, it was a race.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The provider's own hints.&lt;/strong&gt; Salesforce answers one of two simultaneous refreshes with &lt;code&gt;concurrent_request&lt;/code&gt;. Microsoft puts the &lt;code&gt;AADSTS&lt;/code&gt; code in &lt;code&gt;error_description&lt;/code&gt;, which separates inactivity (&lt;code&gt;AADSTS70008&lt;/code&gt;) from a revoked grant (&lt;code&gt;AADSTS50173&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With the package, each connection should show one &lt;code&gt;TokenRefreshed&lt;/code&gt; event per expiry, and &lt;code&gt;TokenRevokedByProvider&lt;/code&gt; carries the error code. Neither event carries token values.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix: one &lt;code&gt;Cache::lock&lt;/code&gt; per token, plus compare-and-swap
&lt;/h2&gt;

&lt;p&gt;The minimal fix is a mutex per connection: a distributed lock (Redis lock, database lock) around the refresh, so only one worker calls the token endpoint and the others wait and use its result. Some call this pattern single-flight. A sketch with Laravel's &lt;code&gt;Cache::lock&lt;/code&gt; and your own token model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"oauth-refresh:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;block&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$provider&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Token&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'key'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;firstOrFail&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;          &lt;span class="c1"&gt;// read again inside the lock&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$token&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;expires_at&lt;/span&gt;&lt;span class="o"&gt;?-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;isAfter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;addMinutes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;   &lt;span class="c1"&gt;// expires_at: nullable 'datetime' cast&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$token&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                           &lt;span class="c1"&gt;// another worker already refreshed&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nv"&gt;$new&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$provider&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getAccessToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'refresh_token'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'refresh_token'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$token&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;refresh_token&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="nv"&gt;$token&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
        &lt;span class="s1"&gt;'access_token'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$new&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getToken&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="s1"&gt;'refresh_token'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$new&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getRefreshToken&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="nv"&gt;$token&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;refresh_token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'expires_at'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$new&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getExpires&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setTimestamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$new&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getExpires&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$token&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Give the token columns Laravel's &lt;code&gt;encrypted&lt;/code&gt; cast; the package's own table does. That already stops most duplicate refreshes. It is not enough on its own: the lock is a lease, so a refresh slower than 30 seconds lets a second worker in (see below); a write from a stale read can overwrite a newer token; a network blip and a real &lt;code&gt;invalid_grant&lt;/code&gt; look the same; and with no &lt;code&gt;expires_in&lt;/code&gt; (Salesforce), &lt;code&gt;expires_at&lt;/code&gt; is null, so the sketch refreshes on every call instead of on a 401. The package handles those:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$accessToken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OAuthRefreshLock&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"quickbooks:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;$realmId&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;accessToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LeagueRefresher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$provider&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Fast path.&lt;/strong&gt; Read the stored token; if the access token is valid for longer than the skew (two minutes by default, &lt;code&gt;skew_seconds&lt;/code&gt;) and was not reported as rejected, return it without taking a lock.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One lock per token key&lt;/strong&gt; with &lt;code&gt;Cache::lock&lt;/code&gt;, from any Laravel cache store that supports atomic locks (Redis, Memcached, DynamoDB, database).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read again inside the lock.&lt;/strong&gt; Workers that waited almost always find the token the first worker stored, and return it without calling the provider.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compare-and-swap write&lt;/strong&gt; on a version number. Connects and reconnects are unconditional writes that start from a time-based version after a disconnect, so a worker holding an old read does not overwrite a newer connection.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Classify errors.&lt;/strong&gt; The network errors the default classifier recognises, and HTTP 408, 429 and 5xx, keep the token and throw &lt;code&gt;TransientRefreshFailure&lt;/code&gt;. Only &lt;code&gt;invalid_grant&lt;/code&gt; marks it revoked.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Which Laravel lock?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Works across servers&lt;/th&gt;
&lt;th&gt;Covers jobs, web requests and commands&lt;/th&gt;
&lt;th&gt;Re-reads the token&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;Cache::lock&lt;/code&gt; on Redis, Memcached, DynamoDB or database&lt;/td&gt;
&lt;td&gt;yes, with a shared store&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;if you re-read inside the lock&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;lockForUpdate()&lt;/code&gt; in a database transaction (SQLite ignores it)&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes, but the transaction stays open during the HTTP call to the provider&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;WithoutOverlapping&lt;/code&gt; job middleware&lt;/td&gt;
&lt;td&gt;yes, with a shared store&lt;/td&gt;
&lt;td&gt;queued jobs only&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;file&lt;/code&gt; cache lock&lt;/td&gt;
&lt;td&gt;no, one server only&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;if you re-read inside the lock&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;array&lt;/code&gt; cache lock&lt;/td&gt;
&lt;td&gt;no, one process only&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Lock or conditional write? You need both
&lt;/h2&gt;

&lt;p&gt;A popular argument is to use a conditional write instead of a distributed lock. A conditional write ("update only if the version is unchanged") does protect the store, but both workers still call the provider, and the provider sees the second call. In my no-lock runs, Xero issued six sibling refresh tokens and Salesforce answered &lt;code&gt;concurrent_request&lt;/code&gt;, then revoked the whole connection on a late reuse. A lock alone has the opposite gap: it is a lease, and the lease can run out mid-refresh. With the default 30-second lease:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 s    A  takes the lock, sends RT1 (slow)
30 s      lease ends; A's call is still in flight
31 s   B  takes the lock, re-reads: still RT1, sends RT1   ← provider sees RT1 twice
32 s   B  gets invalid_grant, records a revocation of that version
35 s   A  gets RT2; compare-and-swap fails, sees B revoked the version A rotated, stores RT2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;One possible order, where B gets &lt;code&gt;invalid_grant&lt;/code&gt; (QuickBooks, Salesforce).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So the package does both: the lock stops the second call, and the version number, which works like a fencing token, keeps the store consistent when the lock is not enough. Whether A's new tokens still work depends on the provider: Salesforce may already have revoked everything after the late reuse. With Xero (within 30 minutes) or Microsoft, B's refresh usually just succeeds; A's compare-and-swap then fails, the store keeps B's tokens, and the provider is left with two token generations. Keep the refresh call's HTTP timeout well below the lease. Details in &lt;a href="https://github.com/dakiquang/laravel-oauth-refresh-lock/blob/main/docs/how-it-works.md#a-refresh-that-outlives-the-lock" rel="noopener noreferrer"&gt;how it works&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;WithoutOverlapping&lt;/code&gt; is not a substitute: it covers only queued jobs with the same key and never re-reads the token (see the table above).&lt;/p&gt;

&lt;h2&gt;
  
  
  Save every new refresh token, in one write
&lt;/h2&gt;

&lt;p&gt;Store the access token, refresh token and expiry from every refresh response together, in one write. Intuit says this outright: keep the latest refresh token value from every response. If you save the access token first and the refresh token in a second write, a crash in between leaves you with a refresh token that has already been retired.&lt;/p&gt;

&lt;p&gt;If a response has no &lt;code&gt;refresh_token&lt;/code&gt;, as with Google, keep the one you have. The package does this: a refresh without a new refresh token keeps the stored one.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to do after &lt;code&gt;invalid_grant&lt;/code&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Re-read the stored token once.&lt;/strong&gt; Another worker may have refreshed while your call was in flight, and your refresh token was simply stale. The package does this: if the stored version moved during the call, it returns what the other worker stored instead of marking the token revoked.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stop retrying.&lt;/strong&gt; Sending the same dead refresh token again cannot succeed, and with reuse detection it can do more damage. The package marks the token revoked and throws &lt;code&gt;TokenRevoked&lt;/code&gt;; later calls throw it straight away without calling the provider.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ask the user to reconnect.&lt;/strong&gt; Listen for &lt;code&gt;TokenRevokedByProvider&lt;/code&gt; to notify the user or flag the integration. Check the stored token before acting on it, as the README explains.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Refresh before expiry, on 401, or on a schedule?
&lt;/h2&gt;

&lt;p&gt;Refresh shortly before expiry when the provider gives one, and on a 401 when it does not (Salesforce) or when a token dies early. The package does both: the two-minute skew for the first, &lt;code&gt;rejected()&lt;/code&gt; for the second.&lt;/p&gt;

&lt;p&gt;A scheduled command that refreshes every token before it expires is one more worker. It still collides with jobs that find a token expired at the same moment, so it still needs the lock. It also has nothing to schedule against with Salesforce, whose tokens have no expiry. Jitter on a scheduled refresh spreads the load across connections, but neither jitter nor the two-minute skew stops two workers racing on one connection.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it was tested
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A concurrency test forks eight PHP processes that wait at a barrier and then all ask for the same expiring (or rejected) token, for three rounds. It runs with the database lock on SQLite, and in CI with the database lock and the Redis lock on MySQL, three times per job. A control run without the lock must show more than one refresh; otherwise the harness would prove nothing.&lt;/li&gt;
&lt;li&gt;CI caught a real bug along the way: on Laravel 10, the database cache lock deadlocked on MySQL when many workers competed for one lock row (Laravel 11 and 12 share that lock code, so the fix covers them too). A concurrency error while acquiring the lock now counts as a lost race and the worker keeps waiting; once a refresh has started nothing is retried.&lt;/li&gt;
&lt;li&gt;Real runs: QuickBooks Online, Xero and Salesforce, as above.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It coordinates one application that shares one cache store. Horizon workers on several servers are coordinated as long as they share that store (Redis, for example). Two different apps using the same OAuth connection are not coordinated.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rejected()&lt;/code&gt; recognises a completed refresh by the new access token value; a provider that returned the same value again would cause one refresh per caller that reported it.&lt;/li&gt;
&lt;li&gt;Laravel 10 and 11 are supported on a best-effort basis. Both are past &lt;a href="https://laravel.com/docs/releases#support-policy" rel="noopener noreferrer"&gt;Laravel's security support window&lt;/a&gt; and have &lt;a href="https://github.com/laravel/framework/security/advisories" rel="noopener noreferrer"&gt;published advisories&lt;/a&gt; that will not be patched, so upgrade if you can (&lt;a href="https://github.com/dakiquang/laravel-oauth-refresh-lock/blob/main/docs/support-policy.md" rel="noopener noreferrer"&gt;support policy&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require dakiquang/laravel-oauth-refresh-lock league/oauth2-client
php artisan vendor:publish &lt;span class="nt"&gt;--tag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;oauth-refresh-lock-migrations
php artisan migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Supports Laravel 10–13 on PHP 8.1–8.5 (each Laravel version's own PHP minimum applies). The README covers connecting, refreshing, &lt;code&gt;rejected()&lt;/code&gt;, disconnecting, errors and events, and the repo has runnable examples for QuickBooks Online, Xero, Salesforce, Microsoft Graph and Google. Reports from providers I have not tested yet are especially welcome.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A personal open source project, not affiliated with any company or OAuth provider.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>oauth</category>
      <category>api</category>
    </item>
  </channel>
</rss>
