|
| 1 | +import { Command } from '@cliffy/command' |
| 2 | +import { customAlphabet } from 'nanoid' |
| 3 | +import { colors } from '@cliffy/ansi/colors' |
| 4 | +import type { GlobalOptions } from '../lib/types.ts' |
| 5 | +import { preferences } from '../lib/preferences.ts' |
| 6 | + |
| 7 | +// base58 uppercase characters |
| 8 | +const idgen = customAlphabet('123456789ABCDEFGHJKMNPQRSTUVWXYZ', 6) |
| 9 | + |
| 10 | +const RUNREAL_API_ENDPOINT = Deno.env.get('RUNREAL_API_ENDPOINT') || 'https://api.dashboard.runreal.dev/v1' |
| 11 | +const RUNREAL_AUTH_ENDPOINT = Deno.env.get('RUNREAL_AUTH_ENDPOINT') || 'https://auth.runreal.dev' |
| 12 | + |
| 13 | +export const auth = new Command<GlobalOptions>() |
| 14 | + .description('auth') |
| 15 | + .arguments('<command> [args...]') |
| 16 | + .stopEarly() |
| 17 | + .action(async (options, command, ...args) => { |
| 18 | + if (command === 'login') { |
| 19 | + const code = idgen(6) |
| 20 | + const hostname = Deno.hostname().replace(/[^a-z0-9A-Z-_\.]/, '') |
| 21 | + |
| 22 | + const link = `${RUNREAL_AUTH_ENDPOINT}/auth/cli?hostname=${hostname}&code=${code}` |
| 23 | + |
| 24 | + console.log(`Login code: ${colors.bold.green(code)}`) |
| 25 | + console.log(`Please open ${colors.bold.cyan(link)} to authorize.`) |
| 26 | + |
| 27 | + let count = 0 |
| 28 | + const interval = setInterval(async () => { |
| 29 | + if (count > 20) { |
| 30 | + clearInterval(interval) |
| 31 | + console.log(colors.red('Authentication timed out.')) |
| 32 | + shutdown() |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + await fetch(`${RUNREAL_API_ENDPOINT}/cli`, { |
| 37 | + method: 'POST', |
| 38 | + body: JSON.stringify({ |
| 39 | + hostname, |
| 40 | + code, |
| 41 | + }), |
| 42 | + headers: { |
| 43 | + 'Content-Type': 'application/json', |
| 44 | + }, |
| 45 | + }).then(async (res) => { |
| 46 | + if (res.status === 200) { |
| 47 | + clearInterval(interval) |
| 48 | + const body = await res.json() |
| 49 | + |
| 50 | + await preferences.set({ |
| 51 | + accessToken: body.token, |
| 52 | + }) |
| 53 | + console.log(colors.bold.cyan('Authenticated successfully!')) |
| 54 | + shutdown() |
| 55 | + } |
| 56 | + }).catch((err) => { |
| 57 | + console.log(err) |
| 58 | + // ignore error |
| 59 | + }) |
| 60 | + count++ |
| 61 | + }, 5000) |
| 62 | + |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + if (command === 'logout') { |
| 67 | + const prefs = await preferences.get() |
| 68 | + if (prefs.accessToken) { |
| 69 | + delete prefs.accessToken |
| 70 | + await preferences.set(prefs) |
| 71 | + console.log(colors.bold.cyan('Logged out successfully!')) |
| 72 | + } else { |
| 73 | + console.log(colors.red('You are not logged in.')) |
| 74 | + } |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + throw new Error('Invalid command. Use "login" or "logout".') |
| 79 | + }) |
| 80 | + |
| 81 | +function shutdown() { |
| 82 | + setTimeout(() => { |
| 83 | + Deno.exit(0) |
| 84 | + }, 500) |
| 85 | +} |
0 commit comments