react-saasify-chrisvxd
Version:
React components for Saasify web clients.
97 lines (71 loc) • 2.01 kB
JavaScript
import { autorun, computed, observable } from 'mobx'
import debug from 'lib/debug'
import API from 'lib/api'
import LocalStore from 'store/LocalStore'
import { config as githubConfig } from 'lib/auth-github'
const AUTH_STORE_KEY = 'SaasifyAuth'
class AuthManagerClass {
auth = null
consumer = null
get user() {
return this.auth && this.auth.user
}
isBootstrapping = true
get isAuthenticated() {
return !!this.auth
}
constructor() {
LocalStore.get(AUTH_STORE_KEY)
.then((auth) => {
this.auth = auth
this.isBootstrapping = false
}, () => {
this.isBootstrapping = false
})
}
async signin(opts) {
debug(`AuthManager.signin [${opts.username}]`)
const auth = await API.signin(opts)
if (opts.remember !== false) {
await LocalStore.set(AUTH_STORE_KEY, auth)
}
this.auth = auth
}
async signup(opts) {
debug(`AuthManager.signup [${opts.email}] [${opts.username}]`)
const auth = await API.signup(opts)
await LocalStore.set(AUTH_STORE_KEY, auth)
this.auth = auth
}
async signout() {
debug('AuthManager.signout')
API.signout()
await LocalStore.remove(AUTH_STORE_KEY)
this.auth = null
}
async authWithGitHub(opts) {
debug('AuthManager.authWithGitHub')
const auth = await API.authWithGitHub({
...githubConfig,
...opts
})
await LocalStore.set(AUTH_STORE_KEY, auth)
this.auth = auth
}
async authWithFacebook(opts) {
debug('AuthManager.authWithFacebook')
const auth = await API.authWithFacebook(opts)
await LocalStore.set(AUTH_STORE_KEY, auth)
this.auth = auth
}
}
export const AuthManager = observable(new AuthManagerClass())
// keep API authentication in sync with AuthManager state
autorun(() => {
API.user = AuthManager.auth && AuthManager.auth.user
API.token = AuthManager.auth && AuthManager.auth.token
})
export default AuthManager