From 1553e15d82b8a1ec4967a90d43b33274f8215c44 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 14 Apr 2016 22:07:46 +0200 Subject: Implement user requests autorizations in the client side --- client/angular/users/models/token.ts | 24 +++++++++++++++++++----- client/angular/users/models/user.ts | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 client/angular/users/models/user.ts (limited to 'client/angular/users/models') diff --git a/client/angular/users/models/token.ts b/client/angular/users/models/token.ts index 688dfdc80..906bf501b 100644 --- a/client/angular/users/models/token.ts +++ b/client/angular/users/models/token.ts @@ -3,13 +3,27 @@ export class Token { refresh_token: string; token_type: string; - constructor (hash) { - this.access_token = hash.access_token; - this.refresh_token = hash.refresh_token; - this.token_type = hash.token_type; + constructor (hash?: any) { + if (hash) { + this.access_token = hash.access_token; + this.refresh_token = hash.refresh_token; + if (hash.token_type === 'bearer') { + this.token_type = 'Bearer'; + } else { + this.token_type = hash.token_type; + } + } } - save() { + static load(): Token { + return new Token({ + access_token: localStorage.getItem('access_token'), + refresh_token: localStorage.getItem('refresh_token'), + token_type: localStorage.getItem('token_type') + }); + } + + save():void { localStorage.setItem('access_token', this.access_token); localStorage.setItem('refresh_token', this.refresh_token); localStorage.setItem('token_type', this.token_type); diff --git a/client/angular/users/models/user.ts b/client/angular/users/models/user.ts new file mode 100644 index 000000000..2c56a6132 --- /dev/null +++ b/client/angular/users/models/user.ts @@ -0,0 +1,20 @@ +import { Token } from './token'; + +export class User { + username: string; + token: Token; + + constructor (username: string, hash_token: any) { + this.username = username; + this.token = new Token(hash_token); + } + + static load(): User { + return new User(localStorage.getItem('username'), Token.load()); + } + + save(): void { + localStorage.setItem('username', this.username); + this.token.save(); + } +} -- cgit v1.2.3