add login and auth to socket
This commit is contained in:
parent
5a72970f34
commit
7cb14bc850
7 changed files with 75 additions and 42 deletions
|
|
@ -1,20 +0,0 @@
|
||||||
|
|
||||||
export const authUser = (event: any, arg: any) => {
|
|
||||||
let reply;
|
|
||||||
reply = {
|
|
||||||
id: "2",
|
|
||||||
email: "hernandeze@xavier.edu",
|
|
||||||
firstName: "Enrique",
|
|
||||||
lastName: "Hernandez",
|
|
||||||
access_token: "test-token"
|
|
||||||
};
|
|
||||||
if (arg === ['undefined']) {
|
|
||||||
reply = new Error('AUTH-FAILED');
|
|
||||||
}
|
|
||||||
event.reply('auth-user-reply', reply);
|
|
||||||
}
|
|
||||||
|
|
||||||
export const authLogin = (event: any, arg: any) => {
|
|
||||||
console.log('testing login', arg);
|
|
||||||
event.reply('auth-login-reply', true);
|
|
||||||
}
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
import { createWindow } from './createWindow';
|
import { createWindow } from './createWindow';
|
||||||
import { ipcMain } from "electron";
|
import { ipcMain } from "electron";
|
||||||
import { initSession, sendMessage} from './session';
|
import { initSession, sendMessage, authUser, authLogin} from './session';
|
||||||
import { windowEmitter } from './windowEmitter';
|
import { windowEmitter } from './windowEmitter';
|
||||||
import { authUser, authLogin } from './auth';
|
|
||||||
const portAudio = require('naudiodon');
|
const portAudio = require('naudiodon');
|
||||||
|
|
||||||
interface ipcRendererPayload {
|
interface ipcRendererPayload {
|
||||||
|
|
@ -24,21 +23,20 @@ export function main(): void {
|
||||||
// create main window.
|
// create main window.
|
||||||
const win = createWindow({ width: 350, height: 525, resizable: false });
|
const win = createWindow({ width: 350, height: 525, resizable: false });
|
||||||
|
|
||||||
|
|
||||||
windowEmitter.on('ipc-renderer', (payload: ipcRendererPayload) => {
|
windowEmitter.on('ipc-renderer', (payload: ipcRendererPayload) => {
|
||||||
win.webContents.send(payload.endpoint, {
|
win.webContents.send(payload.endpoint, {
|
||||||
message: payload.message
|
message: payload.message
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleIpcRender = (event: any, arg: any) => {
|
const handleMessage = (event: any, arg: any) => {
|
||||||
sendMessage(arg)
|
sendMessage(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
const windowMount = (): void => {
|
const windowMount = (): void => {
|
||||||
windowEmitter.emit('window-active', true);
|
windowEmitter.emit('window-active', true);
|
||||||
// ipc event handlers
|
// ipc event handlers
|
||||||
ipcMain.on('send-message', handleIpcRender);
|
ipcMain.on('send-message', handleMessage);
|
||||||
ipcMain.on('auth-login', authLogin);
|
ipcMain.on('auth-login', authLogin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,57 @@
|
||||||
import WebSocket from 'ws';
|
import WebSocket from 'ws';
|
||||||
import { windowEmitter } from './windowEmitter';
|
import { windowEmitter } from './windowEmitter';
|
||||||
|
|
||||||
|
type Message = {
|
||||||
|
type:string;
|
||||||
|
id: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RenderMessage extends Message {
|
||||||
|
content: string;
|
||||||
|
context: string;
|
||||||
|
subContext: string;
|
||||||
|
modifiers: string;
|
||||||
|
time: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AuthMessage extends Message {
|
||||||
|
email: string;
|
||||||
|
firstName: string;
|
||||||
|
lastName: string;
|
||||||
|
access_token: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AuthRequest {
|
||||||
|
token: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PostLogin {
|
||||||
|
email: string;
|
||||||
|
password: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PostMessage {
|
||||||
|
content: string;
|
||||||
|
}
|
||||||
|
|
||||||
const ip = 'ws://localhost';
|
const ip = 'ws://localhost';
|
||||||
const port = 8080;
|
const port = 8080;
|
||||||
const username = 'hernandeze2@xavier.edu';
|
const username = 'hernandeze2@xavier.edu';
|
||||||
|
let socket: WebSocket;
|
||||||
|
|
||||||
const receiveMessage = (message: string): void => {
|
const receiveMessage = (message: string): void => {
|
||||||
// NOTE assuming message is JSON string
|
// NOTE assuming message is JSON string
|
||||||
const parsed = JSON.parse(message);
|
const parsed: RenderMessage | AuthMessage = JSON.parse(message);
|
||||||
|
if (parsed.type === "render") {
|
||||||
windowEmitter.emit('ipc-renderer', {
|
windowEmitter.emit('ipc-renderer', {
|
||||||
endpoint: 'render-message',
|
endpoint: 'render-message',
|
||||||
message: parsed
|
message: parsed
|
||||||
});
|
});
|
||||||
|
} else if (parsed.type === "auth") {
|
||||||
|
windowEmitter.emit('auth-resp', parsed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let socket: WebSocket;
|
|
||||||
|
|
||||||
export function initSession() {
|
export function initSession() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
@ -33,7 +69,23 @@ export function initSession() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const sendMessage = (content: string | Buffer) => {
|
export const authUser = (event: any, arg: AuthRequest) => {
|
||||||
|
sendMessage({
|
||||||
|
content: arg.token
|
||||||
|
});
|
||||||
|
windowEmitter.on('auth-resp', (payload: AuthMessage) => {
|
||||||
|
event.reply('auth-user-reply', payload);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const authLogin = (event: any, arg: PostLogin) => {
|
||||||
|
sendMessage(arg)
|
||||||
|
windowEmitter.on('auth-resp', (payload: AuthMessage) => {
|
||||||
|
event.reply('auth-login-reply', payload);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const sendMessage = (content: PostMessage | Buffer | PostLogin) => {
|
||||||
if (content instanceof Buffer) {
|
if (content instanceof Buffer) {
|
||||||
socket.send(content);
|
socket.send(content);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -3,3 +3,9 @@ const EventEmitter = require('events');
|
||||||
class WindowEmitter extends EventEmitter {}
|
class WindowEmitter extends EventEmitter {}
|
||||||
|
|
||||||
export const windowEmitter = new WindowEmitter();
|
export const windowEmitter = new WindowEmitter();
|
||||||
|
|
||||||
|
const ipcRend = (endpoint: string, message: any) => {
|
||||||
|
win.webContents.send(endpoint, {
|
||||||
|
message: message
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,9 @@ if (token) {
|
||||||
state.authenticating = true;
|
state.authenticating = true;
|
||||||
|
|
||||||
// authenticate token against crimata-platform
|
// authenticate token against crimata-platform
|
||||||
send([token]);
|
send({
|
||||||
|
token: token
|
||||||
|
});
|
||||||
|
|
||||||
watch([loading], () => {
|
watch([loading], () => {
|
||||||
if (error.value) {
|
if (error.value) {
|
||||||
|
|
|
||||||
|
|
@ -22,9 +22,6 @@ export const useIpc = (endpoint: string) => {
|
||||||
error.value = arg;
|
error.value = arg;
|
||||||
throw arg;
|
throw arg;
|
||||||
} else {
|
} else {
|
||||||
if (callback) {
|
|
||||||
callback(arg);
|
|
||||||
}
|
|
||||||
data.value = arg;
|
data.value = arg;
|
||||||
loading.value = false;
|
loading.value = false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -50,13 +50,11 @@ export default defineComponent({
|
||||||
password: payload.password,
|
password: payload.password,
|
||||||
});
|
});
|
||||||
console.log(data.value, "login");
|
console.log(data.value, "login");
|
||||||
// setUser(data.value, payload.rememberMe);
|
|
||||||
// router.push({ name: "home" });
|
|
||||||
};
|
};
|
||||||
|
|
||||||
watch(loading, () => {
|
watch(loading, () => {
|
||||||
console.log("loading changed");
|
setUser(data.value, payload.rememberMe);
|
||||||
console.log(data.value, "post laoding");
|
router.push({ name: "home" });
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue