upgrading auth functionality
This commit is contained in:
parent
4b3023fd4c
commit
9bf7496899
12 changed files with 92 additions and 362 deletions
|
|
@ -21,7 +21,7 @@ if (token) {
|
|||
window.localStorage.setItem(AUTH_KEY, token);
|
||||
}
|
||||
|
||||
// Load key and invoke onAuthSession to try key-login.
|
||||
// Token authentication.
|
||||
const authToken = async () => {
|
||||
console.log("Calling auth token!!")
|
||||
const { invoke } = useIpc();
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ let socket: WebSocket;
|
|||
|
||||
|
||||
// Run every time we want to connect to backend.
|
||||
export default function useWebSockets(receiveCallback: (s: Buffer) => any) {
|
||||
export default function useWebSockets(receiveCallback: (s: string) => any) {
|
||||
|
||||
const sendMessage = (data: string) => {
|
||||
|
||||
|
|
@ -27,14 +27,15 @@ export default function useWebSockets(receiveCallback: (s: Buffer) => any) {
|
|||
|
||||
console.log('Message received: ', event);
|
||||
|
||||
receiveCallback(event.data)
|
||||
receiveCallback(event.data.toString())
|
||||
|
||||
}
|
||||
|
||||
const onClose = (event: WebSocket.CloseEvent) => {
|
||||
console.log("socket closed normally.")
|
||||
console.log("Socket closed normally.")
|
||||
}
|
||||
|
||||
// Reconnect automatically on error.
|
||||
const onError = (event: WebSocket.ErrorEvent) => {
|
||||
console.log('WebSocket error: ', event);
|
||||
|
||||
|
|
@ -55,76 +56,8 @@ export default function useWebSockets(receiveCallback: (s: Buffer) => any) {
|
|||
}
|
||||
|
||||
return {
|
||||
createSocket
|
||||
createSocket,
|
||||
sendMessage
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Close existing sockets.
|
||||
if (socket) {
|
||||
socket.removeAllListeners();
|
||||
socket.terminate();
|
||||
socket.close();
|
||||
success = false;
|
||||
}
|
||||
|
||||
// Init new socket.
|
||||
socket = new WebSocket(`ws://127.0.0.1:8760`);
|
||||
socket.binaryType = 'arraybuffer';
|
||||
|
||||
// Handle requests for authentication (promise).
|
||||
ipcMain.removeHandler('auth-session'); // avoid setting duplicate handlers
|
||||
ipcMain.handle('auth-session', onAuthSession);
|
||||
|
||||
// handle user message event
|
||||
ipcMain.removeAllListeners('client-message');
|
||||
ipcMain.on('client-message', onClientMessage);
|
||||
|
||||
// handle renderer logout event
|
||||
ipcMain.removeAllListeners('logout');
|
||||
ipcMain.on('logout', (_event, _payload: string) => restart());
|
||||
|
||||
// Connect to the backend.
|
||||
socket.on('open', () => {
|
||||
console.log('Connected to Crimata Servers.');
|
||||
|
||||
// Try to authenticate token right away.
|
||||
backgroundMitt.emit('ipc-renderer', {
|
||||
endpoint: 'auth',
|
||||
message: null
|
||||
});
|
||||
|
||||
success = true;
|
||||
|
||||
});
|
||||
|
||||
// Error handling.
|
||||
socket.on('error', (_e) => {
|
||||
|
||||
console.log('ERROR: Failed to connect.');
|
||||
socket.removeAllListeners();
|
||||
socket.close();
|
||||
|
||||
setTimeout(() => {
|
||||
if (!success) {
|
||||
console.log('Reconnecting...');
|
||||
initSession();
|
||||
}
|
||||
}, 3000); // reconnect timeout
|
||||
|
||||
});
|
||||
|
||||
socket.on('close', () => {
|
||||
console.log('Connection droped! Restarting...');
|
||||
restart();
|
||||
});
|
||||
|
||||
socket.on("message", onMessage);
|
||||
}
|
||||
Loading…
Reference in a new issue