Last active
April 12, 2017 22:18
-
-
Save glutengo/5a2bf375da3f0e99e8e38cc2b4f8ce5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Injectable() | |
| export class AuthService { | |
| private token: string; | |
| private _user: BehaviorSubject<any>; | |
| private dataStore; | |
| user: Observable<any>; | |
| private init() { | |
| // create observable and subject | |
| this.dataStore = { user: {}}; | |
| this._user = <BehaviorSubject<any>>new BehaviorSubject({}); | |
| this.user = this._user.asObservable(); | |
| this.setToken(''+this.storage.get('token')); | |
| this.setUser(this.storage.get('user')); | |
| } | |
| setUser(user) { | |
| this.dataStore.user = user; | |
| this._user.next(Object.assign({}, this.dataStore).user); | |
| this.storage.set('user', user); | |
| } | |
| getUser() { | |
| return this.dataStore.user; | |
| } | |
| logout() { | |
| this.setUser(undefined); | |
| this.storage.remove('token'); | |
| this.storage.remove('user'); | |
| this.router.navigate(['login']); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment