diff --git a/src/api.ts b/src/api.ts index 7ebf4d2..4b0f451 100644 --- a/src/api.ts +++ b/src/api.ts @@ -61,6 +61,17 @@ class API { return response.status === 200 } + async loginLdap(username: string, password: string) { + const response = await this.fetch(`${this.serverUrl}/auth/ldap`, { + method: 'post', + body: encodeFormComponent({username, password}), + headers: { + 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' + } + }) + return response.status === 200 + } + async logout() { const response = await this.fetch(`${this.serverUrl}/logout`) return response.status === 200 diff --git a/src/commands/login.ts b/src/commands/login.ts index dba2395..3bc13a6 100644 --- a/src/commands/login.ts +++ b/src/commands/login.ts @@ -18,24 +18,37 @@ Login as HMD successfully! static flags = { help: flags.help({char: 'h'}), - email: flags.string({char: 'u', description: 'Login email'}) + id: flags.string({char: 'u', description: 'Login email/username'}), + ldap: flags.boolean() } async run() { const {flags} = this.parse(Login) - let email = flags.email - - if (!email) { - const out = await inquirer.prompt({ - type: 'input', - name: 'email', - message: 'Enter your email' - }) - email = out.email - if (!email) { - // TODO: do more email validation - return this.log('No email is given') + let id = flags.id + + if (!id) { + if(flags.ldap) { + const out = await inquirer.prompt({ + type: 'input', + name: 'username', + message: 'Enter your username' + }) + id = out.username + if (!id) { + return this.log('No username is given') + } + } else { + const out = await inquirer.prompt({ + type: 'input', + name: 'email', + message: 'Enter your email' + }) + id = out.email + if (!id) { + // TODO: do more email validation + return this.log('No email is given') + } } } @@ -46,10 +59,16 @@ Login as HMD successfully! }) try { - if (await APIClient.login(email, password)) { + let success = false + if(flags.ldap) { + success = await APIClient.loginLdap(id, password) + } else { + success = await APIClient.login(id, password) + } + if (success) { return this.log('Login successfully') } else { - this.log('Login failed, please ensure your email/password is set') + this.log('Login failed, please ensure your credentials are set') } } catch (err) { this.error(err)