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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="MarthinL" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  MarthinL
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I don’t need to actually touch the token or even retrieve it as long as the token gets transferred into the header for the API call the same way it ends up in other requests the browser makes to the site. I believe I might be able to deal witht cross site scripting issues if need be, but I’d like to try avoid having to if possible. I just need a way to instruct the brower to treat my API get request the same as it would treat page level get requests. Is that possible?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="247867" 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/can-phx-gen-auth-be-used-for-api-authentication/45248/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-247867" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="247867"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="247872" data-post-id="247872">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="MarthinL" data-post="12" data-topic="45248">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/m/3da27b/48.png" class="avatar"> MarthinL:</div>
<blockquote>
<p>I just need a way to instruct the brower to treat my API get request the same as it would treat page level get requests. Is that possible?</p>
</blockquote>
</aside>
<p>It is possible. Then you need to use the cookie based token, and do the following:</p>
<ul>
<li>your front end and api backend must be under the same apex domain</li>
<li>your api backend must have a login UI and set the cookie on the apex domain</li>
<li>your frond end must call the api backend with <code>Access-Control-Allow-Credentials</code></li>
<li>your api backend must white list the front end domain in <code>Access-Control-Allow-Origin</code></li>
<li>if your user has a valid token it should work now. if not, your api backend must return a <code>401 Unauthorized</code> and your front end should redirect to the login screen of your api backend.</li>
<li>if the user successfully log in to the api backend, it should redirect to the front end page found in the <code>referer</code> header</li>
</ul> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="247872" 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/can-phx-gen-auth-be-used-for-api-authentication/45248/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-247872" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="247872"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="247876" data-post-id="247876">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="MarthinL" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  MarthinL
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks, it will take me a while to figure out how to get those things done, but I seems very possible.</p>
<p>As for the front- and back-end being on the same domain, that’s a given in my case since they are either the same machines or live in the same cluster of load-balanced containers at the same public address.</p>
<p>Will the api backend need a SEPERATE login UI from what is there already or can it use the same if I want. It might make sense at some point to redirect differently if the token expires while the user is still using the Vue app so none of their data is lost, but it would help if for a start a token authentication failure 401ing and redirecting to the one and only login page would be OK.</p>
<p>Would 'setting the cookie" be something I need to add manually or is it done in the generated login code already?</p>
<p>I shall go read up about the flags that would need to be set and where to inject those.</p>
<p>Thank you cery much.</p>
<p>Oh, one more thing. I hear about it but don’t understand the difference between cookie based token and whatever other option there is (bearer token perhaps?) and even less about what the risks and benefits are. Is there a simple way to explain that or wil I need to go read a bunch of articles to gain an understanding?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="247876" 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/can-phx-gen-auth-be-used-for-api-authentication/45248/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-247876" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="247876"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="247878" data-post-id="247878">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="MarthinL" data-post="14" data-topic="45248">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/m/3da27b/48.png" class="avatar"> MarthinL:</div>
<blockquote>
<p>Would 'setting the cookie" be something I need to add manually or is it done in the generated login code already?</p>
</blockquote>
</aside>
<p>If your api backend is at the apex then nothing is needed. If not, you need to add <code>domain</code> clause in your <a href="https://hexdocs.pm/plug/Plug.Session.html#module-options" rel="noopener nofollow ugc">session option</a></p>
<p>Bearer token is in a different header and fully controlled by the frontend. So you can do cross domain api calls. You may not be able to use cookie in a mobile app, for example.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="247878" 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/can-phx-gen-auth-be-used-for-api-authentication/45248/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-247878" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="247878"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="247886" data-post-id="247886">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="pupdogg" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  pupdogg
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="thojanssens1" data-post="4" data-topic="45248" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/thojanssens1/48/17642_2.png" class="avatar"> thojanssens1:</div>
<blockquote>
<p>JWT is a token that allows you to maintain user sessions. So what do you mean that they are worse than sessions?</p>
</blockquote>
</aside>
<p>JWT tokens, just on the face value, are in-fact worse off then server-based sessions (specifically db stored sessions). Unlike server-based sessions, you cannot forcefully expire JWT tokens without having a sane JWT storage and a refresh-token strategy in place. So, until the JWT expires, these client side tokens are extremely vulnerable to any and all kinds of malicious attacks.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="247886" 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/can-phx-gen-auth-be-used-for-api-authentication/45248/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-247886" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="247886"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="247892" data-post-id="247892">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="pupdogg" data-post="16" data-topic="45248">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/p/50afbb/48.png" class="avatar"> pupdogg:</div>
<blockquote>
<p>JWT tokens, just on the face value, are in-fact worse off then server-based sessions</p>
</blockquote>
</aside>
<p>Yep. I was then mentioning that it doesn’t make sense to say JWT, used as session mechanism, are “worse than sessions”. A session doesn’t always imply session data stored on the server. The post I replied to got deleted since then.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="247892" data-batch-url="/posts/batch_likers">
                        1
                      </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/can-phx-gen-auth-be-used-for-api-authentication/45248/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-247892" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="247892"
                     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>