Skip to content

Commit

Permalink
v8.1.8: improved auth code
Browse files Browse the repository at this point in the history
  • Loading branch information
caiiiycuk committed Jun 20, 2024
1 parent 10dd723 commit c94a70a
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 25 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
// url: "https://cdn.dos.zone/original/2X/b/b4b5275904d86a4ab8a20917b2b7e34f0df47bf7.jsdos", // dhry2
// url: "https://cdn.dos.zone/original/2X/7/744842062905f72648a4d492ccc2526d039b3702.jsdos", // sim-city
// dosboxConf: diabloConf,
// loginUrl: "http://localhost:8080/subscription.html",
pathPrefix: "/emulators/",
server: params.get("server"),
room: params.get("room"),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-dos",
"version": "8.1.7",
"version": "8.1.8",
"description": "Full-featured DOS player with multiple emulator backends",
"type": "module",
"keywords": [
Expand Down
4 changes: 2 additions & 2 deletions src/frame/account-frame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ function PremiumPlan(props: {}) {
return;
}

if (location.host !== "v8.js-dos.com") {
window.open("https://v8.js-dos.com/subscription", "_blank");
if (location.host !== "js-dos.com") {
window.open("https://js-dos.com/subscription.html", "_blank");
return;
}

Expand Down
40 changes: 31 additions & 9 deletions src/login/login.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { useEffect, useRef } from "preact/hooks";
import { Dispatch, useEffect, useRef } from "preact/hooks";
import { useDispatch, useSelector } from "react-redux";
import { State } from "../store";
import { uiSlice } from "../store/ui";
import { CloseButton } from "../components/close-button";
import { authentificator } from "../v8/config";
import { Account, authSlice, postAuthMessage } from "../store/auth";
import { Account, authSlice, getRefreshToken } from "../store/auth";
import { havePremium } from "../v8/subscriptions";
import { AnyAction } from "@reduxjs/toolkit";

export function Login() {
const iframeRef = useRef<HTMLIFrameElement>(null);
const activeAccount = useSelector((state: State) => state.auth.account);
const visible = useSelector((state: State) => state.ui.modal) === "login";
const loginUrl = useSelector((state: State) => state.auth.loginUrl);
const dispatch = useDispatch();

useEffect(() => {
Expand All @@ -28,7 +30,7 @@ export function Login() {

async function onAuthMessage(e: any) {
if (e.data.action === "auth/ready") {
postAuthMessage("auth/authenicate");
postAuthMessage("auth/authenicate", iframe as HTMLIFrameElement, dispatch);
} else if (e.data.action === "auth/authenicate") {
const account = e.data.account as Account | null;
if (account !== null) {
Expand All @@ -55,13 +57,15 @@ export function Login() {
}, []);

useEffect(() => {
const iframe = iframeRef.current;
if (iframe === null) {
return;
}
if (visible) {
const iframe = iframeRef.current;
if (iframe === null) {
return;
}

postAuthMessage("auth/login");
}, [visible]);
postAuthMessage("auth/login", iframe, dispatch, loginUrl);
}
}, [visible, loginUrl]);

return <div
id={"login"}
Expand All @@ -75,3 +79,21 @@ export function Login() {
/>
</div>;
}

function postAuthMessage(action: "auth/login" | "auth/authenicate",
iframe: HTMLIFrameElement,
dispatch: Dispatch<AnyAction>,
loginUrl?: string) {
const message = {
action,
refresh_token: action === "auth/authenicate" ? getRefreshToken() : undefined,
url: action === "auth/login" ? loginUrl : undefined,
};
if (!iframe || !iframe.contentWindow) {
console.error("Can't find authentificator iframe");
dispatch(authSlice.actions.logout({}));
dispatch(authSlice.actions.ready());
} else {
iframe.contentWindow.postMessage(message, "*");
}
}
5 changes: 5 additions & 0 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { loadBundleFromConfg, loadBundleFromUrl } from "./load";
import { DosOptions, DosProps, DosFn } from "./public/types";
import { browserSetFullScreen } from "./host/fullscreen";
import { NonSerializableStore, Store, makeNonSerializableStore, makeStore, postJsDosEvent } from "./store";
import { authSlice } from "./store/auth";

export const Dos: DosFn = (element: HTMLDivElement,
options: Partial<DosOptions> = {}): DosProps => {
Expand Down Expand Up @@ -154,6 +155,10 @@ export const Dos: DosFn = (element: HTMLDivElement,
setFullScreen(options.fullScreen);
}

if (options.loginUrl !== undefined) {
store.dispatch(authSlice.actions.setLoginUrl(options.loginUrl));
}

render(
<Provider store={store}>
{<Ui /> as any}
Expand Down
3 changes: 2 additions & 1 deletion src/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export interface DosOptions {
sockdriveBackend: {
name: string,
host: string,
}
},
loginUrl: string,
}

export interface DosProps {
Expand Down
17 changes: 6 additions & 11 deletions src/store/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ const initAccount = (() => {
})();

const initialState: {
loginUrl: string,
account: Account | null,
ready: boolean,
} = {
loginUrl: location.href,
account: initAccount,
ready: initAccount !== null,
};
Expand Down Expand Up @@ -84,20 +86,13 @@ export const authSlice = createSlice({
ready: (state) => {
state.ready = true;
},
setLoginUrl: (state, action: { payload: string }) => {
state.loginUrl = action.payload;
},
},
});

export function postAuthMessage(action: "auth/login" | "auth/authenicate") {
const message = {
action,
refresh_token: action === "auth/authenicate" ? getRefreshToken() : undefined,
url: action === "auth/login" ? location.href : undefined,
};
document.querySelector<HTMLIFrameElement>("#authentificator")!
.contentWindow?.postMessage(message, "*");
}

function getRefreshToken(): string | null {
export function getRefreshToken(): string | null {
return lStorage.getItem("cached.rt");
}

Expand Down
2 changes: 1 addition & 1 deletion src/ws/ws-transport-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class WsTransportLayer implements TransportLayer {
socket: WebSocket;
sessionId: string = Date.now() + "";
hardware: Hardware;
onInit: (version: number) => void = {/**/};
onInit: (version: number) => void = () => {/**/};

private cycles = 0;
private sockdrive = createSockdrive(this.onSockdriveOpen.bind(this),
Expand Down

0 comments on commit c94a70a

Please sign in to comment.