Auth0 Home Blog Docs

Problem with auth0.js getting WebAuth is not a constructor

auth0js
getting-started

#1

I’m getting the error:

NodeInvocationException:
TypeError: WEBPACK_IMPORTED_MODULE_2_auth0_js.WebAuth is not a constructor at new AuthService .

I’m using auth0-js: “^8.10.1”. On a angular4 project. i followed the quck guide and my auth.service.ts looks like:

import { Injectable } from ‘@angular/core’;
import { AUTH_CONFIG } from ‘./auth0-variables’;
import { Router } from ‘@angular/router’;
import * as auth0 from ‘auth0-js’;

@Injectable()
export class AuthService {

auth0 = new auth0.WebAuth({
    clientID: 'xxxx',
    domain: 'xxx,
    responseType: 'token id_token',
    audience: xx',
    redirectUri: 'http://localhost:14191/callback',
    scope: 'openid'
});

constructor(public router: Router) { }

public login(): void {
    this.auth0.authorize();
}

public handleAuthentication(): void {
    this.auth0.parseHash((err, authResult) => {
        if (authResult && authResult.accessToken && authResult.idToken) {
            window.location.hash = '';
            this.setSession(authResult);
            this.router.navigate('/home']);
        } else if (err) {
            this.router.navigate('/home']);
            console.log(err);
            alert(`Error: ${err.error}. Check the console for further details.`);
        }
    });
}

private setSession(authResult:any): void {
    // Set the time thapt the access token will expire at
    const expiresAt = JSON.stringify((authResult.expiresIn * 1000) + new Date().getTime());
    localStorage.setItem('access_token', authResult.accessToken);
    localStorage.setItem('id_token', authResult.idToken);
    localStorage.setItem('expires_at', expiresAt);
}

public logout(): void {
    // Remove tokens and expiry time from localStorage
    localStorage.removeItem('access_token');
    localStorage.removeItem('id_token');
    localStorage.removeItem('expires_at');
    // Go back to the home route
    this.router.navigate('/']);
}

public isAuthenticated(): boolean {
    // Check whether the current time is past the
    // access token's expiry time
    const expiresAt = JSON.parse(<string>localStorage.getItem("expires_at"));
    return new Date().getTime() < expiresAt;
}

}


#2