import { windowEmitter } from './windowEmitter';
type Message = {
- type:string;
+ type: string;
id: string;
}
export function initSession() {
- try {
- socket = new WebSocket(`${ip}:${port}`);
- socket.binaryType = 'arraybuffer';
+ let success = false;
+ socket = new WebSocket(`${ip}:${port}`);
+ socket.binaryType = 'arraybuffer';
- socket.on('open', () => {
- socket.send(username);
- })
+ socket.on('open', () => {
+ console.log('Connected to crimata-platform');
+ socket.send(username);
+ success = true;
+ })
- socket.on("message", receiveMessage);
+ socket.on("message", receiveMessage);
- } catch(e) {
- console.log('error connecting socket', + e);
- }
+ setTimeout(()=> {
+ if (!success) {
+ throw new Error('Unable to connect to crimata-platform')
+ }
+ }, 3000)
}
export const authUser = (event: any, arg: AuthRequest) => {
- sendMessage({
- content: arg.token
- });
- windowEmitter.on('auth-resp', (payload: AuthMessage) => {
- event.reply('auth-user-reply', payload);
- })
+ console.log('sending token to socket', arg)
+ try {
+ sendMessage({
+ content: arg.token
+ });
+ windowEmitter.on('auth-resp', (payload: AuthMessage) => {
+ event.reply('auth-user-reply', payload);
+ })
+ } catch(e){
+ console.log('Error authenticating user! ' + e);
+ event.reply('auth-user-reply', e);
+ }
}
export const authLogin = (event: any, arg: PostLogin) => {
- sendMessage(arg)
- windowEmitter.on('auth-resp', (payload: AuthMessage) => {
- event.reply('auth-login-reply', payload);
- })
+ try {
+ sendMessage(arg)
+ windowEmitter.on('auth-resp', (payload: AuthMessage) => {
+ event.reply('auth-login-reply', payload);
+ })
+ } catch(e) {
+ console.log('Error loging in' + e);
+ event.reply('auth-login-reply', e);
+ }
}
export const sendMessage = (content: PostMessage | Buffer | PostLogin) => {
const AUTH_KEY = 'crimata_token';
const token = window.localStorage.getItem(AUTH_KEY);
+console.log('token: ',token)
if (token) {
const { loading, error, data, send } = useIpc('auth-user');
state.authenticating = true;
// authenticate token against crimata-platform
+ console.log('sending token to main')
send({
token: token
});
- watch([loading], () => {
+ watch(loading, () => {
if (error.value) {
console.log('ERROR: failed authentication')
window.localStorage.removeItem(AUTH_KEY);
state.authenticating = false;
})
+} else {
+ console.log('no token to send')
}
export const useAuth = () => {