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

100% Statements 12/12
100% Branches 8/8
100% Functions 1/1
100% Lines 10/10

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  4x 4x                   19x 10x   10x 10x   10x   10x 1x     9x    
import {Session} from '../auth/session/session';
import {Context} from '../context';
import OAuth from '../auth/oauth';
 
/**
 * 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 sessionId = OAuth.getOfflineSessionId(shop);
  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;
}