mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-16 06:58:46 +00:00
하나의 공개 키로 여러 계정에 로그인할 수 있도록 구현
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user