Skip to content

Commit

Permalink
fix: better microsoft auth
Browse files Browse the repository at this point in the history
  • Loading branch information
ResetPower committed Jan 28, 2022
1 parent c780da1 commit 0f0df48
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 14 deletions.
8 changes: 3 additions & 5 deletions src/core/launch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,10 @@ export async function launchMinecraft(
if (!validateMicrosoft(account.token)) {
const refreshed = await refreshMicrosoft(account);
if (!refreshed) {
coreLogger.warn(
"Unable to refresh microsoft account, please login again"
);
coreLogger.warn("Unable to refresh microsoft account token");
showOverlay({
title: "Warning",
message: "Unable to refresh microsoft account, please login again",
title: t("warning"),
message: t("unableToRefreshMsToken"),
});
}
}
Expand Down
27 changes: 20 additions & 7 deletions src/eph/components/layouts.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
import { DefaultFn } from "common/utils";
import { ReactNode } from "react";
import { MdError, MdInfo, MdWarning } from "react-icons/md";
import { MdCheck, MdClose, MdError, MdInfo, MdWarning } from "react-icons/md";

export function Alert(props: {
children: ReactNode;
severity: "info" | "warn" | "error";
className?: string;
severity: "info" | "warn" | "error" | "successful";
onClose?: DefaultFn;
}): JSX.Element {
return (
<div
className={`flex bg-opacity-60 text-white shadow-md rounded-md text-base p-3 ${
props.severity === "warn"
className={`flex items-center bg-opacity-60 text-white shadow-md rounded-md text-base p-3 ${
props.severity === "successful"
? "bg-green-500"
: props.severity === "warn"
? "bg-yellow-600"
: props.severity === "error"
? "bg-red-600"
: "bg-blue-600"
}`}
} ${props.className}`}
>
{props.severity === "warn" ? (
{props.severity === "successful" ? (
<MdCheck />
) : props.severity === "warn" ? (
<MdWarning />
) : props.severity === "error" ? (
<MdError />
) : (
<MdInfo />
)}
<p className="pl-3 text-white">{props.children}</p>
<p className="pl-3 text-white flex-grow">{props.children}</p>
{props.onClose && (
<MdClose
className="hover:opacity-80 active:opacity-60 cursor-pointer transition-colors"
onClick={props.onClose}
/>
)}
</div>
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/eph/intl/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,6 @@ export interface LanguageDefinition {
quit: string;
confirmQuitting: string;
removeFilesAsWell: string;
unableToRefreshMsToken: string;
msLoginAgain: string;
}
3 changes: 3 additions & 0 deletions src/eph/intl/en-us.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ const enUs: Language = {
quit: "Quit",
confirmQuitting: "Are you sure you want to quit Epherome?",
removeFilesAsWell: "Remove files as well",
unableToRefreshMsToken:
"Unable to refresh Microsoft account token, please login again.",
msLoginAgain: "Login Again",
},
};

Expand Down
3 changes: 3 additions & 0 deletions src/eph/intl/ja-jp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ const jaJp: Language = {
quit: "終了",
confirmQuitting: "Epherome を終了してもよろしいですか?",
removeFilesAsWell: "ファイルを削除",
unableToRefreshMsToken:
"Microsoft アカウントのトークンを更新できません。もう一度ログインしてください。",
msLoginAgain: "再度ログイン",
},
};

Expand Down
2 changes: 2 additions & 0 deletions src/eph/intl/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ const zhCn: Language = {
quit: "退出",
confirmQuitting: "你确定要退出 Epherome 吗?",
removeFilesAsWell: "删除文件",
unableToRefreshMsToken: "无法刷新微软账户令牌,请重新登录。",
msLoginAgain: "重新登录",
},
};

Expand Down
69 changes: 67 additions & 2 deletions src/eph/views/AccountsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
CreateAccountImplResult,
MinecraftAccount,
removeAccount,
updateAccountToken,
} from "common/struct/accounts";
import { configStore, setConfig } from "common/struct/config";
import { MdCreate, MdDelete, MdEdit } from "react-icons/md";
Expand All @@ -33,6 +34,11 @@ import { downloadSkin } from "core/model/skin";
import { BiExport } from "react-icons/bi";
import { commonLogger } from "common/loggers";
import { Avatar, Body } from "eph/components/skin";
import { ipcRenderer } from "electron";
import {
authCode2AuthToken,
authToken2MinecraftTokenDirectly,
} from "core/auth";

export function ChangeAccountFragment(props: {
onDone: DefaultFn;
Expand Down Expand Up @@ -134,6 +140,8 @@ export function ChangeAccountFragment(props: {
export function AccountGeneralFragment(props: {
current: MinecraftAccount;
}): JSX.Element {
const [stat, setStat] = useState<boolean | "err" | "succ">(false);
const onClose = () => setStat(false);
const handleRemove = (selected: MinecraftAccount) =>
showOverlay({
title: t("account.removing"),
Expand All @@ -144,15 +152,72 @@ export function AccountGeneralFragment(props: {
positiveText: t("remove"),
});

const handleLoginAgain = () => {
if (props.current.mode === "microsoft") {
setStat(true);
ipcRenderer.invoke("ms-auth").then(async (result) => {
let code = "";
const split = result.split("&");
for (const i of split) {
const j = i.split("=");
if (j[0] === "code") {
code = j[1];
}
}
if (code) {
const authTokenResult = await authCode2AuthToken(code);
const accessToken = authTokenResult.access_token;
const refreshToken = authTokenResult.refresh_token;
if (accessToken && refreshToken) {
const mcTokenResult = await authToken2MinecraftTokenDirectly(
accessToken
);
if (mcTokenResult.token) {
updateAccountToken(
props.current,
mcTokenResult.token,
authTokenResult.refresh_token
);
setStat("succ");
return;
}
}
setStat("err");
}
});
}
};

return (
<div className="flex flex-col">
{stat === "succ" && (
<Alert severity="successful" className="mb-3" onClose={onClose}>
{t("doneMessage")}
</Alert>
)}
{stat === "err" && (
<Alert severity="error" className="mb-3" onClose={onClose}>
{t("errorOccurred")}
</Alert>
)}
<div className="flex-grow">
<Info title={t("name")}>{props.current?.name}</Info>
<Info title={t("name")}>{props.current.name}</Info>
<p className="text-shallow text-sm">
{props.current && t(`account.${props.current.mode}`)}
</p>
</div>
<div className="flex justify-end">
<div className="flex mt-3">
{props.current.mode === "microsoft" && (
<Button
onClick={handleLoginAgain}
variant="contained"
disabled={stat === true}
>
{t("msLoginAgain")}
</Button>
)}
{stat === true && <Spin />}
<div className="flex-grow" />
<Button
className="text-danger"
onClick={() => props.current && handleRemove(props.current)}
Expand Down

0 comments on commit 0f0df48

Please sign in to comment.