Commit 60b33f7e authored by Eduardo Sanz García's avatar Eduardo Sanz García
Browse files

feat: added Credentials interface for fast access

parent b35abed3
Pipeline #2762 passed with stages
in 3 minutes and 31 seconds
<a name="1.0.0-beta.4"></a>
# [1.0.0-beta.4](https://gitlab.ebi.ac.uk/tools-glue/angular-app-auth/compare/1.0.0-beta.3...1.0.0-beta.4) (2018-02-15)
### Features
* **auth:** added Credential interface for easy access to user data.
[//]: # * **compiler:** mark @NgModules in provider lists for identification at runtime ([#22005](https://github.com/angular/angular/issues/22005)) ([2d5e7d1](https://github.com/angular/angular/commit/2d5e7d1))
......@@ -60,6 +60,56 @@ The default configuration uses localStorage to save the JWT token under the key
Example use on a component:
```typescript
import {
Component,
OnInit
} from '@angular/core';
import {
Observable,
} from 'rxjs/Observable';
import {
AuthService,
Credentials
} from 'angular-aap-auth';
@Component({
selector: 'app-root',
template: `
<button (click)="auth.windowOpen()">Login small window</button>
<button (click)="auth.tabOpen()" target="_blank">Login new tab</button>
<button (click)="auth.logOut()" target="_blank">Logout</button>
<div *ngIf="(credentials | async) as user; else loggedOut">
<p>Real name: {{ user.realname }}</p>
<p>Username: {{ user.username }}</p>
<p>Token: {{ user.token }}</p>
</div>
<ng-template #loggedOut>
<p>Please, log in.</p>
</ng-template>
`
})
export class AppComponent implements OnInit {
credentials: Observable < Credentials | null > ;
constructor(
// Public for demonstration purposes
public auth: AuthService,
) {
this.credentials = auth.credentials();
}
ngOnInit() {
this.auth.addLogInEventListener(() => console.log('Welcome'));
this.auth.addLogOutEventListener(() => console.log('Bye'));
}
}
```
Alternative approach:
```typescript
import {
Component,
......
......@@ -24,8 +24,8 @@ module.exports = function (config) {
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
// browsers: ['Chrome'],
browsers: ['ChromeCanary'],
browsers: ['Chrome'],
// browsers: ['ChromeCanary'],
singleRun: false
});
};
{
"name": "angular-aap-auth",
"version": "1.0.0-alpha.3",
"version": "1.0.0-alpha.4",
"license": "Apache-2.0",
"scripts": {
"ng": "ng",
......
......@@ -9,6 +9,16 @@
<button (click)="auth.tabOpen({ttl: '1'})" target="_blank">Login new tab</button>
<button (click)="auth.logOut()" target="_blank">Logout</button>
</div>
<div *ngIf="(credentials | async) as user; else loggedOut">
<p>Real name: {{ user.realname }}</p>
<p>Username: {{ user.username }}</p>
<p>Token: {{ user.token }}</p>
</div>
<ng-template #loggedOut>
<p>Please, log in.</p>
</ng-template>
<h3>Alternative approach</h3>
<div>
<p>Authenticated: {{ isAuthenticated|async }}</p>
<p>Real name: {{ realname|async }}</p>
......
......@@ -11,6 +11,7 @@ import {
import {
AuthService,
Credentials
} from 'app/modules/auth/auth.service';
import {
JwtHelperService,
......@@ -22,6 +23,9 @@ import {
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
credentials: Observable< Credentials | null>;
// More specific
username: Observable < string | null > ;
realname: Observable < string | null > ;
token: Observable < string | null > ;
......@@ -35,6 +39,8 @@ export class AppComponent implements OnInit {
public auth: AuthService,
private jwt: JwtHelperService
) {
this.credentials = auth.credentials();
this.username = auth.username();
this.realname = auth.realname();
this.token = auth.token();
......
......@@ -26,9 +26,18 @@ export interface LoginOptions {
[key: string]: string;
}
export interface Credentials {
realname: string;
username: string;
token: string;
}
@Injectable()
export class AuthService {
private _credentials = new BehaviorSubject < Credentials | null > (null);
private _credentials$ = this._credentials.asObservable();
private _realname = new BehaviorSubject < string | null > (null);
private _realname$ = this._realname.asObservable();
......@@ -70,6 +79,10 @@ export class AuthService {
return this._isAuthenticated$;
}
public credentials(): Observable < Credentials | null > {
return this._credentials$;
}
public realname(): Observable < string | null > {
return this._realname$;
}
......@@ -298,9 +311,18 @@ export class AuthService {
this._isAuthenticated.next(isAuthenticated);
if (isAuthenticated) {
this._username.next(this._getUserName());
this._realname.next(this._getRealName());
this._token.next(this._getToken());
const realname = this._getRealName();
const username = this._getUserName();
const token = this._getToken();
this._credentials.next({
realname: < string > realname,
username: < string > username,
token: < string > token
});
this._realname.next(realname);
this._username.next(username);
this._token.next(token);
this._loginCallbacks.map(callback => callback && callback());
......@@ -313,6 +335,7 @@ export class AuthService {
const delay = +expireDate - +new Date();
this._timeoutID = window.setTimeout(this.logOut, delay);
} else {
this._credentials.next(null);
this._username.next(null);
this._realname.next(null);
this._token.next(null);
......
......@@ -270,10 +270,14 @@ agent-base@2:
extend "~3.0.0"
semver "~5.0.1"
ajv-keywords@^2.0.0, ajv-keywords@^2.1.0:
ajv-keywords@^2.0.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-2.1.1.tgz#617997fc5f60576894c435f940d819e135b80762"
ajv-keywords@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-3.1.0.tgz#ac2b27939c543e95d2c06e7f7f5c27be4aa543be"
ajv@^4.9.1:
version "4.11.8"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-4.11.8.tgz#82ffb02b29e662ae53bdc20af15947706739c536"
......@@ -290,6 +294,14 @@ ajv@^5.0.0, ajv@^5.1.0, ajv@^5.1.5, ajv@~5.5.1:
fast-json-stable-stringify "^2.0.0"
json-schema-traverse "^0.3.0"
ajv@^6.1.0:
version "6.1.1"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.1.1.tgz#978d597fbc2b7d0e5a5c3ddeb149a682f2abfa0e"
dependencies:
fast-deep-equal "^1.0.0"
fast-json-stable-stringify "^2.0.0"
json-schema-traverse "^0.3.0"
align-text@^0.1.1, align-text@^0.1.3:
version "0.1.4"
resolved "https://registry.yarnpkg.com/align-text/-/align-text-0.1.4.tgz#0cd90a561093f35d0a99256c22b7069433fad117"
......@@ -3855,17 +3867,17 @@ isstream@~0.1.2:
resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a"
istanbul-api@^1.1.14:
version "1.2.1"
resolved "https://registry.yarnpkg.com/istanbul-api/-/istanbul-api-1.2.1.tgz#0c60a0515eb11c7d65c6b50bba2c6e999acd8620"
version "1.2.2"
resolved "https://registry.yarnpkg.com/istanbul-api/-/istanbul-api-1.2.2.tgz#e17cd519dd5ec4141197f246fdf380b75487f3b1"
dependencies:
async "^2.1.4"
fileset "^2.0.2"
istanbul-lib-coverage "^1.1.1"
istanbul-lib-coverage "^1.1.2"
istanbul-lib-hook "^1.1.0"
istanbul-lib-instrument "^1.9.1"
istanbul-lib-report "^1.1.2"
istanbul-lib-source-maps "^1.2.2"
istanbul-reports "^1.1.3"
istanbul-lib-instrument "^1.9.2"
istanbul-lib-report "^1.1.3"
istanbul-lib-source-maps "^1.2.3"
istanbul-reports "^1.1.4"
js-yaml "^3.7.0"
mkdirp "^0.5.1"
once "^1.4.0"
......@@ -3879,9 +3891,9 @@ istanbul-instrumenter-loader@^3.0.0:
loader-utils "^1.1.0"
schema-utils "^0.3.0"
istanbul-lib-coverage@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/istanbul-lib-coverage/-/istanbul-lib-coverage-1.1.1.tgz#73bfb998885299415c93d38a3e9adf784a77a9da"
istanbul-lib-coverage@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/istanbul-lib-coverage/-/istanbul-lib-coverage-1.1.2.tgz#4113c8ff6b7a40a1ef7350b01016331f63afde14"
istanbul-lib-hook@^1.1.0:
version "1.1.0"
......@@ -3889,40 +3901,40 @@ istanbul-lib-hook@^1.1.0:
dependencies:
append-transform "^0.4.0"
istanbul-lib-instrument@^1.7.3, istanbul-lib-instrument@^1.9.1:
version "1.9.1"
resolved "https://registry.yarnpkg.com/istanbul-lib-instrument/-/istanbul-lib-instrument-1.9.1.tgz#250b30b3531e5d3251299fdd64b0b2c9db6b558e"
istanbul-lib-instrument@^1.7.3, istanbul-lib-instrument@^1.9.2:
version "1.9.2"
resolved "https://registry.yarnpkg.com/istanbul-lib-instrument/-/istanbul-lib-instrument-1.9.2.tgz#84905bf47f7e0b401d6b840da7bad67086b4aab6"
dependencies:
babel-generator "^6.18.0"
babel-template "^6.16.0"
babel-traverse "^6.18.0"
babel-types "^6.18.0"
babylon "^6.18.0"
istanbul-lib-coverage "^1.1.1"
istanbul-lib-coverage "^1.1.2"
semver "^5.3.0"
istanbul-lib-report@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/istanbul-lib-report/-/istanbul-lib-report-1.1.2.tgz#922be27c13b9511b979bd1587359f69798c1d425"
istanbul-lib-report@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/istanbul-lib-report/-/istanbul-lib-report-1.1.3.tgz#2df12188c0fa77990c0d2176d2d0ba3394188259"
dependencies:
istanbul-lib-coverage "^1.1.1"
istanbul-lib-coverage "^1.1.2"
mkdirp "^0.5.1"
path-parse "^1.0.5"
supports-color "^3.1.2"
istanbul-lib-source-maps@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/istanbul-lib-source-maps/-/istanbul-lib-source-maps-1.2.2.tgz#750578602435f28a0c04ee6d7d9e0f2960e62c1c"
istanbul-lib-source-maps@^1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/istanbul-lib-source-maps/-/istanbul-lib-source-maps-1.2.3.tgz#20fb54b14e14b3fb6edb6aca3571fd2143db44e6"
dependencies:
debug "^3.1.0"
istanbul-lib-coverage "^1.1.1"
istanbul-lib-coverage "^1.1.2"
mkdirp "^0.5.1"
rimraf "^2.6.1"
source-map "^0.5.3"
istanbul-reports@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/istanbul-reports/-/istanbul-reports-1.1.3.tgz#3b9e1e8defb6d18b1d425da8e8b32c5a163f2d10"
istanbul-reports@^1.1.4:
version "1.1.4"
resolved "https://registry.yarnpkg.com/istanbul-reports/-/istanbul-reports-1.1.4.tgz#5ccba5e22b7b5a5d91d5e0a830f89be334bf97bd"
dependencies:
handlebars "^4.0.3"
......@@ -6539,11 +6551,11 @@ schema-utils@^0.3.0:
ajv "^5.0.0"
schema-utils@^0.4.0, schema-utils@^0.4.2:
version "0.4.3"
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-0.4.3.tgz#e2a594d3395834d5e15da22b48be13517859458e"
version "0.4.5"
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-0.4.5.tgz#21836f0608aac17b78f9e3e24daff14a5ca13a3e"
dependencies:
ajv "^5.0.0"
ajv-keywords "^2.1.0"
ajv "^6.1.0"
ajv-keywords "^3.1.0"
scss-tokenizer@^0.2.3:
version "0.2.3"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment