import * as jwt from 'jsonwebtoken';
import { Prisma } from './generated/prisma';

export interface Context {
  db: Prisma;
  request: any;
}

export function getUserId(ctx: Context) {
  const Authorization = ctx.request.get('Authorization');
  // Apollo Client sets header to the string 'null' when not logged in
  if (Authorization && Authorization !== 'null') {
    const token = Authorization.replace('Bearer ', '');
    const { userId } = jwt.verify(token, process.env.APP_SECRET) as { userId: string };
    return userId;
  }

  throw new AuthError();
}

export class AuthError extends Error {
  constructor() {
    super('Not authorized');
  }
}

import * as jwt from 'jsonwebtoken';
import { Prisma } from './generated/prisma';

export interface Context {
  db: Prisma;
  request: any;
}

export function getUserId(ctx: Context) {
  const Authorization = ctx.request.get('Authorization');
  if (Authorization) {
    const token = Authorization.replace('Bearer ', '');
    const { userId } = jwt.verify(token, process.env.APP_SECRET) as { userId: string };
    return userId;
  }

  throw new AuthError();
}

export class AuthError extends Error {
  constructor() {
    super('Not authorized');
  }
}

--- 
+++ 
@@ -8,7 +8,8 @@
 
 export function getUserId(ctx: Context) {
   const Authorization = ctx.request.get('Authorization');
-  if (Authorization) {
+  // Apollo Client sets header to the string 'null' when not logged in
+  if (Authorization && Authorization !== 'null') {
     const token = Authorization.replace('Bearer ', '');
     const { userId } = jwt.verify(token, process.env.APP_SECRET) as { userId: string };
     return userId;
Fix check for authorization in getUserId util func
