11/**
2- * Copyright 2015 IBM Corp.
2+ * Copyright OpenJS Foundation and other contributors, https://openjsf.org/
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -18,52 +18,41 @@ var request = require("../request");
1818var config = require ( "../config" ) ;
1919var prompt = require ( "../prompt" ) ;
2020
21- var when = require ( "when" ) ;
22-
2321function command ( argv , result ) {
24- return when . promise ( function ( resolve ) {
25- config . tokens ( null ) ;
26-
27- request . request ( '/auth/login' , { } ) . then ( function ( resp ) {
28- if ( resp . type ) {
29- if ( resp . type == "credentials" ) {
30- prompt . read ( { prompt :"Username:" . bold } , function ( err , username ) {
31- prompt . read ( { prompt :"Password:" . bold , silent : true } , function ( err , password ) {
32- request . request ( '/auth/token' , {
33- method : "POST" ,
34- body : JSON . stringify ( {
35- client_id : 'node-red-admin' ,
36- grant_type : 'password' ,
37- scope : '*' ,
38- username : username ,
39- password : password
40- } )
41- } ) . then ( function ( resp ) {
42- config . tokens ( resp ) ;
43- result . log ( "Logged in" . green ) ;
44- resolve ( ) ;
45- } ) . otherwise ( function ( resp ) {
46- result . warn ( "Login failed" ) ;
47- resolve ( ) ;
48- } ) ;
22+ config . tokens ( null ) ;
23+ return request . request ( '/auth/login' , { } ) . then ( function ( resp ) {
24+ if ( resp . type ) {
25+ if ( resp . type == "credentials" ) {
26+ prompt . read ( { prompt :"Username:" . bold } , function ( err , username ) {
27+ prompt . read ( { prompt :"Password:" . bold , silent : true } , function ( err , password ) {
28+ request . request ( '/auth/token' , {
29+ method : "POST" ,
30+ data : {
31+ client_id : 'node-red-admin' ,
32+ grant_type : 'password' ,
33+ scope : '*' ,
34+ username : username ,
35+ password : password
36+ }
37+ } ) . then ( function ( resp ) {
38+ config . tokens ( resp ) ;
39+ result . log ( "Logged in" . green ) ;
40+ } ) . catch ( function ( resp ) {
41+ result . warn ( "Login failed" ) ;
4942 } ) ;
5043 } ) ;
51- } else {
52- result . warn ( "Unsupported login type" ) ;
53- resolve ( ) ;
54- }
44+ } ) ;
5545 } else {
56- resolve ( ) ;
46+ result . warn ( "Unsupported login type" ) ;
5747 }
58- } ) . otherwise ( function ( resp ) {
59- result . warn ( "Login failed" ) ;
60- resolve ( ) ;
61- } ) ;
48+ }
49+ } ) . catch ( function ( resp ) {
50+ result . warn ( "Login failed" ) ;
6251 } ) ;
6352}
6453
65- command . name = "login" ;
66- command . usage = command . name + "" ;
54+ command . alias = "login" ;
55+ command . usage = command . alias + "" ;
6756command . description = "Log user in to the targetted Node-RED admin api" ;
6857
6958module . exports = command ;
0 commit comments