moontraze 1.0.6 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/auth.js +55 -7
  2. package/package.json +1 -1
package/lib/auth.js CHANGED
@@ -86,17 +86,36 @@ async function loginWithEmail({ apiUrl, label }) {
86
86
  }
87
87
 
88
88
  async function loginWithMoonID({ apiUrl, label }) {
89
- const state = Math.random().toString(36).substring(2, 12);
90
- const port = 17321 + Math.floor(Math.random() * 100);
91
- const redirectUri = `http://127.0.0.1:${port}/callback`;
92
- const appId = 'com.moonstorage.app';
89
+ const fetch = require('node-fetch');
90
+ const nonce = Math.random().toString(36).substring(2, 12);
91
+ const state = `${nonce}.login`;
92
+
93
+ const startRes = await fetch(`${apiUrl}/api/platform/moonid/cli-start`, {
94
+ method: 'POST',
95
+ headers: { 'Content-Type': 'application/json' },
96
+ body: JSON.stringify({ state }),
97
+ });
98
+ const startData = await startRes.json().catch(() => ({}));
99
+ if (!startRes.ok) {
100
+ throw new Error(startData.error || 'Could not start MoonID CLI session');
101
+ }
102
+
103
+ const redirectUri =
104
+ startData.redirectUri ||
105
+ `${apiUrl}/api/platform/moonid/cli-callback`;
106
+
107
+ // App POST yahan karegi (web jaisa)
108
+ const notifyUrl = `${apiUrl}/api/platform/moonid/cli-complete`;
93
109
 
110
+ const appId = 'com.moonstorage.app';
94
111
  const deepLink =
95
112
  `moonid://auth` +
96
113
  `?app_id=${encodeURIComponent(appId)}` +
97
114
  `&action=login` +
98
115
  `&redirect_uri=${encodeURIComponent(redirectUri)}` +
99
- `&state=${encodeURIComponent(state)}`;
116
+ `&notify_url=${encodeURIComponent(notifyUrl)}` +
117
+ `&state=${encodeURIComponent(state)}` +
118
+ `&source=cli`;
100
119
 
101
120
  const qrImageUrl = `https://api.qrserver.com/v1/create-qr-code/?size=220x220&data=${encodeURIComponent(
102
121
  deepLink
@@ -105,7 +124,7 @@ async function loginWithMoonID({ apiUrl, label }) {
105
124
  console.log('');
106
125
  console.log(chalk.cyan(`${label} — MoonID Login`));
107
126
  console.log(chalk.gray('1. Open MoonID app on your phone'));
108
- console.log(chalk.gray('2. Scan the QR code (open link below in browser)'));
127
+ console.log(chalk.gray('2. Scan the QR code (open the link below in a browser if needed)'));
109
128
  console.log(chalk.gray('3. Approve login'));
110
129
  console.log('');
111
130
  console.log(chalk.yellow('QR code image:'));
@@ -114,12 +133,41 @@ async function loginWithMoonID({ apiUrl, label }) {
114
133
  console.log(chalk.gray('Waiting for approval... (Ctrl+C to cancel)'));
115
134
  console.log('');
116
135
 
117
- const token = await waitForMoonIDCallback({ port, state, timeoutMs: 120000 });
136
+ const token = await pollCliStatus({ apiUrl, state, timeoutMs: 180000 });
118
137
  if (!token) throw new Error('MoonID login timed out or denied');
119
138
 
120
139
  await saveAndValidateToken(token, { apiUrl, label });
121
140
  }
122
141
 
142
+ async function pollCliStatus({ apiUrl, state, timeoutMs }) {
143
+ const fetch = require('node-fetch');
144
+ const start = Date.now();
145
+ const interval = 2000;
146
+
147
+ while (Date.now() - start < timeoutMs) {
148
+ await new Promise((r) => setTimeout(r, interval));
149
+ try {
150
+ const res = await fetch(
151
+ `${apiUrl}/api/platform/moonid/cli-status?state=${encodeURIComponent(state)}`
152
+ );
153
+ const data = await res.json().catch(() => ({}));
154
+
155
+ if (data.status === 'done' && data.token) {
156
+ return data.token;
157
+ }
158
+ if (data.status === 'denied' || data.status === 'error') {
159
+ throw new Error(data.error || 'MoonID login denied');
160
+ }
161
+ // pending / unknown → keep waiting
162
+ } catch (e) {
163
+ if (e.message && /denied|failed|not found|mismatch|expired/i.test(e.message)) {
164
+ throw e;
165
+ }
166
+ }
167
+ }
168
+ return null;
169
+ }
170
+
123
171
  function waitForMoonIDCallback({ port, state, timeoutMs }) {
124
172
  return new Promise((resolve) => {
125
173
  const server = http.createServer((req, res) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "moontraze",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "Deploy to Moontraze hosting — like vercel CLI",
5
5
  "bin": {
6
6
  "moon": "./bin/moon.js",