Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 34 additions & 15 deletions src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
}
}

Expand All @@ -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)
Expand Down