support 2FA auth

This commit is contained in:
kawamataryo
2024-10-18 09:14:45 +09:00
parent f7e2c58c72
commit 9544a7f67c
4 changed files with 67 additions and 8 deletions

View File

@@ -1,24 +1,35 @@
import type { PlasmoMessaging } from "@plasmohq/messaging";
import { BskyClient } from "../../lib/bskyClient";
import { AUTH_FACTOR_TOKEN_REQUIRED_ERROR_MESSAGE } from "~lib/constants";
import { ComAtprotoServerCreateSession } from "@atproto/api";
const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const { identifier, password } = req.body;
const { identifier, password, authFactorToken } = req.body;
try {
const agent = await BskyClient.createAgent({
identifier,
password,
...(authFactorToken && { authFactorToken: authFactorToken }),
});
res.send({
session: agent.session,
});
} catch (e) {
res.send({
error: {
message: e.message,
},
});
if (e instanceof ComAtprotoServerCreateSession.AuthFactorTokenRequiredError) {
res.send({
error: {
message: AUTH_FACTOR_TOKEN_REQUIRED_ERROR_MESSAGE,
},
});
} else {
res.send({
error: {
message: e.message,
},
});
}
}
};