-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
7,859 additions
and
2,990 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import {Command} from '@oclif/core' | ||
import * as http from 'node:http' | ||
import storage from 'node-persist' | ||
import open from 'open' | ||
|
||
|
||
|
||
|
||
export default class LoginIndex extends Command { | ||
static override args = { | ||
} | ||
|
||
static override description = 'Login to Hypermode Console' | ||
|
||
static override examples = [ | ||
'<%= config.bin %> <%= command.id %>', | ||
] | ||
|
||
static override flags = { | ||
} | ||
|
||
public async openLoginPage() { | ||
// Open the Hypermode sign-in page in the default browser | ||
const loginUrl = 'https://hypermode-stage.com/app/callback?port=5051&type=cli'; | ||
await open(loginUrl); | ||
} | ||
|
||
public async run(): Promise<void> { | ||
|
||
// Initialize storage to persist JWT and email | ||
await storage.init(); | ||
|
||
// Start a local server to capture JWT and email from redirect | ||
const server = http.createServer(async (req, res) => { | ||
const url = new URL(req.url ?? '', `http://${req.headers.host}`); | ||
|
||
// Extract the JWT and email from query parameters | ||
const token = url.searchParams.get('jwt'); | ||
const email = url.searchParams.get('email'); | ||
|
||
if (token && email) { | ||
// Store JWT and email securely | ||
await storage.setItem('jwt', token); | ||
await storage.setItem('email', email); | ||
|
||
// Send response back to browser indicating success | ||
res.writeHead(200, {'Content-Type': 'text/plain'}); | ||
res.end('Login successful! You can close this tab.'); | ||
|
||
// Close the server once JWT and email are captured | ||
server.close(); | ||
|
||
// Confirm successful login in the CLI | ||
this.log('Successfully logged in as ' + email + '! 🎉'); | ||
} else { | ||
// Respond with an error if JWT or email is missing | ||
res.writeHead(400, {'Content-Type': 'text/plain'}); | ||
res.end('JWT or email not found in the request.'); | ||
} | ||
}); | ||
|
||
// Set a timeout for the server | ||
const timeoutDuration = 300_000; // 300 seconds in milliseconds | ||
const timeout = setTimeout(() => { | ||
server.close(); | ||
throw new Error('Authentication timed out. Please try again.'); | ||
}, timeoutDuration); | ||
|
||
// Listen on port 5051 for the redirect | ||
server.listen(5051, 'localhost', () => { | ||
// Open the Hypermode sign-in page in the default browser | ||
this.log('Opening login page...'); | ||
this.openLoginPage(); | ||
this.log('Waiting for the login callback on port 5051...'); | ||
}); | ||
|
||
// Ensure the timeout is cleared if the server closes successfully | ||
server.on('close', () => { | ||
clearTimeout(timeout); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {Command} from '@oclif/core' | ||
import storage from 'node-persist' | ||
|
||
export default class LogoutIndex extends Command { | ||
static override args = { | ||
} | ||
|
||
static override description = 'Logout of Hypermode Console in the CLI' | ||
|
||
static override examples = [ | ||
'<%= config.bin %> <%= command.id %>', | ||
] | ||
|
||
static override flags = { | ||
} | ||
|
||
public async run(): Promise<void> { | ||
await storage.init(); | ||
const email = await storage.getItem('email'); | ||
if (!email) { | ||
this.log('Not logged in.'); | ||
return; | ||
} | ||
|
||
console.log('Logging out of email: ' + email); | ||
|
||
await storage.removeItem('jwt'); | ||
await storage.removeItem('email'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// import {runCommand} from '@oclif/test' | ||
// import {expect} from 'chai' | ||
// import sinon, { SinonStub } from 'sinon'; | ||
|
||
// import LoginIndex from '../../../src/commands/login/index'; | ||
|
||
describe('login', () => { | ||
// let openLoginPageStub: SinonStub; | ||
|
||
// beforeEach(() => { | ||
// // Create a stub for the openLoginPage method | ||
// openLoginPageStub = sinon.stub(LoginIndex.prototype, 'openLoginPage').returns(Promise.resolve()); | ||
// }); | ||
|
||
// afterEach(() => { | ||
// // Restore all stubs after each test | ||
// sinon.restore(); | ||
// }); | ||
|
||
// it('runs login cmd', async () => { | ||
// const { stdout } = await runCommand('login'); | ||
// expect(stdout).to.contain(''); | ||
// expect(openLoginPageStub.calledOnce).to.be.true; // Ensure the method was called | ||
// }); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {runCommand} from '@oclif/test' | ||
import {expect} from 'chai' | ||
import storage from 'node-persist' | ||
|
||
describe('logout', () => { | ||
it('runs logout cmd', async () => { | ||
await storage.init(); | ||
await storage.setItem('email', '[email protected]'); | ||
const {stdout} = await runCommand('logout') | ||
expect(stdout).to.contain('[email protected]') | ||
|
||
const email = await runCommand('logout') | ||
expect(email.stdout).to.contain('Not logged in.') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters