@@ -20,19 +20,6 @@ export interface PlexPin {
2020 code : string ;
2121}
2222
23- const uuidv4 = ( ) : string => {
24- return ( ( 1e7 ) . toString ( ) + - 1e3 + - 4e3 + - 8e3 + - 1e11 ) . replace (
25- / [ 0 1 8 ] / g,
26- function ( c ) {
27- return (
28- parseInt ( c ) ^
29- ( window . crypto . getRandomValues ( new Uint8Array ( 1 ) ) [ 0 ] &
30- ( 15 >> ( parseInt ( c ) / 4 ) ) )
31- ) . toString ( 16 ) ;
32- }
33- ) ;
34- } ;
35-
3623class PlexOAuth {
3724 private plexHeaders ?: PlexHeaders ;
3825
@@ -41,26 +28,25 @@ class PlexOAuth {
4128
4229 private authToken ?: string ;
4330
44- public initializeHeaders ( ) : void {
45- if ( ! window ) {
31+ public initializeHeaders ( plexClientIdentifier : string ) : void {
32+ if ( typeof window === 'undefined' ) {
4633 throw new Error (
4734 'Window is not defined. Are you calling this in the browser?'
4835 ) ;
4936 }
5037
51- let clientId = localStorage . getItem ( 'plex-client-id' ) ;
52- if ( ! clientId ) {
53- const uuid = uuidv4 ( ) ;
54- localStorage . setItem ( 'plex-client-id' , uuid ) ;
55- clientId = uuid ;
38+ if ( ! plexClientIdentifier ) {
39+ throw new Error (
40+ 'Plex client identifier missing. Reload the page and try again.'
41+ ) ;
5642 }
5743
5844 const browser = Bowser . getParser ( window . navigator . userAgent ) ;
5945 this . plexHeaders = {
6046 Accept : 'application/json' ,
6147 'X-Plex-Product' : 'Seerr' ,
6248 'X-Plex-Version' : 'Plex OAuth' ,
63- 'X-Plex-Client-Identifier' : clientId ,
49+ 'X-Plex-Client-Identifier' : plexClientIdentifier ,
6450 'X-Plex-Model' : 'Plex OAuth' ,
6551 'X-Plex-Platform' : browser . getBrowserName ( ) ,
6652 'X-Plex-Platform-Version' : browser . getBrowserVersion ( ) || 'Unknown' ,
@@ -93,9 +79,14 @@ class PlexOAuth {
9379 this . openPopup ( { title : 'Plex Auth' , w : 600 , h : 700 } ) ;
9480 }
9581
96- public async login ( ) : Promise < string > {
97- this . initializeHeaders ( ) ;
98- await this . getPin ( ) ;
82+ public async login ( plexClientIdentifier : string ) : Promise < string > {
83+ try {
84+ this . initializeHeaders ( plexClientIdentifier ) ;
85+ await this . getPin ( ) ;
86+ } catch ( e ) {
87+ this . closePopup ( ) ;
88+ throw e ;
89+ }
9990
10091 if ( ! this . plexHeaders || ! this . pin ) {
10192 throw new Error ( 'Unable to call login if class is not initialized.' ) ;
@@ -117,12 +108,16 @@ class PlexOAuth {
117108 code : this . pin . code ,
118109 } ;
119110
120- if ( this . popup ) {
121- this . popup . location . href = `https://app.plex.tv/auth/#!? ${ this . encodeData (
122- params
123- ) } ` ;
111+ if ( ! this . popup || this . popup . closed ) {
112+ throw new Error (
113+ 'Unable to open the Plex login window. Please allow popups for this site and try again.'
114+ ) ;
124115 }
125116
117+ this . popup . location . href = `https://app.plex.tv/auth/#!?${ this . encodeData (
118+ params
119+ ) } `;
120+
126121 return this . pinPoll ( ) ;
127122 }
128123
@@ -145,7 +140,7 @@ class PlexOAuth {
145140 this . authToken = response . data . authToken as string ;
146141 this . closePopup ( ) ;
147142 resolve ( this . authToken ) ;
148- } else if ( ! response . data ?. authToken && ! this . popup ? .closed ) {
143+ } else if ( this . popup && ! this . popup . closed ) {
149144 setTimeout ( executePoll , 1000 , resolve , reject ) ;
150145 } else {
151146 reject ( new Error ( 'Popup closed without completing login' ) ) ;
@@ -173,7 +168,7 @@ class PlexOAuth {
173168 w : number ;
174169 h : number ;
175170 } ) : Window | void {
176- if ( ! window ) {
171+ if ( typeof window === 'undefined' ) {
177172 throw new Error (
178173 'Window is undefined. Are you running this in the browser?'
179174 ) ;
0 commit comments