Created
December 31, 2016 13:27
-
-
Save acoshift/bb9f77254495ac28b19d3c8cc531ff5a to your computer and use it in GitHub Desktop.
Auth Service using RxJS for store global state
This file contains 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
import { Observable, BehaviorSubject } from 'rxjs' | |
import axios from 'axios' | |
const API_URL = 'http://localhost:8080' | |
const API = { | |
get (url) { | |
return Observable.fromPromise(axios.get(API_URL + url)) | |
} | |
} | |
const $currentUser = new BehaviorSubject(0) // assume 0 is uninit value | |
export default { | |
currentUser: $currentUser.asObservable() | |
.filter((x) => x !== 0), // exclude uninit value | |
fetchCurrentUser () { | |
API.get('/api/me') | |
.subscribe((user) => { | |
$currentUser.next(user) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment