,
+ props: Props
+) => {
+ const ssrComponent = component as any;
+ const { html: mjml } = ssrComponent.render(props);
const { html, errors } = mjml2html(stripSvelteClasses(mjml));
@@ -48,5 +53,5 @@ export const renderMjmlComponent = (component, props) => {
return html;
};
-const stripSvelteClasses = (html) =>
+const stripSvelteClasses = (html: string) =>
html.replaceAll(/class="s-[\w-]+"/g, '').replaceAll(/data-svelte-h="[\w-]+"/g, '');
diff --git a/src/lib/emailRegex.js b/src/lib/emailRegex.ts
similarity index 100%
rename from src/lib/emailRegex.js
rename to src/lib/emailRegex.ts
diff --git a/src/lib/handleApiResponse.js b/src/lib/handleApiResponse.ts
similarity index 83%
rename from src/lib/handleApiResponse.js
rename to src/lib/handleApiResponse.ts
index b3654bc..c38f5d6 100644
--- a/src/lib/handleApiResponse.js
+++ b/src/lib/handleApiResponse.ts
@@ -1,4 +1,4 @@
-const handleApiResponse = async (res, onSuccess) => {
+const handleApiResponse = async (res: Response, onSuccess: () => void) => {
if (res.statusText === 'OK') {
onSuccess();
} else {
diff --git a/src/lib/models/db.js b/src/lib/models/db.ts
similarity index 88%
rename from src/lib/models/db.js
rename to src/lib/models/db.ts
index 74da2fc..558477f 100644
--- a/src/lib/models/db.js
+++ b/src/lib/models/db.ts
@@ -7,9 +7,10 @@ import { RAIBU_DB_HOST, RAIBU_DB_USER, RAIBU_DB_PASSWORD } from '$env/static/pri
import EmailVerificationCodeSchema from './emailVerificationCode';
import TooManyLoginsTokenSchema from './tooManyLoginsToken';
import passwordResetTokenSchema from './passwordResetToken';
+import type { KeyDoc } from '@lucia-auth/adapter-mongoose/dist/docs';
-// Mongoose state somehow carries-over over reloads and it causes errors when we remake the models.
-// So we clear out the models and connection to prevent this.
+// @ts-expect-error Mongoose state somehow carries-over over reloads and it causes errors when we remake the models.
+// So we clear out the models and connection to prevent this
mongodb.models = [];
export const User = mongodb.model(
@@ -40,7 +41,7 @@ export const User = mongodb.model(
{ _id: false }
)
);
-export const Key = mongodb.model(
+const Key = mongodb.model(
'Key',
new mongodb.Schema(
{
@@ -53,7 +54,7 @@ export const Key = mongodb.model(
required: true
},
hashed_password: String
- },
+ } as unknown as KeyDoc,
{ _id: false }
)
);
@@ -116,6 +117,7 @@ export const auth = lucia({
};
}
});
+export type Auth = typeof auth;
// if (dev) {
// Promise.all([
diff --git a/src/lib/models/emailVerificationCode.js b/src/lib/models/emailVerificationCode.ts
similarity index 87%
rename from src/lib/models/emailVerificationCode.js
rename to src/lib/models/emailVerificationCode.ts
index 3f4fb15..d6831db 100644
--- a/src/lib/models/emailVerificationCode.js
+++ b/src/lib/models/emailVerificationCode.ts
@@ -1,10 +1,10 @@
import { generateRandomString, isWithinExpiration } from 'lucia/utils';
import VerifyEmail from '$lib/email/VerifyEmail.svelte';
-import { renderMjmlComponent } from '$lib/email/email';
-import { sendEmail } from '../email/email';
+import { renderMjmlComponent, sendEmail } from '$lib/email/email';
import { error } from '@sveltejs/kit';
import { auth } from './db';
import mongodb from 'mongoose';
+import type { User } from 'lucia';
const ONE_MINUTE_IN_MS = 1000 * 60;
const THIRTY_MINUTES_IN_MS = ONE_MINUTE_IN_MS * 30;
@@ -33,7 +33,7 @@ const EmailVerificationCodeSchema = new mongodb.Schema(
},
{
statics: {
- async new(user) {
+ async new(user: User) {
const codes = await this.find({ user: user.userId }).exec();
let code;
@@ -44,7 +44,7 @@ const EmailVerificationCodeSchema = new mongodb.Schema(
}
if (code === undefined) {
- let randomCode = generateRandomString(6, '0123456789');
+ const randomCode = generateRandomString(6, '0123456789');
code = await new this({
code: randomCode,
user: user.userId,
@@ -59,7 +59,7 @@ const EmailVerificationCodeSchema = new mongodb.Schema(
return code;
},
- async verifyAndDelete(verifyMe, user) {
+ async verifyAndDelete(verifyMe: string, user: User) {
const token = await this.findOne({ code: verifyMe, user: user.userId }).exec();
if (token === null) {
throw error(400, 'Token does not exist');
diff --git a/src/lib/models/passwordResetToken.js b/src/lib/models/passwordResetToken.ts
similarity index 85%
rename from src/lib/models/passwordResetToken.js
rename to src/lib/models/passwordResetToken.ts
index ebdee27..64f9e0c 100644
--- a/src/lib/models/passwordResetToken.js
+++ b/src/lib/models/passwordResetToken.ts
@@ -2,10 +2,11 @@ import { generateRandomString, isWithinExpiration } from 'lucia/utils';
import ResetPasswordEmail from '$lib/email/ResetPasswordEmail.svelte';
import { renderMjmlComponent } from '$lib/email/email';
import { PUBLIC_RAIBU_URL } from '$env/static/public';
-import { sendEmail } from '../email/email';
+import { sendEmail } from '$lib/email/email';
import { error } from '@sveltejs/kit';
import { auth } from './db';
import mongodb from 'mongoose';
+import type { User } from 'lucia';
const ONE_HOUR_IN_MS = 1000 * 60 * 60;
const FOUR_HOURS_IN_MS = ONE_HOUR_IN_MS * 4;
@@ -34,11 +35,11 @@ const passwordResetTokenSchema = new mongodb.Schema(
},
{
statics: {
- async new(user) {
+ async new(user: User) {
const tokens = await this.find({ user: user.userId }).exec();
if (tokens.length > 0) {
- let existingToken = tokens.find((token) => {
+ const existingToken = tokens.find((token) => {
return isWithinExpiration(token.expires - ONE_HOUR_IN_MS);
});
if (existingToken !== undefined) {
@@ -46,8 +47,8 @@ const passwordResetTokenSchema = new mongodb.Schema(
}
}
- let randomString = generateRandomString(63);
- let token = await new this({
+ const randomString = generateRandomString(63);
+ const token = await new this({
token: randomString,
user: user.userId,
expires: new Date().getTime() + FOUR_HOURS_IN_MS
@@ -60,7 +61,7 @@ const passwordResetTokenSchema = new mongodb.Schema(
return token;
},
- async verifyAndDelete(verifyMe) {
+ async verifyAndDelete(verifyMe: string) {
const token = await this.findOne({ token: verifyMe }).exec();
if (token === null) {
throw error(400, 'Token does not exist');
diff --git a/src/lib/models/tooManyLoginsToken.js b/src/lib/models/tooManyLoginsToken.ts
similarity index 79%
rename from src/lib/models/tooManyLoginsToken.js
rename to src/lib/models/tooManyLoginsToken.ts
index 4a957a0..14dafb0 100644
--- a/src/lib/models/tooManyLoginsToken.js
+++ b/src/lib/models/tooManyLoginsToken.ts
@@ -1,11 +1,12 @@
import { generateRandomString, isWithinExpiration } from 'lucia/utils';
-import tooManyLoginsEmail from '$lib/email/TooManyLoginsEmail.svelte';
+import TooManyLoginsEmail from '$lib/email/TooManyLoginsEmail.svelte';
import { renderMjmlComponent } from '$lib/email/email';
import { PUBLIC_RAIBU_URL } from '$env/static/public';
-import { sendEmail } from '../email/email';
+import { sendEmail } from '$lib/email/email';
import { error } from '@sveltejs/kit';
import { auth } from './db';
import mongodb from 'mongoose';
+import type { User } from 'lucia';
const ONE_HOUR_IN_MS = 1000 * 60 * 60;
const FOUR_HOURS_IN_MS = ONE_HOUR_IN_MS * 4;
@@ -34,11 +35,11 @@ const tooManyLoginsTokenSchema = new mongodb.Schema(
},
{
statics: {
- async new(user) {
+ async new(user: User) {
const tokens = await this.find({ user: user.userId }).exec();
if (tokens.length > 0) {
- let existingToken = tokens.find((token) => {
+ const existingToken = tokens.find((token) => {
return isWithinExpiration(token.expires - ONE_HOUR_IN_MS);
});
if (existingToken !== undefined) {
@@ -46,21 +47,21 @@ const tooManyLoginsTokenSchema = new mongodb.Schema(
}
}
- let randomString = generateRandomString(63);
- let token = await new this({
+ const randomString = generateRandomString(63);
+ const token = await new this({
token: randomString,
user: user.userId,
expires: new Date().getTime() + FOUR_HOURS_IN_MS
}).save();
- const emailHtml = renderMjmlComponent(tooManyLoginsEmail, {
+ const emailHtml = renderMjmlComponent(TooManyLoginsEmail, {
unlockLink: `${PUBLIC_RAIBU_URL}/user/too-many-logins/${token.token}`
});
sendEmail(emailHtml, 'Unlock your account', user.email);
return token;
},
- async verifyAndDelete(verifyMe) {
+ async verifyAndDelete(verifyMe: string) {
const token = await this.findOne({ token: verifyMe }).exec();
if (token === null) {
throw error(400, 'Token does not exist');
diff --git a/src/lib/resetPasswordModal.svelte b/src/lib/resetPasswordModal.svelte
index 5054299..cc59153 100644
--- a/src/lib/resetPasswordModal.svelte
+++ b/src/lib/resetPasswordModal.svelte
@@ -1,4 +1,4 @@
-
diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte
index a798977..27beb7c 100644
--- a/src/routes/+page.svelte
+++ b/src/routes/+page.svelte
@@ -1,4 +1,4 @@
-
+
diff --git a/src/routes/user/+server.js b/src/routes/user/+server.ts
similarity index 85%
rename from src/routes/user/+server.js
rename to src/routes/user/+server.ts
index 35087b7..2db2c2e 100644
--- a/src/routes/user/+server.js
+++ b/src/routes/user/+server.ts
@@ -2,11 +2,12 @@ import { error } from '@sveltejs/kit';
import { auth } from '$lib/models/db';
import { LuciaError } from 'lucia';
import { dev } from '$app/environment';
-import { EmailVerificationCode } from '$lib/models/db.js';
+import { EmailVerificationCode } from '$lib/models/db';
import emailRegex from '$lib/emailRegex';
import commonPasswordList from 'fxa-common-password-list';
+import type { RequestEvent, RequestHandler } from './$types';
-export const POST = async ({ request, locals }) => {
+export const POST: RequestHandler = async ({ request, locals }: RequestEvent) => {
if (!dev) {
throw error(400, 'We are currently not accepting registrations. Come back later');
}
@@ -40,7 +41,8 @@ export const POST = async ({ request, locals }) => {
},
attributes: {
email,
- isEmailVerified: false
+ isEmailVerified: false,
+ isLocked: false
}
});
diff --git a/src/routes/user/CopyMe.svelte b/src/routes/user/CopyMe.svelte
index 9ef5776..d02f381 100644
--- a/src/routes/user/CopyMe.svelte
+++ b/src/routes/user/CopyMe.svelte
@@ -1,6 +1,6 @@
-