Skip to content

Commit

Permalink
Merge branch 'main' into sentry-integration
Browse files Browse the repository at this point in the history
Signed-off-by: Yash Khare <[email protected]>
  • Loading branch information
khareyash05 authored Sep 9, 2024
2 parents dffc7fe + 5ef3ef4 commit 13d5109
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/SignIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,66 @@ export async function SignInWithOthers() {
}



export async function SignInWithOthers() {

Check failure on line 201 in src/SignIn.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Cannot redeclare exported variable 'SignInWithOthers'.

Check failure on line 201 in src/SignIn.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Duplicate function implementation.

Check failure on line 201 in src/SignIn.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Cannot redeclare exported variable 'SignInWithOthers'.

Check failure on line 201 in src/SignIn.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Duplicate function implementation.
const state = generateRandomState(); // Generate a secure random state
const authUrl = `http://localhost:3000/signin?vscode=true&state=${state}`;
vscode.env.openExternal(vscode.Uri.parse(authUrl));

return new Promise((resolve, reject) => {
const server = http.createServer(async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}

if (req && req.url && req.url.startsWith('/login/keploy/callback')) {
const url = new URL(req.url, `http://${req.headers.host}`);
const receivedState = url.searchParams.get('state');
const token = url.searchParams.get('token');
console.log("Received state:", receivedState);
console.log("Received token:", token);

if (!receivedState || !token) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Missing state or token' }));
reject(new Error('Missing state or token'));
server.close();
return;
}

try {
// Simulate processing the token
console.log("Processing token...");

res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Token received and processed', token, receivedState }));

// Resolve the promise with the token
resolve(token.toString());
} catch (err) {
console.error('Error processing token:', err);
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Internal Server Error' }));
reject(err);
} finally {
server.close(); // Close the server once the request is handled
}
} else {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Not Found' }));
}
}).listen(3001, () => {
console.log('Server listening on port 3001');
});
});
}


// async function fetchAccessToken(code: string | null) {
// // Exchange the authorization code for an access token
// const response = await fetch('https://app.keploy.io/token', {
Expand Down
22 changes: 22 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,28 @@ export function activate(context: vscode.ExtensionContext) {
}
});

let signInWithOthersCommand = vscode.commands.registerCommand('keploy.SignInWithOthers', async () => {

Check failure on line 237 in src/extension.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Cannot redeclare block-scoped variable 'signInWithOthersCommand'.

Check failure on line 237 in src/extension.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Cannot redeclare block-scoped variable 'signInWithOthersCommand'.
try {
const result = await SignInWithOthers();
const accessToken = result as string; // Assert that result is a string
getInstallationID();

await context.globalState.update('accessToken', accessToken);

if (Boolean(accessToken)) {
vscode.window.showInformationMessage('You are now signed in!');
vscode.commands.executeCommand('setContext', 'keploy.signedIn', true);
vscode.commands.executeCommand('setContext', 'keploy.signedOut', false);
} else {
console.log('Validation failed for the user !');
vscode.window.showInformationMessage('Failed to sign in Keploy!');
}
} catch (error) {
// console.error('Error during sign-in:', error);
vscode.window.showInformationMessage('Failed to sign in Keploy!');
}
});

context.subscriptions.push(signInCommand);
context.subscriptions.push(signInWithOthersCommand);
}
Expand Down

0 comments on commit 13d5109

Please sign in to comment.