npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@mayaross/auth

v1.0.0

Published

SDK for mayar-cli to authenticate via mayar auth service

Readme

@mayaross/auth

SDK untuk autentikasi mayar-cli via mayar auth service menggunakan Google OAuth + SSE.

Instalasi

npm install @mayaross/auth

Tidak ada dependency eksternal — hanya menggunakan built-in Node.js (https, http, child_process). Membutuhkan Node.js >= 18.


Flow

mayar login
    │
    ├─ 1. Minta appid + URL ke server  →  GET /token/cli/initiate
    │
    ├─ 2. Buka browser ke auth URL     →  /oauth2/google?state=appid,<uuid>
    │
    ├─ 3. Subscribe SSE                →  GET /token/cli/stream?appid=<uuid>
    │         (tunggu token, max 5 menit)
    │
    ├─ 4. User login Google di browser
    │
    └─ 5. Server push token via SSE   →  CLI terima, simpan credentials

Penggunaan

Login satu langkah

const { MayarAuth } = require('@mayaross/auth');

const auth = new MayarAuth('https://auth.mayar.id');

try {
  const token = await auth.login({
    onUrl: ({ url }) => {
      console.log(`Membuka browser untuk login...`);
      console.log(`Jika browser tidak terbuka, buka URL ini:\n${url}`);
    }
  });

  // token = "authToken,refreshToken"
  console.log('Login berhasil!');
  // simpan token ke ~/.mayar/credentials atau keychain
} catch (e) {
  console.error('Login gagal:', e.message);
}

Manual step-by-step

const { MayarAuth } = require('@mayaross/auth');

const auth = new MayarAuth('https://auth.mayar.id');

// 1. Dapatkan appid dan URL login
const { appid, url } = await auth.initiate();
console.log('Buka URL ini di browser:', url);

// 2. (opsional) buka browser secara manual
auth.openBrowser(url);

// 3. Tunggu token via SSE
const token = await auth.listenForToken(appid);
console.log('Token diterima:', token);

API

new MayarAuth(baseUrl, opts?)

| Parameter | Type | Default | Keterangan | |-----------|------|---------|------------| | baseUrl | string | 'https://auth.mayar.id' | Base URL auth server | | opts.timeoutMs | number | 300000 (5 menit) | Timeout tunggu login |


auth.login(opts?)

Full login flow: initiate → buka browser → tunggu token via SSE.

login(opts?: {
  openBrowser?: boolean,   // default: true. Set false untuk print URL saja
  onUrl?: ({ appid, url }) => void  // callback sebelum browser dibuka
}): Promise<string>  // "authToken,refreshToken"

auth.initiate()

Minta appid dan url dari server.

initiate(): Promise<{ appid: string, url: string }>

auth.listenForToken(appid)

Konek ke SSE stream dan tunggu token. Resolve saat token diterima, reject saat timeout.

listenForToken(appid: string): Promise<string>  // "authToken,refreshToken"

auth.openBrowser(url)

Buka URL di browser default. Support macOS, Linux, Windows.

openBrowser(url: string): void

Format Token

Token yang dikembalikan berupa string dengan format:

authToken,refreshToken
  • authToken — JWT RS256, berlaku 24 jam
  • refreshToken — JWT RS256, berlaku 7 hari, digunakan untuk refresh otomatis via /validate

Untuk decode payload:

const [authToken] = token.split(',');
const payload = JSON.parse(
  Buffer.from(authToken.split('.')[1], 'base64url').toString()
);
// { sub: email, role, name, exp, ... }

Contoh Integrasi di mayar-cli

// commands/login.js
const { MayarAuth } = require('@mayaross/auth');
const fs = require('fs');
const os = require('os');
const path = require('path');

const CREDENTIALS_PATH = path.join(os.homedir(), '.mayar', 'credentials.json');

async function loginCommand() {
  const auth = new MayarAuth(process.env.MAYAR_AUTH_URL || 'https://auth.mayar.id');

  console.log('Menghubungkan ke mayar...');

  const token = await auth.login({
    onUrl: ({ url }) => {
      console.log('\nJika browser tidak terbuka otomatis, buka URL ini:');
      console.log(url + '\n');
    }
  });

  const [authToken] = token.split(',');
  const { sub: email, name, exp } = JSON.parse(
    Buffer.from(authToken.split('.')[1], 'base64url').toString()
  );

  fs.mkdirSync(path.dirname(CREDENTIALS_PATH), { recursive: true });
  fs.writeFileSync(CREDENTIALS_PATH, JSON.stringify({ token, email, name }, null, 2));

  console.log(`\nLogin berhasil sebagai ${name} (${email})`);
  console.log(`Token berlaku hingga ${new Date(exp * 1000).toLocaleString()}`);
}

module.exports = { loginCommand };

Server Endpoints

SDK ini mengonsumsi dua endpoint dari auth server:

| Endpoint | Method | Keterangan | |----------|--------|------------| | /token/cli/initiate | GET | Generate appid + auth URL | | /token/cli/stream?appid=<uuid> | GET (SSE) | Stream token setelah user login |