1+ const body = {
2+ client_id : "9a1fd200-8687-44b1-4c20-08d50a96e5cd" ,
3+ client_secret : "8b53f727-08e2-4509-8857-e34bf92b27f2" ,
4+ } // can be found in main.js t.prototype.handleLogin
5+
6+ class CC98 {
7+ #accessToken = "" ;
8+ #refreshToken = "" ;
9+ #tokenType = "" ;
10+ #expiresIn = 0 ;
11+ #username: string ;
12+ #password: string ;
13+ constructor ( username : string , password : string ) {
14+ this . #username = username ;
15+ this . #password = password ;
16+ }
17+ async login ( ) {
18+ console . log ( "[CC98] login begins" )
19+ const response = await fetch ( "https://openid.cc98.org/connect/token" , {
20+ method : "POST" ,
21+ headers : {
22+ "Content-Type" : "application/x-www-form-urlencoded" ,
23+ } ,
24+ body : new URLSearchParams ( { ...body , username : this . #username, password : this . #password, grant_type : "password" } ) ,
25+ } ) ;
26+ // const text = await response.text();
27+ // console.log(response.status,text);
28+ // throw 0;
29+ const data = await response . json ( ) ;
30+ if ( response . ok ) {
31+ this . #accessToken = data . access_token ;
32+ this . #refreshToken = data . refresh_token ;
33+ this . #tokenType = data . token_type ;
34+ this . #expiresIn = data . expires_in ;
35+ }
36+ else {
37+ console . log ( "[CC98] login failed" , data . error , data . error_description ) ;
38+ throw new Error ( `Login failed: ${ data . error } - ${ data . error_description } ` ) ;
39+ }
40+ console . log ( "[CC98] login successful" ) ;
41+ return true
42+ }
43+
44+ async #refresh( ) {
45+ console . log ( "[CC98] token refresh begins" ) ;
46+ const response = await fetch ( "https://openid.cc98.org/connect/token" , {
47+ method : "POST" ,
48+ headers : {
49+ "Content-Type" : "application/x-www-form-urlencoded" ,
50+ } ,
51+ body : new URLSearchParams ( {
52+ ...body ,
53+ refresh_token : this . #refreshToken,
54+ grant_type : "refresh_token" ,
55+ } ) ,
56+ } ) ;
57+ const data = await response . json ( ) ;
58+ if ( response . ok ) {
59+ this . #accessToken = data . access_token ;
60+ this . #refreshToken = data . refresh_token ;
61+ this . #tokenType = data . token_type ;
62+ this . #expiresIn = data . expires_in ;
63+ }
64+ else {
65+ console . log ( "[CC98] token refresh failed" , data . error , data . error_description ) ;
66+ throw new Error ( `Login failed: ${ data . error } - ${ data . error_description } ` ) ;
67+ }
68+ console . log ( "[CC98] token refresh successful" ) ;
69+ return true
70+ }
71+
72+ async fetch ( url : string , options : RequestInit = { } ) {
73+ console . log ( "[CC98] fetch:" , url ) ;
74+ if ( ! this . #accessToken) {
75+ await this . login ( ) ;
76+ }
77+ if ( this . #expiresIn <= 60 ) { // Refresh token if it expires in less than 60 seconds
78+ await this . #refresh( ) ;
79+ }
80+ options . headers = {
81+ ...options . headers ,
82+ Authorization : `${ this . #tokenType} ${ this . #accessToken} ` ,
83+ } ;
84+
85+ return fetch ( url , options ) ;
86+ }
87+ }
88+
89+ export { CC98 } ;
0 commit comments