add auth login error handling
This commit is contained in:
parent
7cb14bc850
commit
b36a961d80
3 changed files with 42 additions and 22 deletions
|
|
@ -2,7 +2,7 @@ import WebSocket from 'ws';
|
||||||
import { windowEmitter } from './windowEmitter';
|
import { windowEmitter } from './windowEmitter';
|
||||||
|
|
||||||
type Message = {
|
type Message = {
|
||||||
type:string;
|
type: string;
|
||||||
id: string;
|
id: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -54,35 +54,50 @@ const receiveMessage = (message: string): void => {
|
||||||
|
|
||||||
export function initSession() {
|
export function initSession() {
|
||||||
|
|
||||||
try {
|
let success = false;
|
||||||
socket = new WebSocket(`${ip}:${port}`);
|
socket = new WebSocket(`${ip}:${port}`);
|
||||||
socket.binaryType = 'arraybuffer';
|
socket.binaryType = 'arraybuffer';
|
||||||
|
|
||||||
socket.on('open', () => {
|
socket.on('open', () => {
|
||||||
socket.send(username);
|
console.log('Connected to crimata-platform');
|
||||||
})
|
socket.send(username);
|
||||||
|
success = true;
|
||||||
|
})
|
||||||
|
|
||||||
socket.on("message", receiveMessage);
|
socket.on("message", receiveMessage);
|
||||||
|
|
||||||
} catch(e) {
|
setTimeout(()=> {
|
||||||
console.log('error connecting socket', + e);
|
if (!success) {
|
||||||
}
|
throw new Error('Unable to connect to crimata-platform')
|
||||||
|
}
|
||||||
|
}, 3000)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const authUser = (event: any, arg: AuthRequest) => {
|
export const authUser = (event: any, arg: AuthRequest) => {
|
||||||
sendMessage({
|
console.log('sending token to socket', arg)
|
||||||
content: arg.token
|
try {
|
||||||
});
|
sendMessage({
|
||||||
windowEmitter.on('auth-resp', (payload: AuthMessage) => {
|
content: arg.token
|
||||||
event.reply('auth-user-reply', payload);
|
});
|
||||||
})
|
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) => {
|
export const authLogin = (event: any, arg: PostLogin) => {
|
||||||
sendMessage(arg)
|
try {
|
||||||
windowEmitter.on('auth-resp', (payload: AuthMessage) => {
|
sendMessage(arg)
|
||||||
event.reply('auth-login-reply', payload);
|
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) => {
|
export const sendMessage = (content: PostMessage | Buffer | PostLogin) => {
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ const state = reactive<AuthState>({
|
||||||
const AUTH_KEY = 'crimata_token';
|
const AUTH_KEY = 'crimata_token';
|
||||||
|
|
||||||
const token = window.localStorage.getItem(AUTH_KEY);
|
const token = window.localStorage.getItem(AUTH_KEY);
|
||||||
|
console.log('token: ',token)
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
const { loading, error, data, send } = useIpc('auth-user');
|
const { loading, error, data, send } = useIpc('auth-user');
|
||||||
|
|
@ -31,11 +32,12 @@ if (token) {
|
||||||
state.authenticating = true;
|
state.authenticating = true;
|
||||||
|
|
||||||
// authenticate token against crimata-platform
|
// authenticate token against crimata-platform
|
||||||
|
console.log('sending token to main')
|
||||||
send({
|
send({
|
||||||
token: token
|
token: token
|
||||||
});
|
});
|
||||||
|
|
||||||
watch([loading], () => {
|
watch(loading, () => {
|
||||||
if (error.value) {
|
if (error.value) {
|
||||||
console.log('ERROR: failed authentication')
|
console.log('ERROR: failed authentication')
|
||||||
window.localStorage.removeItem(AUTH_KEY);
|
window.localStorage.removeItem(AUTH_KEY);
|
||||||
|
|
@ -46,6 +48,8 @@ if (token) {
|
||||||
|
|
||||||
state.authenticating = false;
|
state.authenticating = false;
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
console.log('no token to send')
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useAuth = () => {
|
export const useAuth = () => {
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,7 @@ export default defineComponent({
|
||||||
};
|
};
|
||||||
|
|
||||||
watch(loading, () => {
|
watch(loading, () => {
|
||||||
|
console.log(data.value)
|
||||||
setUser(data.value, payload.rememberMe);
|
setUser(data.value, payload.rememberMe);
|
||||||
router.push({ name: "home" });
|
router.push({ name: "home" });
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue