diff --git a/.env.sample b/.env.sample index 14d3456..a21c4a3 100644 --- a/.env.sample +++ b/.env.sample @@ -16,3 +16,5 @@ BREVO_API_KEY="fake" CONFIG_ALLOW_SIGNUPS="false" CONFIG_ENABLED_APPS="dashboard,news,files,notes,photos" CONFIG_FILES_ROOT_PATH="data-files" +CONFIG_ENABLE_EMAILS="false" # if true, email verification will be required for signups (using Brevo) +CONFIG_ENABLE_FOREVER_SIGNUP="true" # if true, all signups become active for 100 years diff --git a/README.md b/README.md index 2aaaec8..267128e 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,6 @@ This is the [bewCloud app](https://bewcloud.com) built using [Fresh](https://fre Check the [Development section below](#development). -> [!NOTE] -> You don't need to have emails (Brevo) setup to have the app work. Those are only setup and used for email verification and future needs. You can simply make any `user.status = 'active'` and `user.subscription.expires_at = new Date('2100-01-01')` to "never" expire, in the database, directly. - > [!IMPORTANT] > Even with signups disabled (`CONFIG_ALLOW_SIGNUPS="false"`), the first signup will work and become an admin. diff --git a/lib/auth.ts b/lib/auth.ts index 563d5eb..c4a72e7 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -152,7 +152,9 @@ export async function logoutUser(request: Request) { name: COOKIE_NAME, value: '', expires: tomorrow, - domain: isRunningLocally(request) ? 'localhost' : baseUrl.replace('https://', ''), + domain: isRunningLocally(request) + ? 'localhost' + : baseUrl.replace('https://', '').replace('http://', '').split(':')[0], path: '/', secure: isRunningLocally(request) ? false : true, httpOnly: true, diff --git a/lib/config.ts b/lib/config.ts index 4ea45d7..d2166bb 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -14,6 +14,18 @@ export async function isSignupAllowed() { return false; } +export function isEmailEnabled() { + const areEmailsAllowed = Deno.env.get('CONFIG_ENABLE_EMAILS') === 'true'; + + return areEmailsAllowed; +} + +export function isForeverSignupEnabled() { + const areForeverAccountsEnabled = Deno.env.get('CONFIG_ENABLE_FOREVER_SIGNUP') === 'true'; + + return areForeverAccountsEnabled; +} + export function getFilesRootPath() { const configRootPath = Deno.env.get('CONFIG_FILES_ROOT_PATH'); diff --git a/lib/data/user.ts b/lib/data/user.ts index 99925f7..02cfe5b 100644 --- a/lib/data/user.ts +++ b/lib/data/user.ts @@ -1,6 +1,7 @@ import Database, { sql } from '/lib/interfaces/database.ts'; import { User, UserSession, VerificationCode } from '/lib/types.ts'; import { generateRandomCode } from '/lib/utils/misc.ts'; +import { isEmailEnabled, isForeverSignupEnabled } from '/lib/config.ts'; const db = new Database(); @@ -32,7 +33,7 @@ export async function getUserById(id: string) { } export async function createUser(email: User['email'], hashedPassword: User['hashed_password']) { - const trialDays = 30; + const trialDays = isForeverSignupEnabled() ? 36_525 : 30; const now = new Date(); const trialEndDate = new Date(new Date().setUTCDate(new Date().getUTCDate() + trialDays)); @@ -42,7 +43,7 @@ export async function createUser(email: User['email'], hashedPassword: User['has updated_at: now.toISOString(), }; - const extra: User['extra'] = { is_email_verified: false }; + const extra: User['extra'] = { is_email_verified: isEmailEnabled() ? false : true }; // First signup will be an admin "forever" if (!(await isThereAnAdmin())) { @@ -62,7 +63,7 @@ export async function createUser(email: User['email'], hashedPassword: User['has [ email, JSON.stringify(subscription), - extra.is_admin ? 'active' : 'trial', + extra.is_admin || isForeverSignupEnabled() ? 'active' : 'trial', hashedPassword, JSON.stringify(extra), ], diff --git a/routes/login.tsx b/routes/login.tsx index 7f855f6..9650cf0 100644 --- a/routes/login.tsx +++ b/routes/login.tsx @@ -6,6 +6,7 @@ import { FormField, generateFieldHtml, getFormDataField } from '/lib/form-utils. import { createVerificationCode, getUserByEmail, updateUser, validateVerificationCode } from '/lib/data/user.ts'; import { sendVerifyEmailEmail } from '/lib/providers/brevo.ts'; import { FreshContextState } from '/lib/types.ts'; +import { isEmailEnabled } from '/lib/config.ts'; interface Data { error?: string; @@ -62,6 +63,12 @@ export const handler: Handlers = { throw new Error('Email not found or invalid password.'); } + if (!isEmailEnabled() && !user.extra.is_email_verified) { + user.extra.is_email_verified = true; + + await updateUser(user); + } + if (!user.extra.is_email_verified) { const code = getFormDataField(formData, 'verification-code'); diff --git a/routes/settings.tsx b/routes/settings.tsx index a62a5fa..18a5a03 100644 --- a/routes/settings.tsx +++ b/routes/settings.tsx @@ -12,6 +12,7 @@ import { import { convertFormDataToObject, generateHash, validateEmail } from '/lib/utils/misc.ts'; import { getFormDataField } from '/lib/form-utils.tsx'; import { sendVerifyEmailEmail } from '/lib/providers/brevo.ts'; +import { isEmailEnabled } from '/lib/config.ts'; import Settings, { Action, actionWords } from '/islands/Settings.tsx'; interface Data { @@ -73,7 +74,7 @@ export const handler: Handlers = { throw new Error('Email is already in use.'); } - if (action === 'change-email') { + if (action === 'change-email' && isEmailEnabled()) { const verificationCode = await createVerificationCode(user, email, 'email'); await sendVerifyEmailEmail(email, verificationCode); @@ -81,9 +82,11 @@ export const handler: Handlers = { successTitle = 'Verify your email!'; successMessage = 'You have received a code in your new email. Use it to verify it here.'; } else { - const code = getFormDataField(formData, 'verification-code'); + if (isEmailEnabled()) { + const code = getFormDataField(formData, 'verification-code'); - await validateVerificationCode(user, email, code, 'email'); + await validateVerificationCode(user, email, code, 'email'); + } user.email = email; diff --git a/routes/signup.tsx b/routes/signup.tsx index 2542959..ee0c8a9 100644 --- a/routes/signup.tsx +++ b/routes/signup.tsx @@ -3,9 +3,9 @@ import { Handlers, PageProps } from 'fresh/server.ts'; import { generateHash, helpEmail, validateEmail } from '/lib/utils/misc.ts'; import { PASSWORD_SALT } from '/lib/auth.ts'; import { FormField, generateFieldHtml, getFormDataField } from '/lib/form-utils.tsx'; -import { createUser, createVerificationCode, getUserByEmail } from '/lib/data/user.ts'; +import { createUser, createVerificationCode, getUserByEmail, updateUser } from '/lib/data/user.ts'; import { sendVerifyEmailEmail } from '/lib/providers/brevo.ts'; -import { isSignupAllowed } from '/lib/config.ts'; +import { isEmailEnabled, isSignupAllowed } from '/lib/config.ts'; import { FreshContextState } from '/lib/types.ts'; interface Data { @@ -64,9 +64,11 @@ export const handler: Handlers = { const user = await createUser(email, hashedPassword); - const verificationCode = await createVerificationCode(user, user.email, 'email'); + if (isEmailEnabled()) { + const verificationCode = await createVerificationCode(user, user.email, 'email'); - await sendVerifyEmailEmail(user.email, verificationCode); + await sendVerifyEmailEmail(user.email, verificationCode); + } return new Response('Signup successful', { status: 303,