<?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: Maurício Andrade Albuquerque</title>
    <description>The latest articles on DEV Community by Maurício Andrade Albuquerque (@mauricio_andrade).</description>
    <link>https://dev.to/mauricio_andrade</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1150164%2Ffc507b4c-0983-41fb-85e2-32445a2702f9.png</url>
      <title>DEV Community: Maurício Andrade Albuquerque</title>
      <link>https://dev.to/mauricio_andrade</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mauricio_andrade"/>
    <language>en</language>
    <item>
      <title>Implementing OpenID Connect (OIDC) Authentication with Nuxt 3, without saving the token in sessionStorage or localstorage</title>
      <dc:creator>Maurício Andrade Albuquerque</dc:creator>
      <pubDate>Wed, 06 Sep 2023 16:05:05 +0000</pubDate>
      <link>https://dev.to/mauricio_andrade/implementing-openid-connect-oidc-authentication-with-nuxt-3-without-saving-the-token-in-sessionstorage-or-localstorage-37oi</link>
      <guid>https://dev.to/mauricio_andrade/implementing-openid-connect-oidc-authentication-with-nuxt-3-without-saving-the-token-in-sessionstorage-or-localstorage-37oi</guid>
      <description>&lt;p&gt;This tutorial use as base:&lt;br&gt;
[&lt;a href="https://dev.to/taikio/implementing-openid-connect-oidc-authentication-with-nuxt-3-2fa4"&gt;https://dev.to/taikio/implementing-openid-connect-oidc-authentication-with-nuxt-3-2fa4&lt;/a&gt;]&lt;br&gt;
You need to read the tutorial above to understand this tutorial here.&lt;br&gt;
This tutorial here did a change to don't save the token in the localstorage or sessionstorage.&lt;br&gt;
The token is only saved in the pinia .&lt;br&gt;
Another change was about change to use Universal render (nuxt's default).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;nuxt.config.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//remove ssr: false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this change you need take care about only call the oidc-client-ts in the client-side, because nuxt will use server and client render (nuxt's default).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;auth-service.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {InMemoryWebStorage} from 'oidc-client-ts';
import { useAuth } from '@/stores/auth';

... code here

//change the line below
userStore: new WebStorageStateStore({ store: new InMemoryWebStorage() }),

... code here

this.userManager = new UserManager(settings);
this.userManager.events.addUserLoaded((user) =&amp;gt; {
      const authStore = useAuth();
      authStore.setUpUserCredentials(user);
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above change the store to memory, then you don't save the user and token in the sessionStorage or localStorage.&lt;br&gt;
If you use getUser the user is not returned, then you need use Pinia to store and get the User and token.&lt;br&gt;
The code addUserLoaded will be used to refresh the token, and will be called automatically by oidc-client-ts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;auth-middleware.global.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useAuth } from '@/stores/auth';

export default defineNuxtRouteMiddleware(async (to, _from) =&amp;gt; {

if (process.server) {
    return
}

const authStore = useAuth();
const {isLoggedIn} = authStore;

if (!authFlowRoutes.includes(to.path) &amp;amp;&amp;amp; !isLoggedIn) {
    services.$auth.signInRedirect();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above you only render in the client, returning when the process is on the server. Nuxt run the middleware on the client and server, and with this modification will run only on the client.&lt;/p&gt;

&lt;p&gt;Then you will do a signIn when the User is not in the Pinia yet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;auth.vue&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script setup&amp;gt;
import { useServices } from '~/composables/useServices';
import { onMounted } from 'vue';
import { useAuth } from '@/stores/auth';

onMounted( async () =&amp;gt; {
    await authenticateOidc();
});

await services.$auth.signInCallback().then(user=&amp;gt; {
        if(user) {
          authStore.setUpUserCredentials(user);
        }
      });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above is called after the signin is done and in the return you are in the auth.vue page.&lt;br&gt;
You need to call the code inside onMounted, because you need to render it on the client side.&lt;br&gt;
After siginCalllback you store the User in the Pinia.&lt;/p&gt;

&lt;p&gt;The last change is to call logout.vue inside onMounted, to run it on the client side.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
