<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="334122" data-post-id="334122">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="LostKobrakai" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/LostKobrakai/120/3072_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  LostKobrakai
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="caslu" data-post="10" data-topic="64795">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/caslu/48/33615_2.png" class="avatar"> caslu:</div>
<blockquote>
<p>Yeah, i did understand the connection flow but what i’m not getting here is why the connection do not re-assign the user before do the <code>get</code> action even when i pipe through the plug <code>assign_current_user</code> but it does work if i do a manual recycle to connection with <code>recycle/2</code> or build a fresh conn then assign the user to it.</p>
</blockquote>
</aside>
<p>A <code>conn</code> knows and stores if it was already used to make a request (iirc <code>conn.state</code>). So your two approaches are.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># conn was used before
conn = 
  conn 
  # assign current user to the used conn
  |&gt; Pow.Plug.assign_current_user(user, []) 
  # get/2 detects the conn as already being used and recycles it,
  # which doesn't retain the assign you added
  |&gt; get(~p"/api/events/#{id}")
</code></pre>
<p>and</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># conn was used before
conn = 
  build_conn()
  # assign current user to the used conn
  |&gt; Pow.Plug.assign_current_user(user, []) 
  # get/2 detects the conn as not used yet, so no recycling is happening
  # the assign stays
  |&gt; get(~p"/api/events/#{id}")
</code></pre>
<p>You could do <code>conn |&gt; recycle() |&gt; Pow.Plug.assign_current_user(user, [])</code> to work around the automatic recycling happing at an inconvencient time.</p>
<p>You could also consider not writing to <code>conn.assigns</code>, but to put the authentication details in the session. That way authentication will persist across recycles just like it does persist across many requests outside of testing.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="334122" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/testing-pow-authenticated-controllers/64795/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-334122" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="334122"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-solved cat-solved" title="Marked as solution"></div>
  </section>
</div>
    <div class="postbit" id="384016" data-post-id="384016">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="habutre" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/habutre/120/15887_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  habutre
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I was facing the same issue and what worked for me instead of recycling the conn was to take advantage of immutability and not reuse the authenticated connection from setup</p>
<p>I named the conn from context as <code>conn_auth</code>, use it in my calls e.g. <code>conn = get(conn_auth,…)</code></p>
<p>So my authenticated connection was kept untouched and I could assert my responses normally and as much as I need</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="384016" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/testing-pow-authenticated-controllers/64795/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-384016" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="384016"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>