import { ChangeDetectionStrategy , Component , OnInit } from '@angular/core' ; import { ActivatedRoute , Params , Router } from '@angular/router' ; import { StateService } from '../services/state.service' ; @ Component ( { selector : 'spotify-auth' , templateUrl : './authorized.component.html' , styleUrls : [ './authorized.component.scss' ] , changeDetection : ChangeDetectionStrategy . OnPush , } ) export class AuthorizedComponent implements OnInit { public constructor ( private activatedRoute , private router , private _stateService ) { } public async ngOnInit ( ) { this . _stateService . setLoading ( true ) ; const params = new URLSearchParams ( this . activatedRoute . snapshot . fragment ) ; let redirectToUrl ; for ( const param of params ) { if ( param [ 0 ] === 'state' ) { redirectToUrl = decodeURIComponent ( atob ( param [ 1 ] ) ) ; } } if ( redirectToUrl && redirectToUrl !== '/' ) { setTimeout ( async ( ) => { const url = new URL ( window . location . origin + redirectToUrl ) ; const queryParams = { } ; url . searchParams . forEach ( ( value , key ) => { queryParams [ key ] = value ; } ) ; await this . router . navigate ( [ url . pathname ] , { queryParams } ) ; } ) ; } else { await this . router . navigate ( [ '/' ] ) ; } this . _stateService . setLoading ( false ) ; } }