하나의 공개 키로 여러 계정에 로그인할 수 있도록 구현

This commit is contained in:
static
2024-12-29 01:37:44 +09:00
parent c16abca832
commit af51f04b94
7 changed files with 131 additions and 71 deletions

View File

@@ -14,7 +14,7 @@
const login = async () => {
// TODO: Validation
if (await requestLogin(email, password)) {
if (await requestLogin(email, password, $keyPairStore)) {
await goto(
$keyPairStore
? data.redirectPath

View File

@@ -1,10 +1,49 @@
export const requestLogin = async (email: string, password: string) => {
const res = await fetch("/api/auth/login", {
import { exportRSAKeyToBase64 } from "$lib/modules/crypto";
import { requestPubKeyRegistration } from "../../key/export/service";
const callLoginAPI = async (email: string, password: string, pubKeyBase64?: string) => {
return await fetch("/api/auth/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ email, password }),
body: JSON.stringify({
email,
password,
pubKey: pubKeyBase64,
}),
});
return res.ok;
};
export const requestLogin = async (
email: string,
password: string,
keyPair: CryptoKeyPair | null,
registerPubKey = true,
): Promise<boolean> => {
const pubKeyBase64 = keyPair
? await exportRSAKeyToBase64(keyPair.publicKey, "public")
: undefined;
let loginRes = await callLoginAPI(email, password, pubKeyBase64);
if (loginRes.ok) {
return true;
} else if (loginRes.status !== 401 || !keyPair || !registerPubKey) {
return false;
}
const { message } = await loginRes.json();
if (message !== "Unregistered public key") {
return false;
}
loginRes = await callLoginAPI(email, password);
if (!loginRes.ok) {
return false;
}
if (await requestPubKeyRegistration(pubKeyBase64!, keyPair.privateKey)) {
return requestLogin(email, password, keyPair, false);
} else {
return false;
}
};