11/* eslint-disable camelcase */
2+
23import Route from '../../Private/BaseRoute.js' ;
34import SpotifyManager from '../../SpotifyManager.js' ;
5+ import { readFileSync , writeFileSync } from 'node:fs' ;
46import type { Request , Response } from 'express' ;
57
68class RefreshRoute extends Route {
@@ -11,16 +13,13 @@ class RefreshRoute extends Route {
1113
1214 override async handle ( req : Request , res : Response ) {
1315 try {
14- if ( ! this . spotify . token ) {
15- return res . status ( 403 ) . json ( { success : false , cause : this . spotify . Application . messages . accountNotLoggedIn } ) ;
16- }
17-
16+ const authData = JSON . parse ( readFileSync ( 'auth.json' , 'utf-8' ) ) ;
1817 const result = await fetch ( 'https://accounts.spotify.com/api/token' , {
1918 method : 'POST' ,
2019 headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } ,
2120 body : new URLSearchParams ( {
2221 grant_type : 'refresh_token' ,
23- refresh_token : this . spotify . token . refresh ,
22+ refresh_token : authData . refresh ,
2423 client_id : process . env . SPOTIFY_CLIENT_ID
2524 } )
2625 } ) ;
@@ -31,14 +30,21 @@ class RefreshRoute extends Route {
3130 return res . status ( 404 ) . json ( { success : false , cause : 'Something went wrong. Please try again' } ) ;
3231 }
3332 const tokenData = await result . json ( ) ;
34- this . spotify . token = {
35- key : tokenData . access_token ,
36- refresh : tokenData . refresh_token ,
37- type : tokenData . token_type ,
38- expiresIn : tokenData . expires_in ,
39- expirationTime : Date . now ( ) + tokenData . expires_in * 1000 ,
40- scope : tokenData . scope . split ( ' ' )
41- } ;
33+ writeFileSync (
34+ 'auth.json' ,
35+ JSON . stringify (
36+ {
37+ key : tokenData . access_token ,
38+ refresh : tokenData . refresh_token ,
39+ type : tokenData . token_type ,
40+ expiresIn : tokenData . expires_in ,
41+ expirationTime : Date . now ( ) + tokenData . expires_in * 1000 ,
42+ scope : tokenData . scope . split ( ' ' )
43+ } ,
44+ null ,
45+ 2
46+ )
47+ ) ;
4248 res . status ( 200 ) . json ( { success : true , message : this . spotify . Application . messages . tokenGenerated } ) ;
4349 } catch ( error ) {
4450 if ( error instanceof Error ) this . spotify . Application . Logger . error ( error ) ;
0 commit comments