]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/extra-utils/users/login-command.ts
Optimize torrent URL update
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / users / login-command.ts
index 97efcb766618b375c02c3d3f0cb5389684528e94..143f72a59c0e994bebfe4d4115257b26819afce7 100644 (file)
@@ -1,6 +1,4 @@
-import { PeerTubeRequestError } from '@server/helpers/requests'
-import { HttpStatusCode } from '@shared/core-utils'
-import { PeerTubeProblemDocument } from '@shared/models'
+import { HttpStatusCode, PeerTubeProblemDocument } from '@shared/models'
 import { unwrapBody } from '../requests'
 import { AbstractCommand, OverrideCommandOptions } from '../shared'
 
@@ -8,16 +6,16 @@ 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 { client = this.server.store.client, user = this.server.store.user } = options
     const path = '/api/v1/users/token'
 
     const body = {
       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'
@@ -27,19 +25,19 @@ export class LoginCommand extends AbstractCommand {
       ...options,
 
       path,
-      type: 'form',
+      requestType: 'form',
       fields: body,
       implicitToken: false,
       defaultExpectedStatus: HttpStatusCode.OK_200
     }))
   }
 
-  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
+    if (!arg1) user = this.server.store.user
     else if (typeof arg1 === 'object') user = arg1
     else user = { username: arg1, password }
 
@@ -48,7 +46,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})`)
     }
   }
 
@@ -60,8 +58,8 @@ export class LoginCommand extends AbstractCommand {
     const path = '/api/v1/users/token'
 
     const body = {
-      client_id: this.server.client.id,
-      client_secret: this.server.client.secret,
+      client_id: this.server.store.client.id,
+      client_secret: this.server.store.client.secret,
       username: username,
       response_type: 'code',
       grant_type: 'password',
@@ -73,7 +71,7 @@ export class LoginCommand extends AbstractCommand {
       ...options,
 
       path,
-      type: 'form',
+      requestType: 'form',
       fields: body,
       implicitToken: false,
       defaultExpectedStatus: HttpStatusCode.OK_200
@@ -89,7 +87,7 @@ export class LoginCommand extends AbstractCommand {
       ...options,
 
       path,
-      type: 'form',
+      requestType: 'form',
       implicitToken: false,
       defaultExpectedStatus: HttpStatusCode.OK_200
     }))
@@ -101,8 +99,8 @@ export class LoginCommand extends AbstractCommand {
     const path = '/api/v1/users/token'
 
     const body = {
-      client_id: this.server.client.id,
-      client_secret: this.server.client.secret,
+      client_id: this.server.store.client.id,
+      client_secret: this.server.store.client.secret,
       refresh_token: options.refreshToken,
       response_type: 'code',
       grant_type: 'refresh_token'
@@ -112,7 +110,7 @@ export class LoginCommand extends AbstractCommand {
       ...options,
 
       path,
-      type: 'form',
+      requestType: 'form',
       fields: body,
       implicitToken: false,
       defaultExpectedStatus: HttpStatusCode.OK_200