11import { db } from "@/server/db" ;
2- import { emailVerificationToken , user } from "@/server/db/schema" ;
2+ import { user } from "@/server/db/schema" ;
33import crypto from "crypto" ;
44import sendEmail from "./sendEmail" ;
5- import { and , eq } from "drizzle-orm" ;
5+ import { eq } from "drizzle-orm" ;
6+
7+ function getBaseUrl ( ) {
8+ if ( typeof window !== "undefined" ) return "" ;
9+ const env = process . env . DOMAIN_NAME || process . env . VERCEL_URL ;
10+ if ( env ) return "https://" + env ;
11+ return "http://localhost:3000" ;
12+ }
613
714export const generateEmailToken = ( ) => {
815 return crypto . randomBytes ( 64 ) . toString ( "hex" ) ;
916} ;
1017
11- export const storeTokenInDb = async (
12- userId : string ,
13- token : string ,
14- expiresAt : Date ,
15- email : string ,
16- ) => {
17- try {
18- const newToken = await db
19- . insert ( emailVerificationToken )
20- . values ( {
21- userId,
22- token,
23- expiresAt,
24- email,
25- } )
26- . returning ( ) ;
27-
28- return newToken [ 0 ] ;
29- } catch ( error ) {
30- console . error ( "Error storing token in database:" , error ) ;
31- throw new Error ( "Failed to store email verification token" ) ;
32- }
33- } ;
34-
3518export const sendVerificationEmail = async ( email : string , token : string ) => {
36- const verificationLink = `${ process . env . NEXT_PUBLIC_BACKEND_URL } /verify-email?token=${ token } ` ;
19+ const verificationLink = `${ getBaseUrl ( ) } /verify-email?token=${ token } ` ;
3720 const subject = "Verify Your Email Address" ;
3821 const htmlMessage = `
3922 <!DOCTYPE html>
@@ -111,24 +94,6 @@ export const sendVerificationEmail = async (email: string, token: string) => {
11194 }
11295} ;
11396
114- export const getTokenFromDb = async ( token : string , userId : string ) => {
115- try {
116- const tokenFromDb = await db
117- . select ( )
118- . from ( emailVerificationToken )
119- . where (
120- and (
121- eq ( emailVerificationToken . token , token ) ,
122- eq ( emailVerificationToken . userId , userId ) ,
123- ) ,
124- ) ;
125- return tokenFromDb ;
126- } catch ( error ) {
127- console . error ( "Error fetching token from database:" , error ) ;
128- throw new Error ( "Failed to fetch email verification token" ) ;
129- }
130- } ;
131-
13297export const updateEmail = async ( userId : string , newEmail : string ) => {
13398 try {
13499 await db . update ( user ) . set ( { email : newEmail } ) . where ( eq ( user . id , userId ) ) ;
@@ -138,17 +103,6 @@ export const updateEmail = async (userId: string, newEmail: string) => {
138103 }
139104} ;
140105
141- export const deleteTokenFromDb = async ( token : string ) => {
142- try {
143- await db
144- . delete ( emailVerificationToken )
145- . where ( eq ( emailVerificationToken . token , token ) ) ;
146- } catch ( error ) {
147- console . error ( "Error deleting token from database:" , error ) ;
148- throw new Error ( "Failed to delete email verification token" ) ;
149- }
150- } ;
151-
152106export const checkIfEmailExists = async ( email : string ) => {
153107 try {
154108 const existingUser = await db . query . user . findFirst ( {
0 commit comments