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

100% Statements 20/20
100% Branches 10/10
100% Functions 3/3
100% Lines 19/19

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 33 34 35 36 37 38  4x 4x 4x 4x 4x                   10x 10x   10x 20x   10x 10x 10x 10x   10x 10x 10x       1x   9x      
import {Session} from '../auth/session/session';
import {Context} from '../context';
import OAuth from '../auth/oauth';
 
import {sanitizeShop} from './shop-validator';
 
/**
 * Helper method for quickly loading offline sessions by shop url.
 * By default, returns undefined if there is no session, or the session found is expired.
 * Optionally, pass a second argument for 'includeExpired' set to true to return expired sessions.
 *
 * @param shop the shop url to find the offline session for
 * @param includeExpired optionally include expired sessions, defaults to false
 */
export default async function loadOfflineSession(
  shop: string,
  includeExpired = false,
): Promise<Session | undefined> {
  Context.throwIfUninitialized();
  const cleanShop = sanitizeShop(shop, true)!;
 
  const sessionId = OAuth.getOfflineSessionId(cleanShop);
  const session = await Context.SESSION_STORAGE.loadSession(sessionId);
 
  const now = new Date();
 
  if (
    session &&
    !includeExpired &&
    session.expires &&
    session.expires.getTime() < now.getTime()
  ) {
    return undefined;
  }
 
  return session;
}