Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSR with oidc-client #5

Open
rmemon opened this issue Sep 3, 2020 · 2 comments
Open

SSR with oidc-client #5

rmemon opened this issue Sep 3, 2020 · 2 comments

Comments

@rmemon
Copy link

rmemon commented Sep 3, 2020

Hi

How we can store data in quasar cookies instead of local storage for SSR?

@Arsync
Copy link

Arsync commented Sep 3, 2020

Raw answer is:

First, you need to implement CookieStorage like this:

// CookieStorage.ts

import { Cookies } from 'quasar';

// Attention to this to secure your cookies!
const options = {
    sameSite: 'Strict',
    secure: true,
    path: '/'
};

const encode = encodeURIComponent;

export { Cookies };

export default class CookieStorage
{
    private cookies: Cookies;

    public constructor(cookies: Cookies)
    {
        this.cookies = cookies;
    }

    public setItem(key: string, value: any)
    {
        this.cookies.set(encode(key), value, options);
    }

    public getItem(key: string)
    {
        return JSON.stringify(this.cookies.get(encode(key))); //, { doNotParse: true });
    }

    public removeItem(key: string)
    {
        this.cookies.remove(encode(key), options);
    }

    public key(index: number)
    {
        const allKeys = Object.keys(this.cookies.getAll());
        return (index > -1 && index <= allKeys.length) ? allKeys[index] : '';
    }
}

You need to extract right cookies in your custom boot file, then to override userStore field in settings of UserManager:

import { WebStorageStateStore } from 'oidc-client';
import CookieStorage, { Cookies } from './CookieStorage';

export function boot(({ ssrContext }) =>
{
    const cookies = process.env.SERVER
        ? Cookies.parseSSR(ssrContext)
        : Cookies;

    const oidcSettings = {
      //
      // other settings goes here
      //
      userStore: new WebStorageStateStore({
        store: new CookieStorage(cookies),
        //prefix: '' // custom prefix if you like
      }),
    };

    // pass it to new UserManager(oidcSettings);
});

Enjoy!

@rmemon
Copy link
Author

rmemon commented Sep 4, 2020

@Arsync Thank you for the quick help, it works for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants