Commit 98222b96 authored by Pau Ruiz i Safont's avatar Pau Ruiz i Safont
Browse files

feature: expose token through the user

Exposing the token to other services is essential for correct usage
of jwt auth, to be able to authenticate requests against secured
services.

So we make it easier to use tokens
parent fc4f85fb
......@@ -101,7 +101,7 @@ import {
<button (click)="auth.tabOpen()">Login new tab</button>
<button (click)="auth.logOut()">Logout</button>
<div *ngIf="(user | async) as user; else loggedOut">
<div *ngIf="user | async; else loggedOut">
<p>Name: {{ user.name }}</p>
<p>Unique Identifier: {{ user.uid }}</p>
<p>Email: {{ user.email }}</p>
......@@ -235,16 +235,14 @@ export class AppComponent implements OnInit {
constructor(
// Public for demonstration purposes
private auth: AuthService,
private tokens: TokenService,
private jwt: JwtHelperService
) {
this.user = auth.user();
this.expiration = this.user.pipe(
map(_ => {
let token = this.tokens.getToken();
map(user => {
try {
return jwt.getTokenExpirationDate(<string>token);
return jwt.getTokenExpirationDate(<string>user.token);
} catch (e) {
return null;
}
......
......@@ -14,6 +14,7 @@
<p>Name: {{ user.name }}</p>
<p>Nickname: {{ user.nickname }}</p>
<p>Email: {{ user.email }}</p>
<p>Token: {{ user.token }}</p>
</div>
<h3>Advanced handling of tokens:</h3>
<div>
......
......@@ -61,6 +61,8 @@ describe('AuthService with a non-expired token', () => {
expect(user.name).toEqual('Ed Munden Gras');
expect(user.nickname).toEqual('6f37a0beb7b16f37a0beb7b1b');
expect(user.email).toEqual('test@ebi.ac.uk');
// tslint:disable-next-line:max-line-length
expect(user.token).toEqual('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3RzaS5lYmkuYWMudWsiLCJleHAiOjEwMDAwMDAwMDAwMDAsImp0aSI6InR6Wmo4Z29xUWVMRVBNakxIMDJwVEEiLCJpYXQiOjE1MTgwODMzNzMsInN1YiI6InVzci03NWY0YjAwMCIsImVtYWlsIjoidGVzdEBlYmkuYWMudWsiLCJuaWNrbmFtZSI6IjZmMzdhMGJlYjdiMTZmMzdhMGJlYjdiMWIiLCJuYW1lIjoiRWQgTXVuZGVuIEdyYXMiLCJkb21haW5zIjpbImFhcC11c2Vycy1kb21haW4iXX0.up6dm5r1KB0yunL5vlyHpf8citI1JqMKlhzdg0oEXII');
}
});
});
......
......@@ -31,6 +31,7 @@ export interface User {
name: string;
nickname: string;
email: string;
token: string;
}
@Injectable()
......@@ -311,7 +312,8 @@ export class AuthService {
uid: < string > this._getClaim('sub'),
name: < string > this._getClaim('name'),
nickname: < string > this._getClaim('nickname'),
email: < string > this._getClaim('email')
email: < string > this._getClaim('email'),
token: < string > this._tokenService.getToken()
});
this._loginCallbacks.map(callback => callback && callback());
......
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