mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 13:25:18 +00:00
feat: config.js
This commit is contained in:
20
lib/auth.ts
20
lib/auth.ts
@@ -1,6 +1,6 @@
|
||||
import { AUTH_LOGIN_URL, IS_SELF_HOSTED_MODE, SELF_HOSTED_REDIRECT_URL } from "@/lib/constants"
|
||||
import config from "@/lib/config"
|
||||
import { createUserDefaults } from "@/models/defaults"
|
||||
import { getSelfHostedUser, getUserByEmail } from "@/models/users"
|
||||
import { getSelfHostedUser } from "@/models/users"
|
||||
import { User } from "@prisma/client"
|
||||
import { betterAuth } from "better-auth"
|
||||
import { prismaAdapter } from "better-auth/adapters/prisma"
|
||||
@@ -22,7 +22,7 @@ export const auth = betterAuth({
|
||||
database: prismaAdapter(prisma, { provider: "postgresql" }),
|
||||
email: {
|
||||
provider: "resend",
|
||||
from: process.env.RESEND_FROM_EMAIL!,
|
||||
from: config.email.from,
|
||||
resend,
|
||||
},
|
||||
session: {
|
||||
@@ -49,14 +49,10 @@ export const auth = betterAuth({
|
||||
},
|
||||
plugins: [
|
||||
emailOTP({
|
||||
disableSignUp: true,
|
||||
disableSignUp: config.auth.disableSignup,
|
||||
otpLength: 6,
|
||||
expiresIn: 10 * 60, // 10 minutes
|
||||
sendVerificationOTP: async ({ email, otp }) => {
|
||||
const user = await getUserByEmail(email as string)
|
||||
if (!user) {
|
||||
throw new Error("User with this email does not exist")
|
||||
}
|
||||
await sendOTPCodeEmail({ email, otp })
|
||||
},
|
||||
}),
|
||||
@@ -65,7 +61,7 @@ export const auth = betterAuth({
|
||||
})
|
||||
|
||||
export async function getSession() {
|
||||
if (IS_SELF_HOSTED_MODE) {
|
||||
if (config.selfHosted.isEnabled) {
|
||||
const user = await getSelfHostedUser()
|
||||
return user ? { user } : null
|
||||
}
|
||||
@@ -78,10 +74,10 @@ export async function getSession() {
|
||||
export async function getCurrentUser(): Promise<User> {
|
||||
const session = await getSession()
|
||||
if (!session || !session.user) {
|
||||
if (IS_SELF_HOSTED_MODE) {
|
||||
redirect(SELF_HOSTED_REDIRECT_URL)
|
||||
if (config.selfHosted.isEnabled) {
|
||||
redirect(config.selfHosted.redirectUrl)
|
||||
} else {
|
||||
redirect(AUTH_LOGIN_URL)
|
||||
redirect(config.auth.loginUrl)
|
||||
}
|
||||
}
|
||||
return session.user as User
|
||||
|
||||
26
lib/config.ts
Normal file
26
lib/config.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
const config = {
|
||||
app: {
|
||||
title: "TaxHacker",
|
||||
description: "Your personal AI accountant",
|
||||
version: process.env.npm_package_version || "0.0.1",
|
||||
},
|
||||
upload: {
|
||||
acceptedMimeTypes: "image/*,.pdf,.doc,.docx,.xls,.xlsx",
|
||||
},
|
||||
selfHosted: {
|
||||
isEnabled: process.env.SELF_HOSTED_MODE === "true",
|
||||
redirectUrl: "/self-hosted/redirect",
|
||||
welcomeUrl: "/self-hosted",
|
||||
},
|
||||
auth: {
|
||||
loginUrl: "/enter",
|
||||
disableSignup: process.env.DISABLE_SIGNUP === "true" || process.env.SELF_HOSTED_MODE === "true",
|
||||
},
|
||||
email: {
|
||||
apiKey: process.env.RESEND_API_KEY || "",
|
||||
from: process.env.RESEND_FROM_EMAIL || "",
|
||||
audienceId: process.env.RESEND_AUDIENCE_ID || "",
|
||||
},
|
||||
}
|
||||
|
||||
export default config
|
||||
@@ -1,7 +0,0 @@
|
||||
export const APP_TITLE = "TaxHacker"
|
||||
export const APP_DESCRIPTION = "Your personal AI accountant"
|
||||
export const FILE_ACCEPTED_MIMETYPES = "image/*,.pdf,.doc,.docx,.xls,.xlsx"
|
||||
export const IS_SELF_HOSTED_MODE = process.env.SELF_HOSTED_MODE === "true"
|
||||
export const SELF_HOSTED_REDIRECT_URL = "/self-hosted/redirect"
|
||||
export const SELF_HOSTED_WELCOME_URL = "/self-hosted"
|
||||
export const AUTH_LOGIN_URL = "/enter"
|
||||
@@ -27,9 +27,6 @@ export async function getCurrencyRate(currencyCodeFrom: string, currencyCodeTo:
|
||||
export async function fetchHistoricalCurrencyRates(currency: string = "USD", date: Date): Promise<HistoricRate[]> {
|
||||
const formattedDate = format(date, "yyyy-MM-dd")
|
||||
|
||||
console.log("DATE", formattedDate)
|
||||
console.log("QUERY", encodeURIComponent(`https://www.xe.com/currencytables/?from=${currency}&date=${formattedDate}`))
|
||||
|
||||
const url = `https://corsproxy.io/?url=${encodeURIComponent(
|
||||
`https://www.xe.com/currencytables/?from=${currency}&date=${formattedDate}`
|
||||
)}`
|
||||
|
||||
@@ -2,14 +2,15 @@ import { NewsletterWelcomeEmail } from "@/components/emails/newsletter-welcome-e
|
||||
import { OTPEmail } from "@/components/emails/otp-email"
|
||||
import React from "react"
|
||||
import { Resend } from "resend"
|
||||
import config from "./config"
|
||||
|
||||
export const resend = new Resend(process.env.RESEND_API_KEY)
|
||||
export const resend = new Resend(config.email.apiKey)
|
||||
|
||||
export async function sendOTPCodeEmail({ email, otp }: { email: string; otp: string }) {
|
||||
const html = React.createElement(OTPEmail, { otp })
|
||||
|
||||
return await resend.emails.send({
|
||||
from: process.env.RESEND_FROM_EMAIL!,
|
||||
from: config.email.from,
|
||||
to: email,
|
||||
subject: "Your TaxHacker verification code",
|
||||
react: html,
|
||||
@@ -20,7 +21,7 @@ export async function sendNewsletterWelcomeEmail(email: string) {
|
||||
const html = React.createElement(NewsletterWelcomeEmail)
|
||||
|
||||
return await resend.emails.send({
|
||||
from: process.env.RESEND_FROM_EMAIL as string,
|
||||
from: config.email.from,
|
||||
to: email,
|
||||
subject: "Welcome to TaxHacker Newsletter!",
|
||||
react: html,
|
||||
|
||||
Reference in New Issue
Block a user