12 implement twilio sms #37
4 changed files with 13 additions and 24 deletions
8
src/app.d.ts
vendored
8
src/app.d.ts
vendored
|
|
@ -1,7 +1,5 @@
|
||||||
// See https://svelte.dev/docs/kit/types#app.d.ts
|
// See https://svelte.dev/docs/kit/types#app.d.ts
|
||||||
|
|
||||||
import type { TwilioConfiguration } from '$lib/server/twilio';
|
|
||||||
|
|
||||||
// for information about these interfaces
|
// for information about these interfaces
|
||||||
declare global {
|
declare global {
|
||||||
namespace App {
|
namespace App {
|
||||||
|
|
@ -9,10 +7,6 @@ declare global {
|
||||||
interface Locals {
|
interface Locals {
|
||||||
user: import('lucia').User | null;
|
user: import('lucia').User | null;
|
||||||
session: import('lucia').Session | null;
|
session: import('lucia').Session | null;
|
||||||
twilio: {
|
|
||||||
config: TwilioConfiguration;
|
|
||||||
client: import('twilio').Twilio;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
// interface PageData {}
|
// interface PageData {}
|
||||||
// interface PageState {}
|
// interface PageState {}
|
||||||
|
|
@ -20,4 +14,4 @@ declare global {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export {};
|
export {};
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
import { TwilioConfig } from '$lib/server/twilio';
|
|
||||||
import twilio from 'twilio';
|
|
||||||
|
|
||||||
export async function handle({ event, resolve }) {
|
|
||||||
const twilioConfig = TwilioConfig();
|
|
||||||
const twilioClient = twilio(twilioConfig.twilio_account_sid, twilioConfig.twilio_auth_token);
|
|
||||||
event.locals.twilio = {
|
|
||||||
config: twilioConfig,
|
|
||||||
client: twilioClient,
|
|
||||||
};
|
|
||||||
return await resolve(event);
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
import { PhoneRegex } from '$lib/regex';
|
import { PhoneRegex } from '$lib/regex';
|
||||||
import { logger } from '$lib/server/logger';
|
import { logger } from '$lib/server/logger';
|
||||||
|
import { TwilioConfig } from '$lib/server/twilio';
|
||||||
import { fail, type Actions } from '@sveltejs/kit';
|
import { fail, type Actions } from '@sveltejs/kit';
|
||||||
|
import twilio from 'twilio';
|
||||||
import zod from 'zod';
|
import zod from 'zod';
|
||||||
|
|
||||||
export const actions = {
|
export const actions = {
|
||||||
|
|
@ -29,12 +31,17 @@ export const actions = {
|
||||||
return fail(400, { error: 'invalid_message' });
|
return fail(400, { error: 'invalid_message' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const twilio = event.locals.twilio;
|
const twilioConfig = TwilioConfig();
|
||||||
|
const twilioClient = twilio(
|
||||||
|
twilioConfig.twilio_account_sid,
|
||||||
|
twilioConfig.twilio_auth_token
|
||||||
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await twilio.client.messages.create({
|
const result = await twilioClient.messages.create({
|
||||||
to: phone,
|
to: phone,
|
||||||
body: message,
|
body: message,
|
||||||
from: twilio.config.twilio_phone_number,
|
from: twilioConfig.twilio_phone_number,
|
||||||
});
|
});
|
||||||
logger.debug(result);
|
logger.debug(result);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
@ -45,4 +52,4 @@ export const actions = {
|
||||||
success: true,
|
success: true,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
} satisfies Actions;
|
} satisfies Actions;
|
||||||
|
|
@ -64,4 +64,4 @@
|
||||||
.page {
|
.page {
|
||||||
@apply flex flex-col items-center justify-around gap-24 py-[10%];
|
@apply flex flex-col items-center justify-around gap-24 py-[10%];
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
Loading…
Add table
Reference in a new issue