How to use the nookies.parseCookies function in nookies

To help you get started, we’ve selected a few nookies examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github codeleakteam / codeleak / nextjs / helpers / functions / auth.js View on Github external
export const auth = ctx => {
  const { codeleakAuthToken, codeleakUser } = parseCookies(ctx)
  const isLoggedIn = !!codeleakAuthToken && !!codeleakUser
  // console.log('[auth] isLoggedIn ', isLoggedIn)
  // console.log('[auth] codeleakUser', codeleakUser)
  // console.log('[auth] codeleakAuthToken', codeleakAuthToken)

  // Protected routes are the ones we show only to users that are logged in
  let isProtectedRoute
  let isGuestRoute

  // SSR
  if (ctx.req) {
    isProtectedRoute = protectedRoutes.filter(r => r === ctx.req.url)[0] ? true : false
    isGuestRoute = guestRoutes.filter(r => r === ctx.req.url)[0] ? true : false

    if (isProtectedRoute && !isLoggedIn) {
      ctx.res.writeHead(302, { location: '/sign_in' })
github dpla / dpla-frontend / pages / search / index.js View on Github external
Search.getInitialProps = async context => {
  const query = context.query;
  const req = context.req;
  const isLocal = SITE_ENV === "local";
  const isQA = parseCookies(context).hasOwnProperty("qa");
  const currentUrl = getCurrentUrl(req);
  const q = query.q
    ? encodeURIComponent(query.q).replace(/'/g, "%27").replace(/"/g, "%22")
    : "";

  let hasDates = false;
  const theseFacets = isQA ? qaFacets : possibleFacets;

  const queryArray = theseFacets
    .map(facet => {
      if (facet.indexOf("sourceResource.date") !== -1 && !hasDates) {
        hasDates = true; // do it only once for date queries
        // the date “facets” from ES do not map to the way the API expects requests
        // remove whatever is after the last periot (“begin” or “end”)
        facet = facet.replace(".begin", "");
        facet = facet.replace(".end", "");
github ShadOoW / web-starter-kit / pages / _app.js View on Github external
static async getInitialProps({ Component, ctx }) {
    let pageProps = {};

    // On server-side, this runs once and creates new services
    // On client-side, this always reuses existing services

    const cookies = parseCookies(ctx);

    const mobxServices = getServices({
      language: cookies['next-i18next'] || i18n.language,
    });

    // Make services available to page's `getInitialProps`
    ctx.mobxServices = mobxServices;

    // Call "super" to run page's `getInitialProps`
    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx);
    }

    // Gather serialization-friendly data from services
    const initialData = {
      language: mobxServices.languageService.data(),
github dpla / dpla-frontend / pages / item / index.js View on Github external
ItemDetail.getInitialProps = async context => {
  const query = context.query;
  const req = context.req;
  const res = context.res;
  const isQA = parseCookies(context).hasOwnProperty("qa");
  const currentFullUrl = getCurrentFullUrl(req);
  const currentUrl = getCurrentUrl(req);
  const randomItemId = isQA ? await getRandomItemIdAsync(currentUrl) : null;
  // check if item is found
  try {
    const res = await fetch(`${currentUrl}${API_ENDPOINT}/${query.itemId}`);
    const json = await res.json();
    const doc = json.docs[0];
    const thumbnailUrl = doc.object
      ? `${currentUrl}${THUMBNAIL_ENDPOINT}/${doc.id}`
      : getDefaultThumbnail(doc.sourceResource.type);
    const date = doc.sourceResource.date &&
      Array.isArray(doc.sourceResource.date)
      ? doc.sourceResource.date[0]
      : doc.sourceResource.date;
    const language = doc.sourceResource.language &&
github codeleakteam / codeleak / nextjs / pages / _app.js View on Github external
static async getInitialProps({ Component, ctx }) {
    let pageProps = {}

    const { codeleakAuthToken } = parseCookies(ctx)
    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps({ ...ctx, codeleakAuthToken })
    }

    return { pageProps, codeleakAuthToken, codeleakUser: ctx.codeleakUser }
  }
github ShadOoW / web-starter-kit / src / partials / header / menu.js View on Github external
useEffect(() => {
    const userTheme = parseCookies().theme;
    setTheme(userTheme || 'light');
  }, []);
github wassgha / digital-signage / helpers / auth.js View on Github external
static async getInitialProps(ctx) {
      const { req, res, query } = ctx
      const alreadyLoggedIn = parseCookies(ctx).loggedIn
      const host =
        req && req.headers && req.headers.host
          ? 'http://' + req.headers.host
          : window.location.origin

      if ((req && req.user) || alreadyLoggedIn) {
        if (!alreadyLoggedIn) {
          setCookie(ctx, 'loggedIn', true, {
            maxAge: 30 * 24 * 60 * 60,
            path: '/'
          })
        }

        let displayId = query && query.display

        if (!displayId) {
github ShadOoW / web-starter-kit / pages / _document.js View on Github external
static async getInitialProps(ctx) {
    const sheet = new ServerStyleSheet();
    const originalRenderPage = ctx.renderPage;

    try {
      ctx.renderPage = () =>
        originalRenderPage({
          enhanceApp: (App) => (props) =>
            sheet.collectStyles(),
        });

      const initialProps = await Document.getInitialProps(ctx);

      const cookies = parseCookies(ctx);
      initialProps.theme = cookies.theme;
      initialProps.language = ctx.req.language;
      initialProps.direction = initialProps.language === 'ar' ? 'rtl' : 'ltr';

      return {
        ...initialProps,
        styles: (
          <>
            {initialProps.styles}
            {sheet.getStyleElement()}
          
        ),
      };
    } finally {
      sheet.seal();
    }

nookies

A set of cookie helpers for Next.js

MIT
Latest version published 3 years ago

Package Health Score

61 / 100
Full package analysis