Show device token while signing in (#3935)

* Show user code while logging in

* fmt

* A snapshot a day keeps the bugs away! 📷🐛 (OS: windows-latest)

* @jtran feedback

Co-authored-by: Jonathan Tran <jonnytran@gmail.com>

* Revert "A snapshot a day keeps the bugs away! 📷🐛 (OS: windows-latest)"

This reverts commit 5ba9e4351a.

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
This commit is contained in:
Frank Noirot
2024-09-23 10:17:56 -04:00
committed by GitHub
parent f1b0e40388
commit 95ea1427bc
4 changed files with 90 additions and 35 deletions

View File

@ -160,7 +160,7 @@ ipcMain.handle('shell.openExternal', (event, data) => {
return shell.openExternal(data)
})
ipcMain.handle('login', async (event, host) => {
ipcMain.handle('startDeviceFlow', async (_, host: string) => {
// Do an OAuth 2.0 Device Authorization Grant dance to get a token.
// We quiet ts because we are not using this in the standard way.
// @ts-ignore
@ -178,21 +178,33 @@ ipcMain.handle('login', async (event, host) => {
const handle = await client.deviceAuthorization()
// eslint-disable-next-line @typescript-eslint/no-floating-promises
shell.openExternal(handle.verification_uri_complete)
// Register this handle to be used later.
ipcMain.handleOnce('loginWithDeviceFlow', async () => {
if (!handle) {
return Promise.reject(
new Error(
'No handle available. Did you call startDeviceFlow before calling this?'
)
)
}
shell.openExternal(handle.verification_uri_complete).catch(reportRejection)
// Wait for the user to login.
try {
console.log('Polling for token')
const tokenSet = await handle.poll()
console.log('Received token set')
console.log(tokenSet)
return tokenSet.access_token
} catch (e) {
console.log(e)
}
// Wait for the user to login.
try {
console.log('Polling for token')
const tokenSet = await handle.poll()
console.log('Received token set')
console.log(tokenSet)
return tokenSet.access_token
} catch (e) {
console.log(e)
}
return Promise.reject(new Error('No access token received'))
return Promise.reject(new Error('No access token received'))
})
// Return the user code so the app can display it.
return handle.user_code
})
ipcMain.handle('kittycad', (event, data) => {