add authenticate on socket connect
This commit is contained in:
parent
4a9e19bb43
commit
e0da50eb27
7 changed files with 112 additions and 70 deletions
|
|
@ -24,10 +24,23 @@ const state = reactive<AuthState>({
|
|||
const AUTH_KEY = 'crimata_token';
|
||||
|
||||
const token = window.localStorage.getItem(AUTH_KEY);
|
||||
console.log('token: ',token)
|
||||
|
||||
// authenticate on socket connect
|
||||
window.ipcRenderer.on("fetch-token", async (event, payload) => {
|
||||
const { data, invoke } = useIpc('auth-token');
|
||||
try {
|
||||
await invoke(token);
|
||||
state.user = data.value;
|
||||
}
|
||||
catch(e) {
|
||||
state.error = e;
|
||||
console.log('ERROR: failed authentication');
|
||||
window.localStorage.removeItem(AUTH_KEY);
|
||||
}
|
||||
});
|
||||
|
||||
if (token) {
|
||||
const { loading, error, data, send } = useIpc('auth-user');
|
||||
const { loading, error, data, send, invoke } = useIpc('auth-user');
|
||||
|
||||
state.authenticating = true;
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,20 @@ export const useIpc = (endpoint: string) => {
|
|||
}
|
||||
})
|
||||
|
||||
const invoke = (payload: any) => {
|
||||
loading.value = true;
|
||||
error.value = undefined;
|
||||
|
||||
return window.ipcRenderer.invoke(endpoint, payload).then(res => {
|
||||
data.value = res;
|
||||
}).catch(e => {
|
||||
error.value = e;
|
||||
throw e;
|
||||
}).finally(() => {
|
||||
loading.value = false;
|
||||
})
|
||||
}
|
||||
|
||||
const send = (payload?: Record<string, any>, callback?: (arg: any) => void) => {
|
||||
loading.value = true;
|
||||
error.value = undefined;
|
||||
|
|
@ -46,6 +60,7 @@ export const useIpc = (endpoint: string) => {
|
|||
data,
|
||||
error,
|
||||
errorMessage,
|
||||
send
|
||||
send,
|
||||
invoke
|
||||
}
|
||||
}
|
||||
|
|
|
|||
17
src/modules/socket.ts
Normal file
17
src/modules/socket.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
interface SocketState {
|
||||
connection: WebSocket | null;
|
||||
auth: boolean;
|
||||
}
|
||||
|
||||
// Create WebSocket connection.
|
||||
const socket = new WebSocket('ws://localhost:8080');
|
||||
|
||||
// Connection opened
|
||||
socket.onopen = function (event) {
|
||||
socket.send('Hello Server!');
|
||||
}
|
||||
|
||||
socket.onmessage = function(event) {
|
||||
console.debug("WebSocket message received:", event);
|
||||
}
|
||||
|
||||
Loading…
Reference in a new issue