0

This is my first Nuxt 3 application. I am using oidc-client-ts for authorization and authentication.

First I created this Plugin

import { UserManager, WebStorageStateStore } from "oidc-client-ts";
export default defineNuxtPlugin(({ nuxtApp }) => {
  const userManager = new UserManager({
    authority: "https://localhost:7267",
    client_id: "application-name",
    redirect_uri: "http://localhost:3000/oidc/sign-in-callback.html",
    response_type: "code",
    scope: "openid profile IdentityServerApi role",
    loadUserInfo: true,
    post_logout_redirect_uri: "http://localhost:3000",
    //silent_redirect_uri:"http://localhost:3000",
    userStore: new WebStorageStateStore({ store: window.localStorage }),
  });
  return {
    provide: {
      auth: userManager,
    },
  };

});

Then I created this Pinia store

import { defineStore } from "pinia";

export const useAuthStore = defineStore("authStore", {
  state: () => ({
    user: null,
    loading: true,
  }),
  actions: {
    initialize() {
      this.$auth.getUser().then((user) => {
        if (user) {
          this.state.user = user;
        }
      });
    },
  },
});

I have two questions.

  1. How do you call useAuthStore.initialize() after the Plugin is instantiated?
  2. When I try to call this.$auth.getUser() in the pinia store I get a 500 error
launchCode
  • 23
  • 5

0 Answers0