Skip to content

Instantly share code, notes, and snippets.

@acoshift
Created December 31, 2016 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acoshift/bb9f77254495ac28b19d3c8cc531ff5a to your computer and use it in GitHub Desktop.
Save acoshift/bb9f77254495ac28b19d3c8cc531ff5a to your computer and use it in GitHub Desktop.
Auth Service using RxJS for store global state
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