-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
207 additions
and
76 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
NEXT_PUBLIC_DOMAIN=$PUBLIC_DOMAIN | ||
NEXT_PUBLIC_API_URL=http://localhost:8055 | ||
NEXT_PUBLIC_WS_URL=ws://localhost:8055/websocket | ||
NEXT_PUBLIC_CHATWOOT_URL=http://localhost:3001 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
NEXT_PUBLIC_DOMAIN=$PUBLIC_DOMAIN | ||
NEXT_PUBLIC_API_URL=https://directus.$PUBLIC_DOMAIN | ||
NEXT_PUBLIC_WS_URL=wss://directus.$PUBLIC_DOMAIN/websocket | ||
NEXT_PUBLIC_CHATWOOT_URL=https://chatwoot.$PUBLIC_DOMAIN |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,154 @@ | ||
import env from '@beam-australia/react-env'; | ||
import { Directus } from '@directus/sdk'; | ||
|
||
export const url = env('API_URL') || 'http://localhost:8055'; | ||
import locales from '@/locales'; | ||
import { AuthStore } from '@/stores/AuthStore'; | ||
import { LocaleStore } from '@/stores/LocaleStore'; | ||
|
||
export const url = env('API_URL'); | ||
export const directus = new Directus(url, { storage: { prefix: 'jaa_' } }); | ||
|
||
/** | ||
* Get current user from API. | ||
* @returns User object if session is valid, otherwise 'false'. | ||
* @returns User object if session is valid. | ||
*/ | ||
export async function getUser() { | ||
try { | ||
// Fetch and return user (also to check if session is valid) | ||
return await directus.users.me.read(); | ||
} catch { | ||
// Something is wrong | ||
return false; | ||
return; | ||
} | ||
} | ||
|
||
/** Get current auth token. */ | ||
export const getBearer = async () => { | ||
/** | ||
* Get current access token. | ||
* @returns Access token. | ||
*/ | ||
export async function getToken() { | ||
try { | ||
await directus.auth.refreshIfExpired(); | ||
} catch { | ||
// Ignore error | ||
} | ||
return `Bearer ${await directus.auth.token}`; | ||
}; | ||
return directus.auth.token; | ||
} | ||
|
||
export async function getBearer() { | ||
return `Bearer ${await getToken()}`; | ||
} | ||
|
||
/** | ||
* Login to the API. | ||
*/ | ||
export async function login(email, password) { | ||
await directus.auth.login({ email, password }); | ||
|
||
// Update stores | ||
const user = await directus.users.me.read(); | ||
AuthStore.update((s) => { | ||
s.user = user; | ||
}); | ||
const language = user.language?.split(/[-_]/)[0]; | ||
if (language in locales) { | ||
LocaleStore.update((s) => { | ||
s.locale = language; | ||
}); | ||
} | ||
} | ||
|
||
let websocket; | ||
let websocketReadyResolver; | ||
let websocketReady; | ||
const websocketSubscriptions = new Map(); | ||
|
||
async function receiveMessage(message) { | ||
const data = JSON.parse(message.data); | ||
|
||
if (data.type === 'ping') { | ||
websocket.send( | ||
JSON.stringify({ | ||
type: 'pong', | ||
}), | ||
); | ||
return; | ||
} | ||
|
||
if (data.type === 'auth' && data.status === 'ok') { | ||
websocketReadyResolver(websocket); | ||
return; | ||
} | ||
|
||
if ( | ||
data.type === 'auth' && | ||
data.status === 'error' && | ||
data.error.code === 'TOKEN_EXPIRED' | ||
) { | ||
websocketReady = new Promise((r) => { | ||
websocketReadyResolver = r; | ||
}); | ||
websocket.send( | ||
JSON.stringify({ | ||
type: 'auth', | ||
access_token: await getToken(), | ||
}), | ||
); | ||
return; | ||
} | ||
|
||
if (data.type === 'subscription' && data.uid && data.event !== 'init') { | ||
const callback = websocketSubscriptions.get(data.uid); | ||
callback?.(data.data); | ||
} | ||
} | ||
|
||
export async function getWebsocket() { | ||
if (!websocket) { | ||
websocket = new WebSocket(env('WS_URL')); | ||
websocket.addEventListener('open', async () => | ||
websocket.send( | ||
JSON.stringify({ | ||
type: 'auth', | ||
access_token: await getToken(), | ||
}), | ||
), | ||
); | ||
websocketReady = new Promise((r) => { | ||
websocketReadyResolver = r; | ||
}); | ||
websocket.addEventListener('message', (message) => receiveMessage(message)); | ||
} | ||
return websocketReady; | ||
} | ||
|
||
export async function subscribe(uid, options, callback) { | ||
const websocket = await getWebsocket(); | ||
|
||
if (websocketSubscriptions.has(uid)) { | ||
return; | ||
} | ||
|
||
websocketSubscriptions.set(uid, callback); | ||
|
||
websocket.send( | ||
JSON.stringify({ | ||
uid, | ||
type: 'subscribe', | ||
collection: options.collection, | ||
event: options.event, | ||
query: options.query, | ||
}), | ||
); | ||
} | ||
|
||
export function unsubscribe(uid) { | ||
websocket.send( | ||
JSON.stringify({ | ||
uid, | ||
type: 'unsubscribe', | ||
}), | ||
); | ||
|
||
websocketSubscriptions.delete(uid); | ||
} |
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
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
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