MIF_E31221222/sigap-website/app/(pages)/(auth)/action.ts

189 lines
6.6 KiB
TypeScript

"use server";
import { redirect } from "next/navigation"
import { getInjection } from "@/di/container"
import { revalidatePath } from "next/cache"
import { InputParseError, NotFoundError } from "@/src/entities/errors/common"
import { AuthenticationError, UnauthenticatedError } from "@/src/entities/errors/auth"
import { createClient } from "@/app/_utils/supabase/server"
export async function signIn(formData: FormData) {
const instrumentationService = getInjection("IInstrumentationService")
return await instrumentationService.instrumentServerAction("signIn", {
recordResponse: true
},
async () => {
const email = formData.get("email")?.toString()
try {
const signInController = getInjection("ISignInController")
return await signInController({ email })
// if (email) {
// redirect(`/verify-otp?email=${encodeURIComponent(email)}`)
// }
} catch (err) {
if (err instanceof InputParseError) {
return { error: err.message }
}
if (err instanceof AuthenticationError) {
return { error: "Invalid credential. Please try again." }
}
if (err instanceof UnauthenticatedError || err instanceof NotFoundError) {
return {
error: 'User not found. Please tell your admin to create an account for you.',
};
}
const crashReporterService = getInjection('ICrashReporterService');
crashReporterService.report(err);
return {
error:
'An error happened. The developers have been notified. Please try again later.',
};
}
})
}
// export async function signUp(formData: FormData) {
// const instrumentationService = getInjection("IInstrumentationService")
// return await instrumentationService.instrumentServerAction("signUp", { recordResponse: true }, async () => {
// try {
// const data = Object.fromEntries(formData.entries())
// const signUpController = getInjection("ISignUpController")
// await signUpController(data)
// } catch (err) {
// if (err instanceof InputParseError) {
// return { error: err.message, success: false }
// }
// const crashReporterService = getInjection("ICrashReporterService")
// crashReporterService.report(err)
// return {
// error: "An error occurred during sign up. Please try again later.",
// success: false,
// }
// }
// })
// }
export async function signOut() {
const instrumentationService = getInjection("IInstrumentationService")
return await instrumentationService.instrumentServerAction("signOut", {
recordResponse: true
}, async () => {
try {
const signOutController = getInjection("ISignOutController")
await signOutController()
// revalidatePath("/")
// redirect("/sign-in") // Updated to match your route
return { success: true }
} catch (err) {
// if (err instanceof AuthenticationError) {
// return {
// error: "An error occurred during sign out. Please try again later.",
// }
// }
const crashReporterService = getInjection("ICrashReporterService")
crashReporterService.report(err)
return {
error: "An error occurred during sign out. Please try again later.",
}
}
})
}
export async function verifyOtp(formData: FormData) {
const instrumentationService = getInjection("IInstrumentationService")
return await instrumentationService.instrumentServerAction("verifyOtp", {
recordResponse: true
}, async () => {
try {
const email = formData.get("email")?.toString()
const token = formData.get("token")?.toString()
const verifyOtpController = getInjection("IVerifyOtpController")
await verifyOtpController({ email, token })
// redirect("/dashboard")
return { success: true }
} catch (err) {
if (err instanceof InputParseError || err instanceof AuthenticationError) {
return { error: err.message }
}
const crashReporterService = getInjection("ICrashReporterService")
crashReporterService.report(err)
return {
error: "An error occurred during OTP verification. Please try again later.",
}
}
})
}
export async function sendMagicLink(formData: FormData) {
const instrumentationService = getInjection("IInstrumentationService")
return await instrumentationService.instrumentServerAction("sendMagicLink", {
recordResponse: true
}, async () => {
try {
const email = formData.get("email")?.toString()
const sendMagicLinkController = getInjection("ISendMagicLinkController")
await sendMagicLinkController({ email })
return { success: true }
} catch (err) {
if (err instanceof InputParseError) {
return { error: err.message }
}
const crashReporterService = getInjection("ICrashReporterService")
crashReporterService.report(err)
return {
error: "An error occurred during sending magic link. Please try again later.",
}
}
})
}
export async function sendPasswordRecovery(formData: FormData) {
const instrumentationService = getInjection("IInstrumentationService")
return await instrumentationService.instrumentServerAction("sendPasswordRecovery", {
recordResponse: true
}, async () => {
try {
const email = formData.get("email")?.toString()
const sendPasswordRecoveryController = getInjection("ISendPasswordRecoveryController")
await sendPasswordRecoveryController({ email })
return { success: true }
} catch (err) {
if (err instanceof InputParseError) {
return { error: err.message }
}
const crashReporterService = getInjection("ICrashReporterService")
crashReporterService.report(err)
return {
error: "An error occurred during sending password recovery. Please try again later.",
}
}
})
}