aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/standalone/videos/embed.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/standalone/videos/embed.ts')
-rw-r--r--client/src/standalone/videos/embed.ts67
1 files changed, 61 insertions, 6 deletions
diff --git a/client/src/standalone/videos/embed.ts b/client/src/standalone/videos/embed.ts
index dca23ca7e..5bf38a04c 100644
--- a/client/src/standalone/videos/embed.ts
+++ b/client/src/standalone/videos/embed.ts
@@ -4,7 +4,8 @@ import {
4 peertubeTranslate, 4 peertubeTranslate,
5 ResultList, 5 ResultList,
6 ServerConfig, 6 ServerConfig,
7 VideoDetails 7 VideoDetails,
8 UserRefreshToken
8} from '../../../../shared' 9} from '../../../../shared'
9import { VideoCaption } from '../../../../shared/models/videos/caption/video-caption.model' 10import { VideoCaption } from '../../../../shared/models/videos/caption/video-caption.model'
10import { 11import {
@@ -17,7 +18,7 @@ import { PeerTubeEmbedApi } from './embed-api'
17import { TranslationsManager } from '../../assets/player/translations-manager' 18import { TranslationsManager } from '../../assets/player/translations-manager'
18import videojs from 'video.js' 19import videojs from 'video.js'
19import { VideoJSCaption } from '../../assets/player/peertube-videojs-typings' 20import { VideoJSCaption } from '../../assets/player/peertube-videojs-typings'
20import { AuthUser } from '@app/core/auth/auth-user.model' 21import { PureAuthUser, objectToUrlEncoded, peertubeLocalStorage } from '@root-helpers/index'
21 22
22type Translations = { [ id: string ]: string } 23type Translations = { [ id: string ]: string }
23 24
@@ -43,8 +44,12 @@ export class PeerTubeEmbed {
43 mode: PlayerMode 44 mode: PlayerMode
44 scope = 'peertube' 45 scope = 'peertube'
45 46
46 user: AuthUser 47 user: PureAuthUser
47 headers = new Headers() 48 headers = new Headers()
49 LOCAL_STORAGE_OAUTH_CLIENT_KEYS = {
50 CLIENT_ID: 'client_id',
51 CLIENT_SECRET: 'client_secret'
52 }
48 53
49 static async main () { 54 static async main () {
50 const videoContainerId = 'video-container' 55 const videoContainerId = 'video-container'
@@ -60,12 +65,62 @@ export class PeerTubeEmbed {
60 return window.location.origin + '/api/v1/videos/' + id 65 return window.location.origin + '/api/v1/videos/' + id
61 } 66 }
62 67
68 refreshFetch (url: string, options?: Object) {
69 return fetch(url, options)
70 .then((res: Response) => {
71 if (res.status !== 401) return res
72
73 // 401 unauthorized is not catch-ed, but then-ed
74 const error = res
75
76 const refreshingTokenPromise = new Promise((resolve, reject) => {
77 const clientId: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID)
78 const clientSecret: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_SECRET)
79 const headers = new Headers()
80 headers.set('Content-Type', 'application/x-www-form-urlencoded')
81 const data = {
82 refresh_token: this.user.getRefreshToken(),
83 client_id: clientId,
84 client_secret: clientSecret,
85 response_type: 'code',
86 grant_type: 'refresh_token'
87 }
88
89 fetch('/api/v1/users/token', {
90 headers,
91 method: 'POST',
92 body: objectToUrlEncoded(data)
93 })
94 .then(res => res.json())
95 .then((obj: UserRefreshToken) => {
96 this.user.refreshTokens(obj.access_token, obj.refresh_token)
97 this.user.save()
98 this.headers.set('Authorization', `${this.user.getTokenType()} ${this.user.getAccessToken()}`)
99 resolve()
100 })
101 .catch((refreshTokenError: any) => {
102 reject(refreshTokenError)
103 })
104 })
105
106 return refreshingTokenPromise
107 .catch(() => {
108 // If refreshing fails, continue with original error
109 throw error
110 })
111 .then(() => fetch(url, {
112 ...options,
113 headers: this.headers
114 }))
115 })
116 }
117
63 loadVideoInfo (videoId: string): Promise<Response> { 118 loadVideoInfo (videoId: string): Promise<Response> {
64 return fetch(this.getVideoUrl(videoId), { headers: this.headers }) 119 return this.refreshFetch(this.getVideoUrl(videoId), { headers: this.headers })
65 } 120 }
66 121
67 loadVideoCaptions (videoId: string): Promise<Response> { 122 loadVideoCaptions (videoId: string): Promise<Response> {
68 return fetch(this.getVideoUrl(videoId) + '/captions', { headers: this.headers }) 123 return fetch(this.getVideoUrl(videoId) + '/captions')
69 } 124 }
70 125
71 loadConfig (): Promise<Response> { 126 loadConfig (): Promise<Response> {
@@ -115,7 +170,7 @@ export class PeerTubeEmbed {
115 170
116 async init () { 171 async init () {
117 try { 172 try {
118 this.user = AuthUser.load() 173 this.user = PureAuthUser.load()
119 await this.initCore() 174 await this.initCore()
120 } catch (e) { 175 } catch (e) {
121 console.error(e) 176 console.error(e)