@@ -7,6 +7,7 @@ import cloneDeep from 'lodash/cloneDeep'
77import { User } from './user/index'
88import error from './core/contentstackError'
99import OAuthHandler from './core/oauthHandler'
10+ import { authenticator } from 'otplib'
1011
1112export default function contentstackClient ( { http } ) {
1213 /**
@@ -16,7 +17,8 @@ export default function contentstackClient ({ http }) {
1617 * @param {Object } parameters - login parameters
1718 * @prop {string } parameters.email - email id for user to login
1819 * @prop {string } parameters.password - password for user to login
19- * @prop {string } parameters.token - token for user to login
20+ * @prop {string } parameters.tfa_token - tfa token for user to login (2FA token)
21+ * @prop {string } parameters.mfaSecret - TOTP secret key for generating 2FA token
2022 * @returns {Promise }
2123 * @example
2224 * import * as contentstack from '@contentstack/management'
@@ -25,10 +27,23 @@ export default function contentstackClient ({ http }) {
2527 * client.login({ email: <emailid>, password: <password> })
2628 * .then(() => console.log('Logged in successfully'))
2729 *
30+ * @example
31+ * client.login({ email: <emailid>, password: <password>, tfa_token: <tfa_token> })
32+ * .then(() => console.log('Logged in successfully'))
33+ *
34+ * @example
35+ * client.login({ email: <emailid>, password: <password>, mfaSecret: <mfa_secret> })
36+ * .then(() => console.log('Logged in successfully'))
2837 */
29- function login ( requestBody , params = { } ) {
38+ function login ( requestBody = { } , params = { } ) {
3039 http . defaults . versioningStrategy = 'path'
3140
41+ const { mfaSecret, ...credentials } = requestBody
42+ requestBody = credentials
43+
44+ if ( ! requestBody . tfa_token && mfaSecret ) {
45+ requestBody . tfa_token = authenticator . generate ( mfaSecret )
46+ }
3247 return http . post ( '/user-session' , { user : requestBody } , { params : params } )
3348 . then ( ( response ) => {
3449 if ( response . data . user != null && response . data . user . authtoken != null ) {
@@ -55,10 +70,9 @@ export default function contentstackClient ({ http }) {
5570 */
5671 function getUser ( params = { } ) {
5772 http . defaults . versioningStrategy = 'path'
58- return http . get ( '/user' , { params : params } )
59- . then ( ( response ) => {
60- return new User ( http , response . data )
61- } , error )
73+ return http . get ( '/user' , { params : params } ) . then ( ( response ) => {
74+ return new User ( http , response . data )
75+ } , error )
6276 }
6377 /**
6478 * @description Get Stack instance. A stack is a space that stores the content of a project.
@@ -127,13 +141,16 @@ export default function contentstackClient ({ http }) {
127141 */
128142 function organization ( uid = null ) {
129143 http . defaults . versioningStrategy = 'path'
130- return new Organization ( http , uid !== null ? { organization : { uid : uid } } : null )
144+ return new Organization (
145+ http ,
146+ uid !== null ? { organization : { uid : uid } } : null
147+ )
131148 }
132149
133150 /**
134151 * @description The Log out of your account call is used to sign out the user of Contentstack account.
135152 * @memberof ContentstackClient
136- * @param {String } authtoken - Authtoken to logout from.
153+ * @param {String } authtoken - Authtoken to logout from.
137154 * @func logout
138155 * @returns {Object } Response object.
139156 *
@@ -152,25 +169,25 @@ export default function contentstackClient ({ http }) {
152169 function logout ( authtoken ) {
153170 http . defaults . versioningStrategy = 'path'
154171 if ( authtoken !== undefined ) {
155- return http . delete ( '/user-session' , {
156- headers : {
157- authtoken : authtoken
158- }
159- } )
172+ return http
173+ . delete ( '/user-session' , {
174+ headers : {
175+ authtoken : authtoken
176+ }
177+ } )
160178 . then ( ( response ) => {
161179 return response . data
162180 } , error )
163181 }
164- return http . delete ( '/user-session' )
165- . then ( ( response ) => {
166- if ( http . defaults . headers . common ) {
167- delete http . defaults . headers . common . authtoken
168- }
169- delete http . defaults . headers . authtoken
170- delete http . httpClientParams . authtoken
171- delete http . httpClientParams . headers . authtoken
172- return response . data
173- } , error )
182+ return http . delete ( '/user-session' ) . then ( ( response ) => {
183+ if ( http . defaults . headers . common ) {
184+ delete http . defaults . headers . common . authtoken
185+ }
186+ delete http . defaults . headers . authtoken
187+ delete http . httpClientParams . authtoken
188+ delete http . httpClientParams . headers . authtoken
189+ return response . data
190+ } , error )
174191 }
175192
176193 /**
@@ -201,7 +218,15 @@ export default function contentstackClient ({ http }) {
201218 const responseType = params . responseType || 'code'
202219 const scope = params . scope
203220 const clientSecret = params . clientSecret
204- return new OAuthHandler ( http , appId , clientId , redirectUri , clientSecret , responseType , scope )
221+ return new OAuthHandler (
222+ http ,
223+ appId ,
224+ clientId ,
225+ redirectUri ,
226+ clientSecret ,
227+ responseType ,
228+ scope
229+ )
205230 }
206231
207232 return {
0 commit comments