From 2b04763ac05d4b0a114b9a68d0366f65faa84399 Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Wed, 5 Feb 2020 21:25:56 +0000 Subject: [PATCH] Convert lib/auth/index to typescript Change-type: patch --- lib/auth/index.coffee | 63 ------------------------------------------- lib/auth/index.ts | 63 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 63 deletions(-) delete mode 100644 lib/auth/index.coffee create mode 100644 lib/auth/index.ts diff --git a/lib/auth/index.coffee b/lib/auth/index.coffee deleted file mode 100644 index 85e3bb35..00000000 --- a/lib/auth/index.coffee +++ /dev/null @@ -1,63 +0,0 @@ -### -Copyright 2016 Balena - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -### - -###* -# @module auth -### - -open = require('open') -balena = require('balena-sdk').fromSharedOptions() -server = require('./server') -utils = require('./utils') - -###* -# @summary Login to the balena CLI using the web dashboard -# @function -# @public -# -# @description -# This function opens the user's default browser and points it -# to the balena dashboard where the session token exchange will -# take place. -# -# Once the the token is retrieved, it's automatically persisted. -# -# @fulfil {String} - session token -# @returns {Promise} -# -# @example -# auth.login().then (sessionToken) -> -# console.log('I\'m logged in!') -# console.log("My session token is: #{sessionToken}") -### -exports.login = -> - options = - port: 8989 - path: '/auth' - - # Needs to be 127.0.0.1 not localhost, because the ip only is whitelisted - # from mixed content warnings (as the target of a form in the result page) - callbackUrl = "http://127.0.0.1:#{options.port}#{options.path}" - return utils.getDashboardLoginURL(callbackUrl).then (loginUrl) -> - - # Leave a bit of time for the - # server to get up and runing - setTimeout -> - open(loginUrl, { wait: false }) - , 1000 - - return server.awaitForToken(options) - .tap(balena.auth.loginWithToken) diff --git a/lib/auth/index.ts b/lib/auth/index.ts new file mode 100644 index 00000000..f43e812b --- /dev/null +++ b/lib/auth/index.ts @@ -0,0 +1,63 @@ +/* +Copyright 2016 Balena + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/** + * @module auth + */ + +/** + * @summary Login to the balena CLI using the web dashboard + * @function + * @public + * + * @description + * This function opens the user's default browser and points it + * to the balena dashboard where the session token exchange will + * take place. + * + * Once the the token is retrieved, it's automatically persisted. + * + * @fulfil {String} - session token + * @returns {Promise} + * + * @example + * auth.login().then (sessionToken) -> + * console.log('I\'m logged in!') + * console.log("My session token is: #{sessionToken}") + */ +export const login = async () => { + const utils = await import('./utils'); + + const options = { + port: 8989, + path: '/auth', + }; + + // Needs to be 127.0.0.1 not localhost, because the ip only is whitelisted + // from mixed content warnings (as the target of a form in the result page) + const callbackUrl = `http://127.0.0.1:${options.port}${options.path}`; + const loginUrl = await utils.getDashboardLoginURL(callbackUrl); + // Leave a bit of time for the + // server to get up and runing + setTimeout(async () => { + const open = await import('open'); + open(loginUrl, { wait: false }); + }, 1000); + + const server = await import('./server'); + const balena = (await import('balena-sdk')).fromSharedOptions(); + return server.awaitForToken(options).tap(balena.auth.loginWithToken); +};