]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/extra-utils/users/login-command.ts
Introduce user command
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / users / login-command.ts
index 97efcb766618b375c02c3d3f0cb5389684528e94..b4e3bb602cb2f9151e453dcc69c8ca278fb07720 100644 (file)
@@ -1,4 +1,3 @@
-import { PeerTubeRequestError } from '@server/helpers/requests'
 import { HttpStatusCode } from '@shared/core-utils'
 import { PeerTubeProblemDocument } from '@shared/models'
 import { unwrapBody } from '../requests'
@@ -8,7 +7,7 @@ export class LoginCommand extends AbstractCommand {
 
   login (options: OverrideCommandOptions & {
     client?: { id?: string, secret?: string }
-    user?: { username: string, password: string }
+    user?: { username: string, password?: string }
   } = {}) {
     const { client = this.server.client, user = this.server.user } = options
     const path = '/api/v1/users/token'
@@ -17,7 +16,7 @@ export class LoginCommand extends AbstractCommand {
       client_id: client.id,
       client_secret: client.secret,
       username: user.username,
-      password: user.password,
+      password: user.password ?? 'password',
       response_type: 'code',
       grant_type: 'password',
       scope: 'upload'
@@ -34,10 +33,10 @@ export class LoginCommand extends AbstractCommand {
     }))
   }
 
-  getAccessToken (user?: { username: string, password: string }): Promise<string>
-  getAccessToken (username: string, password: string): Promise<string>
-  async getAccessToken (arg1?: { username: string, password: string } | string, password?: string) {
-    let user: { username: string, password: string }
+  getAccessToken (arg1?: { username: string, password?: string }): Promise<string>
+  getAccessToken (arg1: string, password?: string): Promise<string>
+  async getAccessToken (arg1?: { username: string, password?: string } | string, password?: string) {
+    let user: { username: string, password?: string }
 
     if (!arg1) user = this.server.user
     else if (typeof arg1 === 'object') user = arg1
@@ -48,7 +47,7 @@ export class LoginCommand extends AbstractCommand {
 
       return body.access_token
     } catch (err) {
-      throw new Error('Cannot authenticate. Please check your username/password.')
+      throw new Error(`Cannot authenticate. Please check your username/password. (${err})`)
     }
   }