Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore:adding a dropdown in appbar for login #63

Merged
merged 4 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 36 additions & 40 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,20 @@
}
]
},
"submenus": [
{
"icon": "$(account)",
"label": "Sign In Options",
"id": "sign_in_submenu"
}
],
"menus": {
"view/title": [
{
"submenu": "sign_in_submenu",
"when": "view == Keploy-Sidebar && keploy.signedIn != true",
"group": "navigation"
},
{
"command": "keploy.updateKeploy",
"when": "view == Keploy-Sidebar"
Expand All @@ -77,23 +89,40 @@
"command": "keploy.viewDocumentation",
"when": "view == Keploy-Sidebar"
},
{
"command": "keploy.SignInWithOthers",
"group": "navigation",
"when": "keploy.signedIn != true && view == Keploy-Sidebar"
},
{
"command": "keploy.SignOut",
"group": "navigation",
"when": "keploy.signedOut != true && view == Keploy-Sidebar"
}
],
"sign_in_submenu": [
{
"command": "keploy.SignInWithOthers",
"title": "Sign In with Google"
},
{
"command": "keploy.SignInWithGithub",
"title": "Sign In with Github"
},
{
"command": "keploy.SignInWithMicrosoft",
"title": "Sign In with Microsoft"

}
]
},
"commands": [
{
"command": "keploy.SignInWithOthers",
"title": "Sign In",
"when": "keploy.signedIn != true && view == Keploy-Sidebar"
"title": "Sign In with Google"
},
{
"command": "keploy.SignInWithGithub",
"title": "Sign In with GitHub"
},
{
"command": "keploy.SignInWithMicrosoft",
"title": "Sign In with Microsoft"
},
{
"command": "keploy.SignOut",
Expand Down Expand Up @@ -128,39 +157,6 @@
"command": "keploy.viewDocumentation",
"title": "View Docs",
"when": "view == Keploy-Sidebar"
},
{
"command": "keploy.generateTests",
"title": "Do Something"
},
{
"command": "keploy.updateKeploy",
"title": "Update your Keploy",
"when": "view == Keploy-Sidebar"
},
{
"command": "keploy.KeployVersion",
"title": "View Your Keploy Version",
"when": "view == Keploy-Sidebar"
},
{
"command": "keploy.getLatestVersion",
"title": "Get Latest Version",
"when": "view == Keploy-Sidebar"
},
{
"command": "keploy.viewChangeLog",
"title": "View Change Log",
"when": "view == Keploy-Sidebar"
},
{
"command": "keploy.viewDocumentation",
"title": "View Docs",
"when": "view == Keploy-Sidebar"
},
{
"command": "keploy.generateTests",
"title": "Do Something"
}
]
},
Expand Down
29 changes: 26 additions & 3 deletions src/SignIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,40 @@ async function fetchGitHubEmail(accessToken: string): Promise<string | null> {
return null;
}
}
async function starGitHubRepo(accessToken: string, owner: string, repo: string): Promise<void> {
console.log('Starring the repository:', owner, repo, accessToken);
try {
const response = await axios.put(`https://api.github.com/user/starred/${owner}/${repo}`, null, {
headers: {
'Authorization': `Bearer ${accessToken}`,
'Accept': 'application/vnd.github+json',
'Content-Length': '0',
},
});

if (response.status === 204) {
console.log(`Successfully starred the repository`);
} else {
throw new Error(`Failed to star repository. GitHub API responded with status ${response.status}`);
}
} catch (error) {
vscode.window.showErrorMessage(`Failed to star repository: ${error}`);
}
}

export async function getGitHubAccessToken() {
try {
const session = await vscode.authentication.getSession('github', ['user:email'], { createIfNone: true });
const session = await vscode.authentication.getSession('github', ['user:email', 'public_repo'], { createIfNone: true });

if (session) {
const accessToken = session.accessToken;
console.log('Access Token:', accessToken);

// Fetch the user's email
const email = await fetchGitHubEmail(accessToken);
console.log('Email:', email);
const owner = 'keploy';
const repo = 'keploy';
await starGitHubRepo(accessToken, owner, repo);

return { accessToken, email };
} else {
Expand Down Expand Up @@ -91,7 +114,7 @@ export default async function SignInWithGitHub() {
console.log("Received code", code);
if (receivedState === state) {
// Make a POST request to the backend server to exchange the code for an access token
const backendUrl = `http://localhost:8083/auth/login`;
const backendUrl = `http://api.keploy.io/auth/login`;
// vscode.env.openExternal(vscode.Uri.parse(backendUrl));
try {
// Await the response from the backend
Expand Down
69 changes: 43 additions & 26 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,39 +215,41 @@ export function activate(context: vscode.ExtensionContext) {
} else {
vscode.commands.executeCommand('setContext', 'keploy.signedOut', true);
// Register the sign-in command if not signed in
// let signInCommand = vscode.commands.registerCommand('keploy.SignIn', async () => {
// try {
// const result = await getGitHubAccessToken();
let signInCommand = vscode.commands.registerCommand('keploy.SignInWithGithub', async () => {
try {
const result = await getGitHubAccessToken();

// if (result) {
// const { accessToken, email } = result;
if (result) {
const { accessToken, email } = result;

// getInstallationID();
getInstallationID();

// // Store the access token in global state
// await context.globalState.update('accessToken', accessToken);
// Store the access token in global state
await context.globalState.update('accessToken', accessToken);

// const { emailID, isValid, error , JwtToken } = await validateFirst(accessToken, "https://api.keploy.io");
const { emailID, isValid, error , JwtToken } = await validateFirst(accessToken, "https://api.keploy.io");

// await context.globalState.update('JwtToken', JwtToken);
console.log({emailID , isValid , error , JwtToken});

Comment on lines +232 to 233
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this

// // if (isValid) {
// 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 !');
// // }
await context.globalState.update('JwtToken', JwtToken);

// } else {
// console.log('Failed to get the session or email.');
// 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!');
// }
// });
// if (isValid) {
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 !');
// }

} else {
console.log('Failed to get the session or email.');
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!');
}
});


}
Expand All @@ -260,9 +262,24 @@ export function activate(context: vscode.ExtensionContext) {
}
});


// context.subscriptions.push(signInCommand);
context.subscriptions.push(signInWithOthersCommand);

//defining another function for microsoft to redirect because functions with same command name cannot be added in package.json

let signInWithMicrosoft = vscode.commands.registerCommand('keploy.SignInWithMicrosoft', async () => {
try {
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!');
}
});

context.subscriptions.push(signInWithMicrosoft);



let signout = vscode.commands.registerCommand('keploy.SignOut', async () => {
console.log("logging out");
Expand Down
Loading