add persistent auth session
This commit is contained in:
parent
bf7c0a8445
commit
a7253ebf40
8 changed files with 69 additions and 66 deletions
|
|
@ -2,12 +2,12 @@ import { reactive, toRefs } from 'vue';
|
|||
import { useIpc } from './ipc';
|
||||
|
||||
interface AuthState {
|
||||
accessToken?: string | null;
|
||||
accessToken: string | null;
|
||||
error?: Error;
|
||||
}
|
||||
|
||||
const state = reactive<AuthState>({
|
||||
accessToken: undefined,
|
||||
accessToken: null,
|
||||
error: undefined,
|
||||
});
|
||||
|
||||
|
|
@ -16,7 +16,7 @@ const AUTH_KEY = 'crimata_token';
|
|||
const token = window.localStorage.getItem(AUTH_KEY);
|
||||
|
||||
const authToken = async () => {
|
||||
const { data, invoke } = useIpc('auth-user');
|
||||
const { data, invoke } = useIpc('auth-session');
|
||||
try {
|
||||
await invoke(token);
|
||||
state.accessToken = data.value;
|
||||
|
|
@ -28,29 +28,26 @@ const authToken = async () => {
|
|||
}
|
||||
}
|
||||
|
||||
// authenticate on socket connect
|
||||
window.ipcRenderer.on("fetch-token", async (_event, _arg) => {
|
||||
authToken();
|
||||
// authenticate on auth event
|
||||
window.ipcRenderer.on("auth", async (_event, _arg) => {
|
||||
await authToken();
|
||||
});
|
||||
|
||||
if (token) {
|
||||
authToken();
|
||||
state.accessToken = token;
|
||||
}
|
||||
|
||||
export const useAuth = () => {
|
||||
const setToken = (token: string, remember: boolean) => {
|
||||
if (remember) {
|
||||
// Save
|
||||
window.localStorage.setItem(AUTH_KEY, token);
|
||||
}
|
||||
|
||||
const setToken = (token: string) => {
|
||||
window.localStorage.setItem(AUTH_KEY, token);
|
||||
state.accessToken = token;
|
||||
state.error = undefined;
|
||||
}
|
||||
|
||||
const logout = (): Promise<void> => {
|
||||
const logout = (): Promise<null> => {
|
||||
window.localStorage.removeItem(AUTH_KEY);
|
||||
return Promise.resolve(state.accessToken = undefined);
|
||||
return Promise.resolve(state.accessToken = null);
|
||||
}
|
||||
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -1,36 +1,18 @@
|
|||
import { ref, inject } from "vue";
|
||||
import { ref } from "vue";
|
||||
import keyboardNameMap from "./keyBoardMaps/keyboardNameMap";
|
||||
import keyboardCharMap from "./keyBoardMaps/keyboardCharMap";
|
||||
// This catches keys from a physical keyboard, a soft keyboard on a tablet, or a
|
||||
// scanner attached via USB
|
||||
export default function useKeyDownHandler() {
|
||||
interface KeyDownEvent {
|
||||
|
||||
interface KeyDownEvent {
|
||||
keyCode: number;
|
||||
shiftKey: boolean;
|
||||
}
|
||||
}
|
||||
|
||||
// This catches keys from a physical keyboard, a soft keyboard on a tablet, or a
|
||||
// scanner attached via USB
|
||||
export default function useKeyDownHandler(enterCallback: (input: string) => void | null) {
|
||||
|
||||
const emitter: any = inject("mitt");
|
||||
const input = ref("");
|
||||
|
||||
// submits newSelfMessage event
|
||||
function handleEnterKey() {
|
||||
// does nothing if input value is empty
|
||||
if (input.value === "") {
|
||||
return;
|
||||
}
|
||||
const message = {
|
||||
text: input.value,
|
||||
audio: 0
|
||||
};
|
||||
// fire event with message payload
|
||||
emitter.emit("newSelfMessage", message);
|
||||
|
||||
window.postMessage({
|
||||
myTypeField: 'send-message',
|
||||
message: message
|
||||
}, '*')
|
||||
}
|
||||
|
||||
function keyDownHandler(e: KeyDownEvent) {
|
||||
// keyboardCharMap is an array of arrays, with each inner
|
||||
// array having 2 columns - un-shifted, and shifted values.
|
||||
|
|
@ -49,7 +31,7 @@ export default function useKeyDownHandler() {
|
|||
const cmd = keyboardNameMap[e.keyCode];
|
||||
switch (cmd) {
|
||||
case "ENTER":
|
||||
handleEnterKey();
|
||||
enterCallback(input.value);
|
||||
break;
|
||||
case "BACK_SPACE":
|
||||
input.value = input.value.slice(0, -1);
|
||||
|
|
@ -66,4 +48,4 @@ export default function useKeyDownHandler() {
|
|||
keyDownHandler,
|
||||
input
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue