Skip to content

Commit

Permalink
didn't test but changed add call to _setAuthHeaders inside auth.service
Browse files Browse the repository at this point in the history
  • Loading branch information
jmurphzyo committed Apr 18, 2017
1 parent e201b36 commit 154d0e0
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 39 deletions.
60 changes: 60 additions & 0 deletions .angular-cli.json
@@ -0,0 +1,60 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"project": {
"name": "angular2-oidc-client"
},
"apps": [
{
"root": "src",
"outDir": "dist",
"assets": [
{"glob":"**/*.js", "input":"../node_modules/oidc-client/dist/","output":"./"},
{ "glob": "**/*", "input": "./assets/", "output": "./assets/" },
{ "glob": "favicon.ico", "input": "./", "output": "./" },
{ "glob": "auth.html", "input": "./", "output": "./" }

],
"index": "index.html",
"main": "main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",
"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",
"prefix": "app",
"styles": [
"styles.scss"
],
"scripts": [],
"environmentSource": "environments/environment.ts",
"environments": {
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
}
],
"e2e": {
"protractor": {
"config": "./protractor.conf.js"
}
},
"lint": [
{
"project": "src/tsconfig.app.json"
},
{
"project": "src/tsconfig.spec.json"
},
{
"project": "e2e/tsconfig.e2e.json"
}
],
"test": {
"karma": {
"config": "./karma.conf.js"
}
},
"defaults": {
"styleExt": "scss",
"component": {}
}
}
4 changes: 3 additions & 1 deletion .vscode/settings.json
@@ -1 +1,3 @@
// Place your settings in this file to overwrite default and user settings.
{
"vsicons.presets.angular": true
} // Place your settings in this file to overwrite default and user settings.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -28,7 +28,7 @@
"zone.js": "0.6.23"
},
"devDependencies": {
"angular-cli": "1.0.0-beta.20-4",
"angular-cli": "1.0.0",
"@angular/compiler-cli": "2.1.0",
"@types/jasmine": "^2.2.30",
"@types/node": "^6.0.42",
Expand Down
80 changes: 43 additions & 37 deletions src/app/shared/services/auth.service.ts
Expand Up @@ -2,15 +2,31 @@ import { Injectable, EventEmitter } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import { Observable } from 'rxjs/Rx';

import { UserManager, Log, MetadataService, User } from 'oidc-client';
import { UserManager, User } from 'oidc-client';
import { environment } from '../../';

const settings: any = {
authority: 'http://localhost:5000/oidc',
client_id: 'js.tokenmanager',
redirect_uri: 'http://localhost:4200/auth.html',
post_logout_redirect_uri: 'http://localhost:4200/',
response_type: 'id_token token',
scope: 'openid email roles',

silent_redirect_uri: 'http://localhost:4200',
automaticSilentRenew: true,
// silentRequestTimeout:10000,

filterProtocolClaims: true,
loadUserInfo: true
};

@Injectable()
export class AuthService {
mgr: UserManager = new UserManager(settings);
userLoadededEvent: EventEmitter<User> = new EventEmitter<User>();
currentUser:User;
loggedIn: boolean = false;
currentUser: User;
loggedIn = false;

authHeaders: Headers;

Expand All @@ -32,22 +48,22 @@ export class AuthService {
});
this.mgr.events.addUserUnloaded((e) => {
if (!environment.production) {
console.log("user unloaded");
console.log('user unloaded');
}
this.loggedIn = false;
});
}
clearState() {
this.mgr.clearStaleState().then(function () {
console.log("clearStateState success");
console.log('clearStateState success');
}).catch(function (e) {
console.log("clearStateState error", e.message);
console.log('clearStateState error', e.message);
});
}

getUser() {
this.mgr.getUser().then((user) => {
console.log("got user", user);
console.log('got user', user);
this.userLoadededEvent.emit(user);
}).catch(function (err) {
console.log(err);
Expand All @@ -57,42 +73,42 @@ export class AuthService {
removeUser() {
this.mgr.removeUser().then(() => {
this.userLoadededEvent.emit(null);
console.log("user removed");
console.log('user removed');
}).catch(function (err) {
console.log(err);
});
}

startSigninMainWindow() {
this.mgr.signinRedirect({ data: 'some data' }).then(function () {
console.log("signinRedirect done");
console.log('signinRedirect done');
}).catch(function (err) {
console.log(err);
});
}
endSigninMainWindow() {
this.mgr.signinRedirectCallback().then(function (user) {
console.log("signed in", user);
console.log('signed in', user);
}).catch(function (err) {
console.log(err);
});
}

startSignoutMainWindow() {
this.mgr.signoutRedirect().then(function (resp) {
console.log("signed out", resp);
console.log('signed out', resp);
setTimeout(5000, () => {
console.log("testing to see if fired...");
console.log('testing to see if fired...');

})
});
}).catch(function (err) {
console.log(err);
});
};

endSignoutMainWindow() {
this.mgr.signoutRedirectCallback().then(function (resp) {
console.log("signed out", resp);
console.log('signed out', resp);
}).catch(function (err) {
console.log(err);
});
Expand Down Expand Up @@ -148,45 +164,35 @@ export class AuthService {

if (options) {
options = this._setRequestOptions(options);
}
else {
} else {
options = this._setRequestOptions();
}
return this.http.post(url, body, options);
}


private _setAuthHeaders(user: any) {
private _setAuthHeaders(user: any): void {
this.authHeaders = new Headers();
this.authHeaders.append('Authorization', user.token_type + " " + user.access_token);
this.authHeaders.append('Content-Type', 'application/json');
this.authHeaders.append('Authorization', user.token_type + ' ' + user.access_token);
if (this.authHeaders.get('Content-Type')) {

} else {
this.authHeaders.append('Content-Type', 'application/json');
}
}
private _setRequestOptions(options?: RequestOptions) {

if (this.loggedIn) {
this._setAuthHeaders(this.currentUser);
}
if (options) {
options.headers.append(this.authHeaders.keys[0], this.authHeaders.values[0]);
}
else {
options = new RequestOptions({ headers: this.authHeaders, body: "" });
} else {
options = new RequestOptions({ headers: this.authHeaders, body: '' });
}

return options;
}

}

const settings: any = {
authority: 'http://localhost:5000/oidc',
client_id: 'js.tokenmanager',
redirect_uri: 'http://localhost:4200/auth.html',
post_logout_redirect_uri: 'http://localhost:4200/',
response_type: 'id_token token',
scope: 'openid email roles',

silent_redirect_uri: 'http://localhost:4200',
automaticSilentRenew: true,
//silentRequestTimeout:10000,

filterProtocolClaims: true,
loadUserInfo: true
};

0 comments on commit 154d0e0

Please sign in to comment.