initial commit, have most of the categories crud methods
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { AppState } from '../AppState'
|
||||
import { logger } from '../utils/Logger'
|
||||
import { api } from './AxiosService'
|
||||
|
||||
class AccountService {
|
||||
async getAccount() {
|
||||
try {
|
||||
const res = await api.get('/account')
|
||||
AppState.account = res.data
|
||||
} catch (err) {
|
||||
logger.error('HAVE YOU STARTED YOUR SERVER YET???', err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const accountService = new AccountService()
|
||||
@@ -0,0 +1,45 @@
|
||||
import { initialize } from '@bcwdev/auth0provider-client'
|
||||
import { AppState } from '../AppState'
|
||||
import { audience, clientId, domain } from '../env'
|
||||
import { router } from '../router'
|
||||
import { accountService } from './AccountService'
|
||||
import { api } from './AxiosService'
|
||||
import { socketService } from './SocketService'
|
||||
|
||||
export const AuthService = initialize({
|
||||
domain,
|
||||
clientId,
|
||||
audience,
|
||||
useRefreshTokens: true,
|
||||
onRedirectCallback: appState => {
|
||||
router.push(
|
||||
appState && appState.targetUrl
|
||||
? appState.targetUrl
|
||||
: window.location.pathname
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
AuthService.on(AuthService.AUTH_EVENTS.AUTHENTICATED, async function() {
|
||||
api.defaults.headers.authorization = AuthService.bearer
|
||||
api.interceptors.request.use(refreshAuthToken)
|
||||
AppState.user = AuthService.user
|
||||
await accountService.getAccount()
|
||||
socketService.authenticate(AuthService.bearer)
|
||||
// NOTE if there is something you want to do once the user is authenticated, place that here
|
||||
})
|
||||
|
||||
async function refreshAuthToken(config) {
|
||||
if (!AuthService.isAuthenticated) { return config }
|
||||
const expires = AuthService.identity.exp * 1000
|
||||
const expired = expires < Date.now()
|
||||
const needsRefresh = expires < Date.now() + (1000 * 60 * 60 * 12)
|
||||
if (expired) {
|
||||
await AuthService.loginWithPopup()
|
||||
} else if (needsRefresh) {
|
||||
await AuthService.getTokenSilently()
|
||||
api.defaults.headers.authorization = AuthService.bearer
|
||||
socketService.authenticate(AuthService.bearer)
|
||||
}
|
||||
return config
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import Axios from 'axios'
|
||||
import { baseURL } from '../env'
|
||||
export const api = Axios.create({
|
||||
baseURL,
|
||||
timeout: 8000
|
||||
})
|
||||
@@ -0,0 +1,16 @@
|
||||
import Pop from '../utils/Pop'
|
||||
import { SocketHandler } from '../utils/SocketHandler'
|
||||
|
||||
class SocketService extends SocketHandler {
|
||||
constructor() {
|
||||
super()
|
||||
this
|
||||
.on('error', this.onError)
|
||||
}
|
||||
|
||||
onError(e) {
|
||||
Pop.toast(e.message, 'error')
|
||||
}
|
||||
}
|
||||
|
||||
export const socketService = new SocketService()
|
||||
Reference in New Issue
Block a user