feat: remove auth method

This commit is contained in:
sam 2024-11-04 22:04:04 +01:00
parent 201c56c3dd
commit 9160281ea2
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
7 changed files with 144 additions and 16 deletions

View file

@ -142,6 +142,8 @@ export const errorCodeDesc = (t: TFunction, code: ErrorCode) => {
return t("error.errors.user-not-found");
case ErrorCode.AccountAlreadyLinked:
return t("error.errors.account-already-linked");
case ErrorCode.LastAuthMetod:
return t("error.errors.last-auth-method");
}
return t("error.errors.generic-error");

View file

@ -17,6 +17,7 @@ export enum ErrorCode {
UserNotFound = "USER_NOT_FOUND",
MemberNotFound = "MEMBER_NOT_FOUND",
AccountAlreadyLinked = "ACCOUNT_ALREADY_LINKED",
LastAuthMetod = "LAST_AUTH_METHOD",
}
export type ValidationError = {

View file

@ -0,0 +1,73 @@
import { ActionFunctionArgs, json, LoaderFunctionArgs, redirect } from "@remix-run/node";
import i18n from "~/i18next.server";
import serverRequest, { fastRequest, getToken } from "~/lib/request.server";
import { AuthMethod } from "~/lib/api/user";
import { useTranslation } from "react-i18next";
import { useLoaderData, Form } from "@remix-run/react";
import { Button } from "react-bootstrap";
export const action = async ({ request }: ActionFunctionArgs) => {
const data = await request.formData();
const token = getToken(request);
const id = data.get("remove-id") as string;
await fastRequest("DELETE", `/auth/methods/${id}`, { token, isInternal: true });
return redirect("/settings/auth", 303);
};
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
const t = await i18n.getFixedT(request);
const token = getToken(request);
const method = await serverRequest<AuthMethod>("GET", `/auth/methods/${params.id}`, {
token,
isInternal: true,
});
return json({ method, meta: { title: t("settings.auth.remove-auth-method-title") } });
};
export default function RemoveAuthMethodPage() {
const { t } = useTranslation();
const { method } = useLoaderData<typeof loader>();
let methodName;
switch (method.type) {
case "EMAIL":
methodName = "email";
break;
case "DISCORD":
methodName = "Discord";
break;
case "FEDIVERSE":
methodName = "Fediverse";
break;
case "GOOGLE":
methodName = "Google";
break;
case "TUMBLR":
methodName = "Tumblr";
break;
}
return (
<>
<h3>{t("settings.auth.remove-auth-method-title")}</h3>
<p>
{t("settings.auth.remove-auth-method-hint", {
username: method.remote_username || method.remote_id,
methodName,
})}
</p>
<p>
<Form method="POST">
<input type="hidden" name="remove-id" value={method.id} />
<Button type="submit" color="primary">
{t("settings.auth.remove-auth-method")}
</Button>
</Form>
</p>
</>
);
}

View file

@ -17,7 +17,8 @@
"internal-server-error": "Server experienced an internal error, please try again later.",
"member-not-found": "Member not found, please check your spelling and try again.",
"user-not-found": "User not found, please check your spelling and try again.",
"account-already-linked": "This account is already linked with a pronouns.cc account."
"account-already-linked": "This account is already linked with a pronouns.cc account.",
"last-auth-method": "You cannot remove your last authentication method."
},
"title": "An error occurred",
"more-info": "Click here for a more detailed error"
@ -141,8 +142,10 @@
"new-email-pending": "Email address added! Click the link in your inbox to confirm.",
"email-link-success": "Email successfully linked",
"redirect-to-auth-hint": "You will be redirected back to the authentication page in a few seconds.",
"email-addresses": "Email addresses",
"remove-auth-method-title": "Remove authentication method",
"remove-auth-method-hint": "Are you sure you want to remove {{username}} ({{methodName}}) from your account? You will no longer be able to log in using it.",
"remove-auth-method": "Remove",
"email-addresses": "Email addresses",
"discord-accounts": "Linked Discord accounts",
"fediverse-accounts": "Linked Fediverse accounts"
},