Giter Club home page Giter Club logo

Comments (5)

victorlmneves avatar victorlmneves commented on July 20, 2024 1

hi @RihanArfan
Thanks for the tip
I was currently looking at this but going to try your approach
If you figure out how to manage the refresh token, please leave a comment, and I'll do the same.

from nuxt-auth-utils.

RihanArfan avatar RihanArfan commented on July 20, 2024 1

@victorlmneves Make sure to include the offline_access scope, and you'll get a refresh token within tokens, assuming you've enabled the relevant settings within your Auth0 dashboard (or in my case, via Azure).

I'm working on a server util getAccessToken(session) which would handle refreshing an expired access token. I'm thinking of storing the refresh token via useStorage(), and the result of getAccessToken() using Nitro's caching layer with a TTL a little less than the JWT expiry. https://nitro.unjs.io/guide/cache#function (which uses unstorage under the hood too).

If the refresh token doesn't work (90+ days unused, user changed pw/removed device from trusted, etc. in Microsoft specifically), I'll redirect them to the auth endpoint.

from nuxt-auth-utils.

RihanArfan avatar RihanArfan commented on July 20, 2024

Had the same query and figured it out. It's exposed where you register the OAuth event handler.

// /server/routes/auth/sso.ts
export default oauth.microsoftEventHandler({
  async onSuccess(event, { user, tokens }) { // <-- tokens exposed here
    console.log("Tokens", tokens);

    await setUserSession(event, {
      user: {
        microsoft: user,
      },
      loggedInAt: Date.now(),
    });

    return sendRedirect(event, "/");
  },
});

Be aware that storing the token within setUserSession() will expose it to the client at /api/_auth/session. That's fine in examples like a client calling GitHub API using their access token to access their own resources. However, if substancial scopes are granted (e.g. deleting devices on Active Directory), you may want to store the access token on the server, and keep a reference to it within setUserSession() so you can access the token within requests to server routes.

from nuxt-auth-utils.

victorlmneves avatar victorlmneves commented on July 20, 2024

hi @RihanArfan
I know that the question is to be able to expose it to be to access it without having to add to the object
Currently, I'm doing like this, but I don't think it's safe to do it

export default oauth.auth0EventHandler({
    config: authConfig(),
    async onSuccess(event, { user, tokens }) {
        const userStore = useUserStore();
        userStore.$state.isLoggedIn = true;
        userStore.$state.user = user;

        await setUserSession(event, {
            user: {
                login: user,
            },
            loggedInAt: Date.now(),
            auth0AccessToken: tokens.access_token,
        });

        return sendRedirect(event, '/');
    },
});

from nuxt-auth-utils.

RihanArfan avatar RihanArfan commented on July 20, 2024

However, if substancial scopes are granted (e.g. deleting devices on Active Directory), you may want to store the access token on the server, and keep a reference to it within setUserSession() so you can access the token within requests to server routes.

Here's an example of what I'm doing:

// server/routes/auth.ts
export default oauth.microsoftEventHandler({
  async onSuccess(event, { user, tokens }) {
    await useStorage().setItem(`token:${user.id}`, tokens.access_token);

    await setUserSession(event, { user, loggedInAt: Date.now() });
    return sendRedirect(event, "/");
  },
});
// server/api/example.get.ts
export default defineEventHandler(async (event) => {
  const session = await requireUserSession(event);
  const accessToken = await useStorage().getItem(`token:${(session.user as { id: string }).id}`);
  // TODO: validate JWT is valid

  // TODO: your code
  ...
});

You'll still need to handle refresh tokens otherwise your application won't be able to call an API after 1 hour (most common access token JWT expiry). I'm figuring out how to handle refresh tokens still.

from nuxt-auth-utils.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.