Skip to content

Commit

Permalink
fix: login and install jest if not present (#51)
Browse files Browse the repository at this point in the history
* fix: login and install jest if not present

Signed-off-by: Hermione Dadheech <hermionedadheech@gmail.com>

* update: url

Signed-off-by: shivamsouravjha <2019145@iiitdmj.ac.in>

* fix: installing jest globally

Signed-off-by: Hermione Dadheech <hermionedadheech@gmail.com>

---------

Signed-off-by: Hermione Dadheech <hermionedadheech@gmail.com>
Signed-off-by: shivamsouravjha <2019145@iiitdmj.ac.in>
Co-authored-by: shivamsouravjha <2019145@iiitdmj.ac.in>
  • Loading branch information
Hermione2408 and shivamsouravjha authored Sep 18, 2024
1 parent 933e22e commit 9435518
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 74 deletions.
19 changes: 17 additions & 2 deletions scripts/utg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,23 @@ extension="${sourceFilePath##*.}"

# If the file is a Python file, install pytest-cov
if [ "$extension" = "py" ]; then
echo "Installing pytest-cov..."
pip3 install pytest-cov --break-system-packages
echo "Checking if pytest-cov is installed..."
if ! pip3 show pytest-cov > /dev/null 2>&1; then
echo "pytest-cov is not installed. Installing pytest-cov..."
pip3 install pytest-cov --break-system-packages
else
echo "pytest-cov is already installed."
fi
fi

if [ "$extension" = "js" ] || [ "$extension" = "ts" ]; then
echo "Checking if Jest is installed..."
if ! npm list -g jest > /dev/null 2>&1; then
echo "Jest is not installed. Installing Jest..."
npm install --global jest
else
echo "Jest is already installed."
fi
fi

if [ "$extension" = "go" ]; then
Expand Down
51 changes: 0 additions & 51 deletions src/SignIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,57 +123,6 @@ export async function SignInWithOthers() {
const state = generateRandomState(); // Generate a secure random state
const authUrl = `https://app.keploy.io/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);

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(12408, () => {
console.log('Server listening on port 12408');
});
});
}


Expand Down
46 changes: 25 additions & 21 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,31 @@ export function activate(context: vscode.ExtensionContext) {
const sidebarProvider = new SidebarProvider(context.extensionUri , context);
context.subscriptions.push(
vscode.window.registerUriHandler({
handleUri(uri: vscode.Uri): vscode.ProviderResult<void> {
vscode.window.showInformationMessage(`URI handler called: ${uri.toString()}`);
async handleUri(uri) {
// Extract the token and state from the URI query parameters
const token = uri.query.split('token=')[1]?.split('&')[0];
const state = uri.query.split('state=')[1];

if (token) {
vscode.window.showInformationMessage(`You are now logged in!`);

await context.globalState.update('JwtToken', token);
await context.globalState.update('SignedOthers', true);

const response = await ValidateSignInWithOthers(token);

if (response) {
vscode.commands.executeCommand('setContext', 'keploy.signedIn', true);
vscode.commands.executeCommand('setContext', 'keploy.signedOut', false);
} else {
vscode.window.showInformationMessage('Token validation failed!');
}
} else {
vscode.window.showInformationMessage('Login failed');
}
}
}),
}),

vscode.window.registerWebviewViewProvider(
"Keploy-Sidebar",
sidebarProvider
Expand Down Expand Up @@ -232,24 +253,7 @@ export function activate(context: vscode.ExtensionContext) {
}
let signInWithOthersCommand = vscode.commands.registerCommand('keploy.SignInWithOthers', async () => {
try {
const result = await SignInWithOthers();
// console.log("result from the Sign in with other , ",result);
const accessToken = result as string; // Assert that result is a string
// console.log('Jwt token:', accessToken);
await context.globalState.update('JwtToken', accessToken);
await context.globalState.update('SignedOthers', true);
const reponse = await ValidateSignInWithOthers(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!');
}
await SignInWithOthers(); // The result will now be handled in the URI handler
} catch (error) {
// console.error('Error during sign-in:', error);
vscode.window.showInformationMessage('Failed to sign in Keploy!');
Expand Down

0 comments on commit 9435518

Please sign in to comment.