]> Repos - mime-chat/commitdiff
add auth login error handling
authorEnrique Hernandez <hernandeze2@xavier.edu>
Tue, 9 Feb 2021 05:00:14 +0000 (23:00 -0600)
committerEnrique Hernandez <hernandeze2@xavier.edu>
Tue, 9 Feb 2021 05:13:43 +0000 (23:13 -0600)
src/background/session.ts
src/modules/auth.ts
src/views/login.vue

index 1b508052c0d4bb8eb6fd927ce7a1986fae8f9097..edeb95a0a8a0586db05f2f8d4b03f69179b6ba0a 100644 (file)
@@ -2,7 +2,7 @@ import WebSocket from 'ws';
 import { windowEmitter } from './windowEmitter';
 
 type Message = {
-    type:string;
+    type: string;
     id: string;
 }
 
@@ -54,35 +54,50 @@ const receiveMessage = (message: string): void => {
 
 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) => {
index 65e590795ff34fd1b1def469dab58b362a77b6e6..16be95c6ab2e13ede2c594355656d5442335b08e 100644 (file)
@@ -24,6 +24,7 @@ const state = reactive<AuthState>({
 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');
@@ -31,11 +32,12 @@ if (token) {
     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);
@@ -46,6 +48,8 @@ if (token) {
 
         state.authenticating = false;
     })
+} else {
+    console.log('no token to send')
 }
 
 export const useAuth = () => {
index 2f60f7e55bc50dc564b66f8c010436138702550a..7150b5389ad0ee84a7a2a7f733bd6158d0e704fc 100644 (file)
@@ -53,6 +53,7 @@ export default defineComponent({
     };
 
     watch(loading, () => {
+      console.log(data.value)
       setUser(data.value, payload.rememberMe);
       router.push({ name: "home" });
     });