All files / src/utils load-current-session.ts

100% Statements 14/14
100% Branches 4/4
100% Functions 3/3
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32  7x 7x 7x 7x                 26x 26x   26x 26x 26x 25x 4x   21x       7x          
import http from 'http';
 
import {Context} from '../context';
import {ShopifyOAuth} from '../auth/oauth/oauth';
import {Session} from '../auth/session';
 
/**
 * Loads the current user's session, based on the given request and response.
 *
 * @param request  Current HTTP request
 * @param response Current HTTP response
 * @param isOnline Whether to load online (default) or offline sessions (optional)
 */
export default async function loadCurrentSession(
  request: http.IncomingMessage,
  response: http.ServerResponse,
  isOnline = true,
): Promise<Session | undefined> {
  Context.throwIfUninitialized();
 
  const sessionId = ShopifyOAuth.getCurrentSessionId(
    request,
    response,
    isOnline,
  );
  if (!sessionId) {
    return Promise.resolve(undefined);
  }
 
  return Context.SESSION_STORAGE.loadSession(sessionId);
}