title: Updating/mutating a session with next-auth and tRPC
body: <p>I am building a multi-tenant NextJS app that uses <a href="https://next-auth.js.org/" rel="nofollow noreferrer">next-auth</a> for account authentication, <a href="https://trpc.io/" rel="nofollow noreferrer">tRPC</a> for API's, and postgresql for a data store.</p>
  <p>I am trying to find a way to dynamically update/set/mutate a session value based on some client-side interaction</p>
  <p>The approach I am taking is similar to the one described in <a href="https://blitzjs.com/docs/multitenancy#data-model" rel="nofollow noreferrer">this article</a>:</p>
  <ul>
  <li>a <code>User</code> is granted access to an <code>Organization</code> through a <code>Membership</code></li>
  <li>a <code>User</code> may have a <code>Membership</code> to &gt;1 <code>Organization</code></li>
  <li>a <code>User</code> can change which <code>Organization</code> they are &quot;logged in&quot; to through some client-side UI.</li>
  </ul>
  <p>When the user authenticates, I want to:</p>
  <ul>
  <li>set <code>session.user.orgId</code> to some orgId (if they belong to an org)</li>
  </ul>
  <p>When the user changes the org they are accessing through some client-side UI, I want to:</p>
  <ul>
  <li>update <code>session.user.orgId = newOrgId</code> (validating they have proper permissions before doing so, of course).</li>
  </ul>
  <p>I have searched the net for ways to update/mutate session values, and as far as I can tell, it's only possible using next-auth's <a href="https://next-auth.js.org/configuration/callbacks" rel="nofollow noreferrer">callbacks</a>:</p>
  <pre><code>...
    callbacks: {
      async session({ session, user, token }) {
        // we can modify session here, i.e `session.orgId = 'blah'`
        // or look up a value in the db and attach it here.
        return session
      },
  ...
  }
  </code></pre>
  <p>However, there is no clear way to trigger this update from the client, outside of the authentication flow. I.E, if the user clicks to change their org in some UI, how do I validate the change + update the session value, without requiring the user to re-authenticate?</p>