cookie.</li><li><code>secure: true</code> ensures that the cookie is only sent over secure, encrypted connections (HTTPS). but since most testing environments are on localhost, it is allowed</li></ul><p id="6bc1">then executing this action looks like:</p><div id="e2cb"><pre><span class="hljs-keyword">import</span> { storeToken } <span class="hljs-keyword">from</span> <span class="hljs-string">"@/lib/actions"</span>;
}</pre></div><p id="173a">After testing this check your cookie storage in devtools and the token should be set:</p><figure id="de3a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*VxsyZ7KTXg_XygebpIfq1Q.png"><figcaption></figcaption></figure><p id="7f8d">Now let’s move on to using the token to communicate with our API.</p><h1 id="ad3b">2. Accessing the Token</h1><p id="3425">So accessing the token is different depending on the context. If you are accessing it on the server it looks like:</p><div id="ba24"><pre>import {cookies} <span class="hljs-keyword">from</span> <span class="hljs-string">"next/headers"</span>;
<span class="hljs-keyword">const</span> authToken = cookies().<span class="hljs-keyword">get</span>(<span class="hljs-string">"accessToken"</span>)?.<span class="hljs-keyword">value</span> </pre></div><p id="636a">In the Client side, because we set <code>httpOnly: true</code> . How do we now access our jwt? That’s where creating an api route comes in. It would have been really cool to have a server action for retrieving the token but we don’t have that yet. “fix up Next.js team”!</p><p id="dc9e">Docs for api routes using the app router <a href="https://nextjs.org/docs/app/building-your-application/routing/route-handlers">here</a></p><p id="c93d">so the gist is a folder structure of <code>app/api/auth/token/route.ts</code> resolves to an endpoint with path of <code>/api/auth/token</code> . Now in the route.ts file we write:</p><div id="916b"><pre>import { cookies } <span class="hljs-keyword">from</span> <span class="hljs-string">'next/headers'</span>
}</pre></div><p id="66db">Now let’s break down what this api route does:</p><ul><li>we retrieve the jwt from the cookie store</li><li>we make a request to /user on our api to check if the token is still valid. /user can be replaced with any protected route on your api</li><li>if we receive the <code>unauthorized(401)</code> error code we send a request to get a new token pair with our refresh token.</li><li>then we return the valid token back to the client for use.</li></ul><p id="260f">Now, to make use of this API route we can write an axios interceptor like so:</p><div id="b457"><pre>axiosInstance.<span class="hljs-property">interceptors</span>.<span class="hljs-property">request</span>.<span class="hljs-title function_">use</span>(<span class="hljs-keyword">async</span> (config) => {
<span class="hljs-keyword">if</span> (config.<span class="hljs-property">url</span>?.<span class="hljs-title function_">includes</span>(<span class="hljs-string">"auth"</span>)) {
<span class="hljs-keyword">return</span> config
}
)</pre></div><p id="8f27">What this interceptor does is: “if the app is making a request to an auth route like <code>/auth/login</code> , <code>/auth/signup</code> no token is sent along with the request but if it is to a protected route it fetches the access token from our api route and passes it in the <code>Authorization</code> header.</p><h2 id="c7b2">Further reading:</h2><p id="fb63">This article was greatly inspired by:</p><div id="b5f3" class="link-block">
<a href="https://javascript.plainenglish.io/next-js-secure-authentication-using-http-only-cookie-graphql-or-rest-a4ef94cec9e8">
<div>
<div>
<h2>Next.js Secure Authentication Using http-only Cookie (GraphQL or REST)</h2>
<div><h3>When it comes to user authentication we need to make sure our application is secured from any potential threats. To…</h3></div>
<div><p>javascript.plainenglish.io</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*FBBZ5ksEs057sEfEkcl42g.png)"></div>
</div>
</div>
</a>
</div><p id="7edc">If you want to see how to do this with graphql. or you are working on older versions of Next without server actions, I suggest the article.</p><p id="948d">Thanks for reading and if you have any questions, drop them in the comments.</p><p id="50be">Happy building!🚀</p><h1 id="d403">Stackademic</h1><p id="3a1b"><i>Thank you for reading until the end. Before you go:</i></p><ul><li><i>Please consider <b>clapping</b> and <b>following</b> the writer! 👏</i></li><li><i>Follow us on <a href="https://twitter.com/stackademichq"><b>Twitter(X)</b></a>, <a href="https://www.linkedin.com/company/stackademic"><b>LinkedIn</b></a>, and <a href="https://www.youtube.com/c/stackademic"><b>YouTube</b></a><b>.</b></i></li><li><i>Visit <a href="http://stackademic.com/"><b>Stackademic.com</b></a> to find out more about how we are democratizing free programming education around the world.</i></li></ul></article></body>
What Is Your Relationship With Expressing Emotions?
Sunny Side Updates with The Brain is a Noodle || Week 59
It’s easy to crack a joke that makes me laugh. As long as they’re not offensive and hurtful, I pretty much like them all, lame to brilliant.
Yet on the flip side, it takes a lot to make me cry. I don’t cry … at the usual things. I don’t cry at loss. I don’t cry about deaths. I don’t cry about negative things.
I mostly cry about instances when people come together and actually do something altruistic. Those moments in tv shows really hit me. It really made me reflect on why, helping me to realize that I cry because I don’t naturally believe in the hope that people can be kind, regardless of how much I promote it. Maybe … that’s why I promote it in the first place. It seems rare and we, as humanity, need more of it to happen.
Digging deep into why, how and when I express emotions has been so helpful in elucidating which beliefs I have. This brings me to this week’s shortform prompt:
What is your relationship with expressing emotions? Which one(s) do you express most easily? Which one(s) are difficult to express or feel? What are some reasons you think that’s the case?
I’m looking forward to hearing more about your stance!
This week’s shortform prompt
What is your relationship with expressing emotions? Which one(s) do you express most easily? Which one(s) are difficult to express or feel? What are some reasons you think that’s thecase?
🧠🍜 [3] Your Cognitive Health Is Very Important by Isabel Young
‘There are many reasons why it is so important that you should pay attention to your cognitive health. The importance of cognitive health has become a major concern for most people.’
🧠🍜 [4] The House of Rules by Kyomi O'Connor
‘The House of Rules
determines the rules of housing,
yet subsequently, it projects the judgment
how well you fit or how badly you fail in the house.’
🧠🍜 [7] I Am Here in the Shadows by Obinna Uruakpa
‘It is a little too early
to celebrate my departure
or to roll out the valedictory drums
’cause I am not really gone.’
🧠🍜 [8] Value of Patience by Tarun Gupta
‘Patience is a virtue. It is the most important key in any journey. When traveling, it requires patience until we reach the destination. During cooking, we need patience until the vegetables soften.’
🧠🍜 [9] White Boy Tears by Kate Lynch
‘Kyle Rittenhouse cried on the witness stand. He’s white, and was found not guilty. If you don’t know the story, look it up. There is something to be learned here. White parents, we can do something with our disillusion and emptiness.’
🧠🍜 [10] Whys of Lives by Tarun Gupta
‘These days everyone has become subject to an external locus. They don’t have an identity of themselves.’
🧠🍜 [13] Caring or caring? by Dennett
‘Five days hospitalized —
not a card or a bouquet,
not an email, call, or text.’
🧠🍜 [14] Signs from Within by Mark Tulin
‘There are signs within me,
rules on how to live,
I harbor unconsciously,
guideposts on my journey,
the world instilled in me’
🧠🍜 [17] Regaining My True Self by Imad
‘I was in a barren place
for forlorn minds;
where words could not find,
devoid of inspiration,
for in that space
I could not rhyme.’
🧠🍜 [19] Old Haunts and Hauntings by Dennett
‘We went to downtown, haven’t been in so long,
used to be one of our haunts, favorite spots to be,
back when being meant more than staying home’
🧠🍜 [28] I feel lighter by Ali
‘Anxiety strapped across my back. That’s its way of Boldly taking a stance Calling itself my best friend while’
🧠🍜 [29] Addiction to Pain by Tarun Gupta
‘Humans are natural addicts. We want to keep doing something that we enjoy irrespective of either a negative or a positive reason.’