Reputation: 195
I'm trying to drop some authentication into my Angular2 app using this bit of code that was mentioned in the Angular2 gitter channel: https://github.com/ronzeidman/ng2-ui-auth and I think I may not be understanding something completely.
I get the following errors in the console:
Uncaught SyntaxError: Unexpected token <
in (program):1
and
Uncaught SyntaxError: Unexpected token <
in angular2-polyfills.js:138
But in the body of that second error, it says the following:
Evaluating http://localhost:3001/node_modules/ng2-ui-auth/src/auth
Error loading http://localhost:3001/app/boot.js
Relevant code:
app/boot.ts:
import {bootstrap} from 'angular2/platform/browser';
import {provide} from 'angular2/core';
import {AuthHttp} from 'angular2-jwt';
import {AppComponent} from './app.component';
import {HTTP_PROVIDERS} from 'angular2/http';
import {Config, SATELLIZER_PROVIDERS, Auth} from 'ng2-ui-auth';
import 'rxjs/add/operator/map'
bootstrap(AppComponent, [
HTTP_PROVIDERS,
SATELLIZER_PROVIDERS({providers: {google: {clientId: GOOGLE_CLIENT_ID}}}),
provide(AuthHttp, {
useFactory: (auth: Auth, config: Config) => {
return new AuthHttp({
tokenName: config.tokenName,
tokenGetter: () => auth.getToken(),
});
},
deps: [Auth, Config],
}),
]);
index.html:
<html>
<head>
<title>App Name</title>
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="config.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="node_modules/angular2/bundles/http.js"></script>
<script src="node_modules/angular2/bundles/router.dev.js"></script>
<script>System.import('app/boot').then(null, console.error.bind(console));</script>
</head>
<body>
<cw-api-app>Loading...<cw-api-app>
</body>
</html>
config.js:
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
},
map: {
'angular2-jwt': 'node_modules/angular2-jwt/angular2-jwt.js',
'browser': 'node_modules/angular2/platform/browser.js',
'ng2-ui-auth': 'node_modules/ng2-ui-auth/export.js',
'auth': 'node_modules/ng2-ui-auth/export.js'
}
});
app/app.component.ts:
import {Component} from 'angular2/core';
import {UserService} from './services/user.service';
import {LoginComponent} from './login.component';
import {UserDashboardComponent} from './user-dashboard.component';
@Component({
selector: 'cw-api-app',
template: `
<h1>{{title}}</h1>
<div><p>Test!</p></div>
<ul><li (click)="userFetch(event)">Blah</li></ul>
<div class="user-info">
<cw-user-dashboard [user]="user"></cw-user-dashboard>
</div>
<app-login></app-login>
`
directives: [UserDashboardComponent, LoginComponent],
providers: [UserService]
})
export class AppComponent {
public title = "App Name";
public user: Object;
constructor(private _userService: UserService) { }
userFetch() {
console.log("wtf");
this._userService.getUserDashboard().subscribe(res => this.user = res);
}
}
app/login.component.ts:
import {Component, AfterContentInit, ElementRef, Renderer} from 'angular2/core';
import {Router, ROUTER_DIRECTIVES} from 'angular2/router';
import {FormBuilder, ControlGroup, Validators} from 'angular2/common';
import {Auth} from 'ng2-ui-auth';
//import {NgMessages} from '../formComponents/ngMessages';
//import {CustomValidators} from '../formComponents/customValidators';
//import {DefaultHandlers} from '../httpDefaults';
/**
* Created by Ron on 18/12/2015.
*/
@Component({
selector: 'app-login',
// templateUrl: '../templates/login_form.html',
directives: [NgMessages, ROUTER_DIRECTIVES],
})
export class Login implements AfterContentInit {
form: ControlGroup;
user = { email: '', password: '' };
userControlsConfig = {
email: ['', Validators.compose([Validators.required, CustomValidators.email])],
password: ['', Validators.required],
};
login() {
this.auth.login(this.user)
.subscribe(
() => this.goToMain(),
this.handlers.error
);
}
authenticate(provider: string) {
this.auth.authenticate(provider)
.subscribe(
() => this.goToMain(),
this.handlers.error
);
}
goToMain() {
this.router.navigate(['Main']);
}
ngAfterContentInit() {
this.renderer.setElementClass(this.element, 'app', true);
if (this.auth.isAuthenticated()) {
this.goToMain();
}
}
constructor(private auth: Auth,
private fb: FormBuilder,
private router: Router,
private element: ElementRef,
private renderer: Renderer,
private handlers: DefaultHandlers) {
this.form = fb.group(this.userControlsConfig);
}
}
Upvotes: 0
Views: 1781
Reputation: 467
look at this: Evaluating http://localhost:3001/node_modules/ng2-ui-auth/src/auth
, it tries to get the ng2-ui-auth/src/auth without the .js extention, in your config file you've put
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
},
which means the default extension only exists within the 'app' folder, try adding default extension to the entire system or specifically to ng2-ui-auth. If you wish add this issue to my github and I'll just add all js extensions explicitly.
--- edit ---
Tried adding js extenstions, doesn't work well with typescript. concating everything to a single file may work but typescript supports only 'amd' and 'system' module types and people really like the commonjs one. If someone has a good solution for this he is welcome to make a pull request (it will probably be a gulpfile which handles it)
--- second edit ---
In version 1.1.0 all files are merged so this shouldn't be a problem.
Upvotes: 2
Reputation: 381
UPD 1: Finally I managed to resolve the initial problem: Implicit OAuth2 with Angular 2. Again, this is not a direct answer to topic starter, but might be more general answer.
Look at this gist
There you will find how to implement Client Side OAuth2 with another Angular 2 TS code.
Credits to Michael Oryl.
======= Below is the Initial Answer ======
I don't have precise answer 'how do I fix it', but I believe the cause of the issue is related to the 'import' statements.
import {bootstrap} from 'angular2/platform/browser';
import {provide} from 'angular2/core'; // << This works OK
import {HTTP_PROVIDERS} from 'angular2/http';
import {NG2_UI_AUTH_PROVIDERS, JwtHttp} from 'ng2-ui-auth'; // << This tries to load a script from URL like '{SERVER_ROOT}/ng2-ui-auth', but gets the HTML from node.js
In 'node_modules' there is file 'angular2/core.ts' and the same path in second import statement. But 'ng2-ui-auth' has only 'export.ts'. I tried to change import path to 'ng2-ui-auth/export' without success.
It is possible that export.ts is supposed to be imported by default using the 'ng2-ui-auth' path so import statements exposed by Ron is correct. So, this is just my guess what I described above.
If I'm, right we need to find out what file should be used in 'import' statement.
Upvotes: 2