All files / src/auth/session session.ts

100% Statements 22/22
100% Branches 6/6
100% Functions 4/4
100% Lines 22/22

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52  22x 22x 22x       22x   43x 43x 43x 43x   22x 2x 2x 2x 2x 2x 2x   22x 2x 2x     1x   1x   22x   22x                                    
import {Context} from '../../context';
import {OnlineAccessInfo} from '../oauth/types';
 
import {SessionInterface} from './types';
 
/**
 * Stores App information from logged in merchants so they can make authenticated requests to the Admin API.
 */
class Session implements SessionInterface {
  public static cloneSession(session: Session, newId: string): Session {
    const newSession = new Session(
      newId,
      session.shop,
      session.state,
      session.isOnline,
    );
 
    newSession.scope = session.scope;
    newSession.expires = session.expires;
    newSession.accessToken = session.accessToken;
    newSession.onlineAccessInfo = session.onlineAccessInfo;
 
    return newSession;
  }
 
  public scope?: string;
  public expires?: Date;
  public accessToken?: string;
  public onlineAccessInfo?: OnlineAccessInfo;
 
  constructor(
    readonly id: string,
    public shop: string,
    public state: string,
    public isOnline: boolean,
  ) {}
 
  public isActive(): boolean {
    const scopesUnchanged = Context.SCOPES.equals(this.scope);
    if (
      scopesUnchanged &&
      this.accessToken &&
      (!this.expires || this.expires >= new Date())
    ) {
      return true;
    }
    return false;
  }
}
 
export {Session};