make phone calls

This commit is contained in:
m5r
2022-06-11 19:29:58 +02:00
parent dbe209c7fc
commit f1702180b7
9 changed files with 49 additions and 24 deletions

View File

@ -2,7 +2,7 @@ import { type LoaderFunction } from "@remix-run/node";
import Twilio from "twilio";
import { refreshSessionData, requireLoggedIn } from "~/utils/auth.server";
import { encrypt } from "~/utils/encryption";
import { decrypt, encrypt } from "~/utils/encryption";
import db from "~/utils/db.server";
import { commitSession } from "~/utils/session.server";
import getTwilioClient from "~/utils/twilio.server";
@ -10,7 +10,7 @@ import getTwilioClient from "~/utils/twilio.server";
export type TwilioTokenLoaderData = string;
const loader: LoaderFunction = async ({ request }) => {
const { user, organization, twilio } = await requireLoggedIn(request);
const { user, twilio } = await requireLoggedIn(request);
if (!twilio) {
throw new Error("unreachable");
}
@ -39,15 +39,15 @@ const loader: LoaderFunction = async ({ request }) => {
shouldRefreshSession = true;
const apiKey = await twilioClient.newKeys.create({ friendlyName: "Shellphone" });
apiKeySid = apiKey.sid;
apiKeySecret = apiKey.secret;
apiKeySecret = encrypt(apiKey.secret);
await db.twilioAccount.update({
where: { accountSid: twilioAccount.accountSid },
data: { apiKeySid: apiKey.sid, apiKeySecret: encrypt(apiKey.secret) },
data: { apiKeySid, apiKeySecret },
});
}
const accessToken = new Twilio.jwt.AccessToken(twilioAccount.accountSid, apiKeySid, apiKeySecret, {
identity: `${organization.id}__${user.id}`,
const accessToken = new Twilio.jwt.AccessToken(twilioAccount.accountSid, apiKeySid, decrypt(apiKeySecret), {
identity: `${twilio.accountSid}__${user.id}`,
ttl: 3600,
});
const grant = new Twilio.jwt.AccessToken.VoiceGrant({