Skip to content

Commit

Permalink
Support token acquisition API for account modification
Browse files Browse the repository at this point in the history
  • Loading branch information
vicr123 committed Sep 23, 2024
1 parent 4244b28 commit 8ec1036
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 44 deletions.
6 changes: 5 additions & 1 deletion Parlance.ClientApp/src/helpers/TokenAcquisitionSession.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ export class TokenAcquisitionSession {
try {
let response = await Fetch.post<TokenResponseToken>(
`/api/user/token`,
{ ...this.loginSessionDetails, username: this._username },
{
...this.loginSessionDetails,
username: this._username,
purpose: this.purpose,
},
);
Modal.unmount();
this._successFunction(response.token);
Expand Down
11 changes: 9 additions & 2 deletions Parlance.ClientApp/src/helpers/UserManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,21 @@ class UserManager extends EventEmitter {
return `https://www.gravatar.com/avatar/${md5}`;
}

obtainToken(username: string, purpose: TokenPurpose, prePassword: string) {
obtainToken(
username: string,
purpose: TokenPurpose,
prePassword: string = "",
) {
return new Promise<string>(async (res, rej) => {
const acquisitionSession = new TokenAcquisitionSession(
username,
purpose,
prePassword,
res,
rej,
() => {
Modal.unmount();
rej();
},
);
Modal.mount(<LoadingModal />);
await acquisitionSession.loadLoginTypes();
Expand Down
17 changes: 10 additions & 7 deletions Parlance.ClientApp/src/pages/Account/EmailChange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ export default function EmailChange() {
const navigate = useNavigate();
const { t } = useTranslation();

const performEmailChange = () => {
const performEmailChange = async () => {
if (newEmail === "") return;

const accept = async (password: string) => {
//Perform the username change
try {
const token = await UserManager.obtainToken(
UserManager.currentUser?.username!,
"accountModification",
);

//Perform the email change
Modal.mount(<LoadingModal />);
try {
await Fetch.post("/api/user/email", {
newEmail: newEmail,
password: password,
password: token,
});
await UserManager.updateDetails();
navigate("..");
Expand All @@ -49,9 +54,7 @@ export default function EmailChange() {
</Modal>,
);
}
};

Modal.mount(<PasswordConfirmModal onAccepted={accept} />);
} catch {}
};

return (
Expand Down
21 changes: 10 additions & 11 deletions Parlance.ClientApp/src/pages/Account/Otp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
OtpStateEnabled,
} from "@/interfaces/users";
import { ServerInformationContext } from "@/context/ServerInformationContext";
import UserManager from "@/helpers/UserManager";

function OtpBlock({ otpState }: { otpState: OtpStateEnabled }) {
return (
Expand Down Expand Up @@ -354,19 +355,17 @@ export default function Otp() {
const navigate = useNavigate();
const { t } = useTranslation();

const requestPassword = () => {
const accept = (password: string) => {
setPassword(password);
};

const reject = () => {
const requestPassword = async () => {
try {
const token = await UserManager.obtainToken(
UserManager.currentUser?.username!,
"accountModification",
);
setPassword(token);
} catch {
navigate("..");
Modal.unmount();
};

Modal.mount(
<PasswordConfirmModal onAccepted={accept} onRejected={reject} />,
);
}
};

const updateState = async () => {
Expand Down
17 changes: 11 additions & 6 deletions Parlance.ClientApp/src/pages/Account/PasswordChange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function PasswordChange() {
const navigate = useNavigate();
const { t } = useTranslation();

const performPasswordChange = () => {
const performPasswordChange = async () => {
if (newPassword === "") return;

if (newPassword !== newPasswordConfirm) {
Expand All @@ -34,13 +34,18 @@ export default function PasswordChange() {
return;
}

const accept = async (password: string) => {
try {
const token = await UserManager.obtainToken(
UserManager.currentUser?.username!,
"accountModification",
);

//Perform the username change
Modal.mount(<LoadingModal />);
try {
await Fetch.post("/api/user/password", {
newPassword: newPassword,
password: password,
password: token,
});
await UserManager.updateDetails();
navigate("..");
Expand All @@ -62,9 +67,9 @@ export default function PasswordChange() {
</Modal>,
);
}
};

Modal.mount(<PasswordConfirmModal onAccepted={accept} />);
} catch {
// Do nothing
}
};

return (
Expand Down
21 changes: 10 additions & 11 deletions Parlance.ClientApp/src/pages/Account/SecurityKeys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import RegisterSecurityKeyModal from "../../components/modals/account/securityKe
import Styles from "./SecurityKeys.module.css";
import { ServerInformationContext } from "@/context/ServerInformationContext.js";
import { SecurityKey } from "../../interfaces/users";
import UserManager from "@/helpers/UserManager";

function KeyList({
keys,
Expand Down Expand Up @@ -208,19 +209,17 @@ export default function SecurityKeys() {
const navigate = useNavigate();
const { t } = useTranslation();

const requestPassword = () => {
const accept = (password: string) => {
setPassword(password);
};

const reject = () => {
const requestPassword = async () => {
try {
const token = await UserManager.obtainToken(
UserManager.currentUser?.username!,
"accountModification",
);
setPassword(token);
} catch {
navigate("..");
Modal.unmount();
};

Modal.mount(
<PasswordConfirmModal onAccepted={accept} onRejected={reject} />,
);
}
};

const updateState = async () => {
Expand Down
17 changes: 11 additions & 6 deletions Parlance.ClientApp/src/pages/Account/UsernameChange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ export default function UsernameChange() {
const navigate = useNavigate();
const { t } = useTranslation();

const performUsernameChange = () => {
const performUsernameChange = async () => {
if (newUsername === "") return;

const accept = async (password: string) => {
try {
const token = await UserManager.obtainToken(
UserManager.currentUser?.username!,
"accountModification",
);

//Perform the username change
Modal.mount(<LoadingModal />);
try {
await Fetch.post("/api/user/username", {
newUsername: newUsername,
password: password,
password: token,
});
await UserManager.updateDetails();
navigate("..");
Expand All @@ -49,9 +54,9 @@ export default function UsernameChange() {
</Modal>,
);
}
};

Modal.mount(<PasswordConfirmModal onAccepted={accept} />);
} catch {
// Do nothing
}
};

return (
Expand Down

0 comments on commit 8ec1036

Please sign in to comment.