@@ -5,22 +5,29 @@ const NAMESPACE = 'Callback';
55
66/** Import dependecies */
77const Spotify = require ( '../controller/Spotify.controller' ) ;
8-
8+
99
1010router . get ( '/spotify' , async ( req : Request , res : Response ) => {
1111 try {
12- globalThis . Logger . log ( 'info' , `[${ NAMESPACE } ] Authenticating from Spotify ` , { ...globalThis . LoggingParams } )
12+ globalThis . Logger . log ( 'info' , `[${ NAMESPACE } ] Authenticating from Spotify ` , { ...globalThis . LoggingParams } )
1313
1414 const { code, state } = req . query ;
1515 const [ userId , device ] = state . toString ( ) . split ( "+" ) ;
1616 const { email, display_name, product, country, spotifyId,
1717 expires_in, access_token, refresh_token } = await Spotify . authorizeUser ( code ) ;
1818
19- await Spotify . storeSpotifyCredentials ( {
20- email, display_name, product, country, spotifyId,
21- expires_in, access_token, refresh_token
22- } , userId ) ;
2319 const { isDuplicate, providerId } = await Spotify . checkIfSpotifyAccountDuplicate ( userId , spotifyId ) ;
20+ if ( isDuplicate ) {
21+ await Spotify . updateSpotifyCredentials ( providerId , {
22+ email, display_name, product, country, spotifyId,
23+ expires_in, access_token, refresh_token
24+ } , userId ) ;
25+ } else {
26+ await Spotify . storeSpotifyCredentials ( {
27+ email, display_name, product, country, spotifyId,
28+ expires_in, access_token, refresh_token
29+ } , userId ) ;
30+ }
2431
2532 /** Requirement for Hosts only
2633 if(product != "premium") res.status().json({
@@ -32,11 +39,11 @@ router.get('/spotify', async (req: Request, res: Response) => {
3239 } else if ( device == 'Android' ) {
3340 res . redirect ( "https://fonzmusic.com/spotify" ) ;
3441 } else {
35- res . status ( 400 ) . send ( { message : "Invalid device" } ) ;
42+ res . status ( 400 ) . send ( { message : "Invalid device" } ) ;
3643 }
3744 } catch ( error ) {
3845 globalThis . Logger . log ( 'error' , `[${ NAMESPACE } ] Could not Authenticate from Spotify ` , { ...globalThis . LoggingParams , error } , )
39-
46+
4047 res . status ( error . status || 500 ) . send ( error ) ;
4148 }
4249} ) ;
0 commit comments