Skip to content

Instantly share code, notes, and snippets.

@ShawInnes
Created May 23, 2017 10:25
Show Gist options
  • Save ShawInnes/04d0809add506448d2e548869f2b6a8e to your computer and use it in GitHub Desktop.
Save ShawInnes/04d0809add506448d2e548869f2b6a8e to your computer and use it in GitHub Desktop.
Angular2 and Auth0 with API

app.component.ts

import { provideAuth, AuthHttp, AuthConfig } from 'angular2-jwt';

export function authHttpServiceFactory(http: Http, options: RequestOptions) {
  return new AuthHttp( new AuthConfig({}), http, options);
}
@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
  ],
  providers: [
    ...
    {
      provide: AuthHttp,
      useFactory: authHttpServiceFactory,
      deps: [ Http, RequestOptions ]
    }
    ...
  ],
  

api.service.ts

export class ApiService {
  constructor(private http: AuthHttp, private anonHttp: Http) { }

  public getVersion(): Promise<any> {
    return this.anonHttp
               .get('/assets/version.json')
               .toPromise()
               .then(response => response.json())
               .catch(this.handleError);
  }

  public getUser(user: User): Promise<any> {
    return this.http
               .get(`${environment.API_URL}/api/user`)
               .toPromise()
               .then(response => Object.assign(user, response.json()))
               .catch(this.handleError);
  }

API Side

import { GetUsersData, ManagementClient, ManagementClientOptions } from "auth0";

const expressJwt = require("express-jwt");
const authenticate = expressJwt({
    secret: process.env.AUTH0_CLIENT_SECRET,
    credentialsRequired: false,
  });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment