npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

spotify-auth

v2.0.6

Published

Library spotify-auth for angular 5

Downloads

18

Readme

Demo

App: https://7jpsan.github.io/spotify-auth-demo/ Repo: https://github.com/7jpsan/spotify-auth-demo/

Spotify Auth - Angular 5

This is a simple library that handles the authentication workflow for an angular 5 application interacting with angular api in a JS only environment, no backend service is required!

Features (v2+):

  • An auth library for spotify api v1 and angular 5
  • A demo application that exemplifies the use.

Coming next:

  • Handle token expiration / invalidity
  • Handle session persistence

Difference of this fork

This is a fork/implementation of https://github.com/cyrilletuzi/angular-quickstart-lib which was forked from https://github.com/filipesilva/angular-quickstart-lib go ahead and read their work, it is awesome!

Gotchas

After a lot of time, using the @angular/router and/or RouterModule in a library is quite complicated... I have decided to remove it and expose the mechanisms to the consumer app.

Requirements

Make sure you have at least Node 6.9 and NPM 3.0 installed.

Install

npm install spotify-auth

Usage

  • Inherit the http Interceptor spotify-auth.interceptor.ts

    import { Injectable } from  '@angular/core';
    import  'rxjs/add/operator/do'; //Required, yes!
    import { TokenService, SpotifyAuthInterceptor } from  'spotify-auth';
    
    @Injectable()
    export  class  SpotifyAuthInterceptor2  extends  SpotifyAuthInterceptor {
      	
    	doOnError(err:  any):  void {}
      	
    	constructor(tokenSvc:  TokenService) {
    		super(tokenSvc);
    	}
    }
  • Listen for the authService.authorizedStream and route accordingly. When the login is complete, the stream will emit a true value, which means everything is ok (it hit the authorized url with the right stuff, you should now go to a route you have defined in your router module.

    app.component.ts

    import { Component, OnInit } from  '@angular/core';
    import { AuthService, ScopesBuilder, AuthConfig, TokenService } from  'spotify-auth';
    ...
    constructor(
    	private  infoSvc:  InfoService,
    	private  tokenSvc:  TokenService,
    	private  authService:  AuthService,
    	private  router:  Router
    ) {}
    
    ngOnInit():  void {
    	this.authService.authorizedStream.pipe(filter(x  =>  x)).subscribe(() => {
    		this.router.navigate(['user']);	
    	});
    }
    ...
  • Create an authorize/login method that uses the module: login.component.ts

    	import { Component, OnInit } from  '@angular/core';
    	import { AuthService, ScopesBuilder, AuthConfig } from 'spotify-auth';
    	...
    	@Component({...});
    	export class SomeClass(){
    		constructor(
    			private  authService:  AuthService,
    			private  tokenSvc:  TokenService,
    			private  router:  Router) { }
    		...
    		public  login():  void {
    			const scopes = new ScopesBuilder()/* .withScopes(ScopesBuilder.LIBRARY) */.build();
    				const ac:  AuthConfig  = {
    					// WebPortal App Id. Your application id from spotify
    					client_id:  "3af5f43840144db2a5ef883b56c5fb7e", 
    					response_type:  "token",
    					// Whitelisted URL, must end with the authozed path for the magic to happen.
    					//i.e:(http://your-domain/yourapp/authorized Or http://localhost:4200/authorized)
    					redirect_uri:  "http://7jpsan.github.io/spotify-auth-demo/authorized", 
    					state:  "",
    					show_dialog:  true,
    					scope:  scopes
    				};
    				this.authService.configure(ac).authorize(); // Magic happens here
    		}
    	}
  • app.module.ts and/or app-routing.module.ts :

    	import { NgModule, Injectable } from  '@angular/core';
    	...
    	import { SpotifyAuthModule } from  'spotify-auth';
    	import { SpotifyAuthInterceptor2 } from  './spotify-auth.interceptor';
    
        const  routes:  Routes  = [
    
    {
    	path:  '',
    	redirectTo:  'user',
    	pathMatch:  'full'
    },
    {
    	path:  'user',
    	component:  UserComponent
    },
    ...,
    	SpotifyAuthModule.authRoutes()[0] //(Static guarded route with component)
    ];
    NgModule({
    	imports: [
    	BrowserModule,
    	HttpClientModule,
    	HttpModule,
    	SpotifyAuthModule.forRoot(),  //forRoot!!!!
    	RouterModule.forRoot(routes),
    	],
    	declarations: [ AppComponent, UserComponent, LoginComponent, AlbumsComponent ],
    	bootstrap: [ AppComponent ],
    	exports: [],
    	providers: [
    			InfoService,
    			{
    				provide:  HTTP_INTERCEPTORS,
    				//Force interception to use your new shiny headers!
    				useClass:  SpotifyAuthInterceptor2,
    				multi:  true
    			}
    		]
    	})
    export  class  AppModule {}

That is it! The code should take care of the rest for you. Enjoy! There is a demo app here https://github.com/7jpsan/spotify-auth-demo. Ignore the one present in this repo. It was split, but never removed. Self TODO :)

If anything goes wrong, please raise an issue!