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

Feathers.authenticate gives window undefined (server-rendered) #573

Closed
jarnojellesma opened this Issue Sep 18, 2017 · 11 comments

Comments

Projects
None yet
4 participants
@jarnojellesma
Copy link

jarnojellesma commented Sep 18, 2017

Expected behavior

feathersClient.authenticate({ strategy: 'jwt', accessToken }) should authenticate the user, the accessToken is a valid token which I'm taking from the "req" object (server rendered).

Actual behavior

ReferenceError: window is not defined
    at Object.setItem (src/client/helpers/ssr-storage.js:13:4)
    at Passport.setJWT (/current/CURRENT/PROJECT/node_modules/feathers-authentication-client/lib/passport.js:282:33)
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

I receive a window is not defined error, but I am rendering server-side (NuxtJs / Vue)

Module versions (especially the part that's not working):
"feathers": "^2.1.1",
"feathers-authentication": "^1.2.6",
"feathers-authentication-hooks": "^0.1.4",
"feathers-authentication-jwt": "^0.3.1",
"feathers-authentication-local": "^0.4.3",

@marshallswain

This comment has been minimized.

Copy link
Member

marshallswain commented Sep 29, 2017

@jarnojellesma I don't think feathers-authentication-client uses window, at all. This might be happening because you're passing a storage option that does use window.

@andysay

This comment has been minimized.

Copy link

andysay commented Oct 3, 2017

up, how fix it?

@marshallswain

This comment has been minimized.

Copy link
Member

marshallswain commented Oct 3, 2017

@andysay if you're going to build a server side rendered app, you have two options to fix this:

  1. Don't use window anywhere in your code.
  2. Shim window and window.localStorage on the server.
@andysay

This comment has been minimized.

Copy link

andysay commented Oct 3, 2017

  return feathersClient.authenticate({ strategy: 'jwt', accessToken })
      .then((response) => {

        return feathersClient.passport.verifyJWT(response.accessToken);
      })
      .then(payload => (
         feathersClient.service('users').get(payload.id)
      ))
      .then(user => {
        feathersClient.set('user', user);
       console.log('User', feathersClient.get('user'));
      });


@marshallswain its Example like is docs , but if i refresh webpage ReferenceError: window is not defined

but i not use window anywhere

@marshallswain

This comment has been minimized.

Copy link
Member

marshallswain commented Oct 3, 2017

@andysay Do you have a stack trace for the error? Next step is to find the line of code causing the error.

Also, the window.localStorage would be in the feathers client setup, usually, if that were the problem.

@andysay

This comment has been minimized.

Copy link

andysay commented Oct 3, 2017

@marshallswain i found token = window.localStorage.getItem(cookieName);
what to replace this ?

@marshallswain

This comment has been minimized.

Copy link
Member

marshallswain commented Oct 3, 2017

@andysay where did you find that?

@andysay

This comment has been minimized.

Copy link

andysay commented Oct 3, 2017

@marshallswain in client side

@marshallswain

This comment has been minimized.

Copy link
Member

marshallswain commented Oct 3, 2017

@andysay If you want my help, you'll need to provide more information. Otherwise, you'll just have to figure it out.

@daffl daffl closed this Dec 18, 2017

@andysay

This comment has been minimized.

Copy link

andysay commented Apr 4, 2018

@marshallswain

/* eslint-disable */
const cookieName = 'feathers-jwt';

function getTokenFromRequest(req) {
  if (!req || !req.headers || !req.headers.cookie) return undefined;
  const jwtCookie = req.headers.cookie.split(';').find(c => c.trim().startsWith(`${cookieName}=`));
  if (!jwtCookie) return undefined;
  return jwtCookie.split('=')[1];
}

export default function ({ isServer, store, req }) {
  const accessToken = (isServer) ? getTokenFromRequest(req) :
    window.localStorage.getItem(cookieName);

  if (!accessToken) return undefined;

  return store.dispatch('auth/jwt', { accessToken })
    .then(() => { console.log('success'); })
    .catch((error) => {
      console.log(error);
      // Ignore invalid JWT
    });
}

its my middleware check-auth on client side

server response ReferenceError: window is not defined
thanks

@marshallswain

This comment has been minimized.

Copy link
Member

marshallswain commented Apr 4, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.