70 lines
2.4 KiB
TypeScript
70 lines
2.4 KiB
TypeScript
'use server';
|
|
|
|
import { auth } from "@/lib/better-auth/auth";
|
|
import { inngest } from "@/lib/inngest/client";
|
|
import { headers } from "next/headers";
|
|
|
|
export const signUpWithEmail = async ({ email, password, fullName, country, investmentGoals, riskTolerance, preferredIndustry }: SignUpFormData) => {
|
|
try {
|
|
const response = await auth.api.signUpEmail({ body: { email, password, name: fullName } })
|
|
|
|
if (response) {
|
|
try {
|
|
console.log('📤 Sending Inngest event: app/user.created for', email);
|
|
await inngest.send({
|
|
name: 'app/user.created',
|
|
data: { email, name: fullName, country, investmentGoals, riskTolerance, preferredIndustry }
|
|
});
|
|
console.log('✅ Inngest event sent successfully');
|
|
} catch (error) {
|
|
console.error('❌ Failed to send Inngest event:', error);
|
|
// Don't fail signup if email fails
|
|
}
|
|
}
|
|
|
|
return { success: true, data: response }
|
|
} catch (e) {
|
|
console.log('Sign up failed', e)
|
|
return { success: false, error: 'Sign up failed' }
|
|
}
|
|
}
|
|
|
|
export const signInWithEmail = async ({ email, password }: SignInFormData) => {
|
|
try {
|
|
const response = await auth.api.signInEmail({ body: { email, password } })
|
|
|
|
// Update lastActiveAt
|
|
if (response) {
|
|
try {
|
|
// Dynamic import or ensure path is correct
|
|
const { connectToDatabase } = await import("@/database/mongoose");
|
|
const mongoose = await connectToDatabase();
|
|
const db = mongoose.connection.db;
|
|
if (db) {
|
|
await db.collection('user').updateOne(
|
|
{ email },
|
|
{ $set: { lastActiveAt: new Date() } }
|
|
);
|
|
}
|
|
} catch (err) {
|
|
console.error("Failed to update lastActiveAt", err);
|
|
}
|
|
}
|
|
|
|
return { success: true, data: response }
|
|
} catch (e) {
|
|
console.log('Sign in failed', e)
|
|
return { success: false, error: 'Sign in failed' }
|
|
}
|
|
}
|
|
|
|
export const signOut = async () => {
|
|
try {
|
|
await auth.api.signOut({ headers: await headers() });
|
|
} catch (e) {
|
|
console.log('Sign out failed', e)
|
|
return { success: false, error: 'Sign out failed' }
|
|
}
|
|
}
|
|
|