update sockets, account, auth
This commit is contained in:
parent
9e8641b4de
commit
d851db2fcc
20 changed files with 411 additions and 326 deletions
|
|
@ -1,126 +1,32 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
import { submit, fetchProfile, logout } from "../api/account";
|
||||
import { ipcMain, IpcMainInvokeEvent } from "electron";
|
||||
import { store } from "@/composables/store";
|
||||
import { accountLogin, accountLogout } from "@/account";
|
||||
import {IpcHandler} from "@/composables/ipcHandler";
|
||||
|
||||
const LOGIN_CHANNEL = "account-login";
|
||||
const LOGOUT_CHANNEL = "account-logout";
|
||||
|
||||
const parseAuthRes = (authRes: any) => {
|
||||
const token = authRes.headers['set-cookie'][0].split(";")[0].split("=")[1] as string;
|
||||
const profile = authRes.data as Profile;
|
||||
return {
|
||||
token,
|
||||
profile
|
||||
}
|
||||
};
|
||||
const loginHandler = new IpcHandler({
|
||||
channel: LOGIN_CHANNEL,
|
||||
handlerCallback: accountLogin
|
||||
});
|
||||
|
||||
const logoutHandler = new IpcHandler({
|
||||
channel: LOGOUT_CHANNEL,
|
||||
handlerCallback: accountLogout
|
||||
});
|
||||
|
||||
const handlers = [loginHandler, logoutHandler];
|
||||
|
||||
export default handlers;
|
||||
|
||||
|
||||
/**
|
||||
* Get user profile from store and try to login with it.
|
||||
*/
|
||||
const onTokenLogin = async (
|
||||
_event: IpcMainInvokeEvent,
|
||||
_payload: null
|
||||
): Promise<Profile | Error> => (
|
||||
|
||||
new Promise(async (resolve, reject) => {
|
||||
console.log('[IPC]: user-profile');
|
||||
|
||||
// get jwt token and crimataId from store
|
||||
const token = store.get('key');
|
||||
const crimataId = store.get('crimataId');
|
||||
|
||||
// authenticate and fetch profile
|
||||
try {
|
||||
|
||||
// attempt login with email token
|
||||
const res = await fetchProfile(crimataId, token);
|
||||
const parsed = parseAuthRes(res);
|
||||
|
||||
// return profile to renderer
|
||||
resolve(parsed.profile);
|
||||
|
||||
} catch(e) {
|
||||
reject(new Error('Failed to fetch profile.'));
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
|
||||
const onLogin = async (
|
||||
_event: IpcMainInvokeEvent,
|
||||
payload: string
|
||||
): Promise<Profile | Error> => (
|
||||
|
||||
new Promise(async (resolve, reject) => {
|
||||
console.log('[IPC]: user-login');
|
||||
|
||||
const account = JSON.parse(payload);
|
||||
|
||||
if ( account.password && account.email ) {
|
||||
try {
|
||||
|
||||
// attempt login with email password
|
||||
const res = await submit(account.email, account.password);
|
||||
const parsed = parseAuthRes(res);
|
||||
|
||||
// save jwt token and profile
|
||||
store.set('key', parsed.token);
|
||||
store.set('crimataId', parsed.profile.crimataId);
|
||||
|
||||
// init session
|
||||
|
||||
// return profile to renderer
|
||||
resolve(parsed.profile);
|
||||
|
||||
} catch(e) {
|
||||
console.log('[API]', e);
|
||||
reject(new Error('Failed to authenticate'));
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
const onLogout = async (
|
||||
_event: IpcMainInvokeEvent,
|
||||
_payload: null
|
||||
): Promise<void> => (
|
||||
|
||||
new Promise(async (resolve, reject) => {
|
||||
console.log('[IPC]: user-logout');
|
||||
|
||||
try {
|
||||
// post logout to backend
|
||||
await logout();
|
||||
|
||||
// remove key and crimataId
|
||||
store.delete('key');
|
||||
store.delete('crimataId');
|
||||
|
||||
// TODO: kill crimata platform session
|
||||
// endSession();
|
||||
|
||||
resolve();
|
||||
} catch(e) {
|
||||
reject(new Error('Failed to logout. Please try again.'));
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
|
||||
export default function useAccountListeners(): void {
|
||||
|
||||
ipcMain.removeHandler("user-profile");
|
||||
ipcMain.handle("user-profile", onProfile);
|
||||
|
||||
ipcMain.removeHandler("user-login");
|
||||
ipcMain.handle("user-login", onLogin);
|
||||
|
||||
ipcMain.removeHandler("user-logout");
|
||||
ipcMain.handle("user-logout", onLogout);
|
||||
|
||||
}
|
||||
// export default function useAccountListeners(): void {
|
||||
//
|
||||
// ipcMain.removeHandler(LOGIN_HANDLER);
|
||||
// ipcMain.handle(LOGIN_HANDLER, onLogin);
|
||||
//
|
||||
// ipcMain.removeHandler(LOGOUT_HANDLER);
|
||||
// ipcMain.handle(LOGOUT_HANDLER, onLogout);
|
||||
//
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -1,17 +1,36 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
import useAccountListeners from "./account";
|
||||
import { IpcHandler } from "@/composables/ipcHandler";
|
||||
import handlers from "./account";
|
||||
import useSessionListeners from "./session";
|
||||
// import useAudioListeners from "./audio";
|
||||
|
||||
interface IPCHandlers {
|
||||
[channel: string]: IpcHandler<any, any>;
|
||||
}
|
||||
|
||||
const ipcHandlers: IPCHandlers = {};
|
||||
|
||||
const _initHandlers = () => {
|
||||
handlers.forEach((h) => {
|
||||
if (!(h.channel in ipcHandlers)) {
|
||||
ipcHandlers[h.channel] = h;
|
||||
h.handle();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export default function useIpc(): void {
|
||||
|
||||
useAccountListeners();
|
||||
_initHandlers();
|
||||
|
||||
// useAccountListeners();
|
||||
|
||||
useSessionListeners();
|
||||
|
||||
// useAudioListeners();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -10,11 +10,11 @@ function onSendMessage(_event: IpcMainEvent, payload: Message): void {
|
|||
}
|
||||
|
||||
// Login attempt, returns success or not.
|
||||
function onLogin(_event: IpcMainEvent, payload: LoginPayload) => {
|
||||
authenticate(payload.email, payload.password);
|
||||
function onLogin(_event: IpcMainEvent, payload: LoginPayload): void {
|
||||
console.log('hello')
|
||||
}
|
||||
|
||||
export default function useSessionListeners(): void {
|
||||
ipcMain.removeAllListeners("client-message");
|
||||
ipcMain.on("client-message", onSendMessage);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue