Skip to content

Commit c76fad9

Browse files
fix: environmental variables missing in GitHub actions + remove some consoles
1 parent 0957357 commit c76fad9

File tree

5 files changed

+55
-18
lines changed

5 files changed

+55
-18
lines changed

app/api/get-ip/route.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export async function GET(request: NextRequest) {
2424

2525
return NextResponse.json({ ip });
2626
} catch (error) {
27-
console.error("Error getting IP:", error);
2827
return NextResponse.json({ error: "Failed to get IP" }, { status: 500 });
2928
}
3029
}

app/api/graphql/route.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ const wrappedHandler = async (req: Request) => {
5858
try {
5959
return await handler(req);
6060
} catch (error) {
61-
console.error("GraphQL Error:", error);
6261
return new Response(
6362
JSON.stringify({
6463
errors: [{ message: "Internal server error" }],

hooks/useSecureTrial.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ export const useSecureTrial = () => {
126126
// Check if user already has an active trial
127127
const checkExistingTrial = async (): Promise<TrialRecord | null> => {
128128
try {
129+
// Check if Appwrite is available
130+
if (!databases) {
131+
return null;
132+
}
133+
129134
const sessionId = await getSessionId();
130135
const deviceFingerprint = getDeviceFingerprint();
131136
const ipAddress = await getUserIP();
@@ -194,14 +199,18 @@ export const useSecureTrial = () => {
194199

195200
return null;
196201
} catch (error) {
197-
console.error("Error checking existing trial:", error);
198202
return null;
199203
}
200204
};
201205

202206
// Create a new trial record
203207
const createTrial = async (): Promise<TrialRecord | null> => {
204208
try {
209+
// Check if Appwrite is available
210+
if (!databases) {
211+
return null;
212+
}
213+
205214
const sessionId = await getSessionId();
206215
const deviceFingerprint = getDeviceFingerprint();
207216
const ipAddress = await getUserIP();
@@ -254,7 +263,6 @@ export const useSecureTrial = () => {
254263

255264
return trialRecord as unknown as TrialRecord;
256265
} catch (error) {
257-
console.error("Error creating trial:", error);
258266
// Clear the creation flag on error
259267
if (typeof window !== "undefined") {
260268
localStorage.removeItem("creating_trial");
@@ -266,6 +274,11 @@ export const useSecureTrial = () => {
266274
// Mark trial as expired
267275
const expireTrial = async (trialId: string) => {
268276
try {
277+
// Check if Appwrite is available
278+
if (!databases) {
279+
return;
280+
}
281+
269282
await databases.updateDocument(
270283
DATABASE_ID,
271284
TRIAL_COLLECTION_ID,
@@ -337,7 +350,6 @@ export const useSecureTrial = () => {
337350
}
338351
}
339352
} catch (error) {
340-
console.error("Error initializing trial:", error);
341353
// Don't block access on error - allow trial to proceed
342354
setTrialStartTime(Date.now());
343355
setTimeRemaining(TRIAL_DURATION_MS);

lib/appwrite/auth.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,23 @@ export interface AuthError {
1414
}
1515

1616
export class AuthService {
17+
// Check if Appwrite is available
18+
private static checkAppwriteAvailable() {
19+
if (!account) {
20+
throw new Error(
21+
"Appwrite is not initialized. Please check your environment variables.",
22+
);
23+
}
24+
}
25+
1726
// Email OTP Authentication
1827
static async createEmailOTPSession(
1928
email: string,
2029
): Promise<{ success: boolean; error?: AuthError; userId?: string }> {
2130
try {
31+
this.checkAppwriteAvailable();
2232
// Create email token (sends 6-digit OTP to email)
23-
const sessionToken = await account.createEmailToken(ID.unique(), email);
33+
const sessionToken = await account!.createEmailToken(ID.unique(), email);
2434
return { success: true, userId: sessionToken.userId };
2535
} catch (error: any) {
2636
return {
@@ -39,8 +49,9 @@ export class AuthService {
3949
otp: string,
4050
): Promise<{ success: boolean; error?: AuthError }> {
4151
try {
52+
this.checkAppwriteAvailable();
4253
// Create session with userId and OTP
43-
await account.createSession(userId, otp);
54+
await account!.createSession(userId, otp);
4455
return { success: true };
4556
} catch (error: any) {
4657
return {
@@ -58,7 +69,8 @@ export class AuthService {
5869
secret: string,
5970
): Promise<{ success: boolean; error?: AuthError }> {
6071
try {
61-
await account.updateMagicURLSession(userId, secret);
72+
this.checkAppwriteAvailable();
73+
await account!.updateMagicURLSession(userId, secret);
6274
return { success: true };
6375
} catch (error: any) {
6476
return {
@@ -77,8 +89,9 @@ export class AuthService {
7789
error?: AuthError;
7890
}> {
7991
try {
92+
this.checkAppwriteAvailable();
8093
const redirectUrl = `${window.location.origin}/auth/callback`;
81-
const url = await account.createOAuth2Session(
94+
const url = await account!.createOAuth2Session(
8295
"google" as any,
8396
redirectUrl,
8497
redirectUrl,
@@ -107,8 +120,9 @@ export class AuthService {
107120
error?: AuthError;
108121
}> {
109122
try {
123+
this.checkAppwriteAvailable();
110124
const redirectUrl = `${window.location.origin}/auth/callback`;
111-
const url = await account.createOAuth2Session(
125+
const url = await account!.createOAuth2Session(
112126
"apple" as any,
113127
redirectUrl,
114128
redirectUrl,
@@ -135,7 +149,8 @@ export class AuthService {
135149
error?: AuthError;
136150
}> {
137151
try {
138-
const user = await account.get();
152+
this.checkAppwriteAvailable();
153+
const user = await account!.get();
139154
return {
140155
user: {
141156
$id: user.$id,
@@ -157,7 +172,8 @@ export class AuthService {
157172
// Sign out
158173
static async signOut(): Promise<{ success: boolean; error?: AuthError }> {
159174
try {
160-
await account.deleteSession("current");
175+
this.checkAppwriteAvailable();
176+
await account!.deleteSession("current");
161177
return { success: true };
162178
} catch (error: any) {
163179
return {
@@ -173,7 +189,8 @@ export class AuthService {
173189
// Check if user is authenticated
174190
static async isAuthenticated(): Promise<boolean> {
175191
try {
176-
await account.get();
192+
this.checkAppwriteAvailable();
193+
await account!.get();
177194
return true;
178195
} catch {
179196
return false;

lib/appwrite/config.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import { Client, Account, Databases } from "appwrite";
22

3-
const client = new Client()
4-
.setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT || "")
5-
.setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID || "");
3+
// Only initialize Appwrite client if we have the required environment variables
4+
// This prevents build-time errors during static generation
5+
const isClientSide = typeof window !== "undefined";
6+
const hasRequiredEnvVars =
7+
process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT &&
8+
process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID;
69

7-
export const account = new Account(client);
8-
export const databases = new Databases(client);
10+
const client =
11+
isClientSide && hasRequiredEnvVars
12+
? new Client()
13+
.setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT!)
14+
.setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID!)
15+
: null;
16+
17+
export const account = client ? new Account(client) : null;
18+
export const databases = client ? new Databases(client) : null;
919

1020
export default client;

0 commit comments

Comments
 (0)