question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hello,

I’m currently trying to implement OAuth2 for my Nuxt 3 (v3.0.0) application. I’m able to request a valid token, login goes through and everything is set properly in the localStorage. Once I refresh the site the localStorage will be cleared {"token":"","model":null}.

I’m using the provided example https://github.com/pocketbase/js-sdk#ssr-integration

Any idea how to prevent it from clearing on a refresh?

Many thanks!

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:19 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
ganigeorgievcommented, Dec 5, 2022

@Ponkhy I’m not able to reproduce the issue. Just tested your code with a new nuxt3 project (aka. npx nuxi init myapp) and it woks fine and the correct cookie is set both client and server side.

Are you authenticating as app user or admin? If the latter then it is possible that the authRefresh call will fail, since it is for the users collection only.

1reaction
Ponkhycommented, Dec 5, 2022

I’ve modified now the Nuxt plugin and it seems to hold the token and user infos even after refreshing. Do you @ganigeorgiev think this is a good practice?

import PocketBase from 'pocketbase';

export default defineNuxtPlugin(async (nuxtApp) => {
  const config = useRuntimeConfig()
  const authCookie = useCookie('pocketbase_auth', { httpOnly: false });

  const pb = new PocketBase(config.public.pocketbase);

  pb.authStore.loadFromCookie(authCookie.value || '');

  pb.authStore.onChange(() => {
    authCookie.value = pb.authStore.exportToCookie({ httpOnly: false });
  });

  try {
    pb.authStore.isValid && await pb.collection('users').authRefresh();
  } catch (_) {
    pb.authStore.clear();
  }

  return {
    provide: { pb }
  }
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

NextJS SSR - JWT (Access/Refresh Token) Authentication ...
Access token : short-lived token (in our example it will be around 10 seconds) that let's user access guarded by content by the...
Read more >
Using OAuth 2.0 for Web Server Applications
Obtaining OAuth 2.0 access tokens ... The following steps show how your application interacts with Google's OAuth 2.0 server to obtain a user's ......
Read more >
Server-Side Apps - OAuth 2.0 Simplified
We've built API access management as a service that is secure, scalable, and always on, so you can ship a more secure product,...
Read more >
Server-Side Rendering - Supabase
Single-page apps with server-side rendering (SSR) is a popular way to optimize ... The first access and refresh tokens after a successful verification...
Read more >
Next.js SSR Refresh Tokens - YouTube
Learn how to get refresh tokens working with Next.js and server side rendering.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found