@@ -3,7 +3,7 @@ import { render } from "@react-email/components";
33import { db } from "@/db" ;
44import { FormStep , Obj } from "@/lib/types/questions" ;
55import { Trigger } from "@/lib/types/forms" ;
6- import { createClient } from "@/lib/utils/supabase/server " ;
6+ import { getSessionUser , requireUser , isAdmin } from "@/lib/utils/auth " ;
77import { JSONValidationToZod } from "@/lib/utils/forms/helpers" ;
88import { sendEmail } from "@/lib/utils/forms/email" ;
99import { SubmissionTemplate } from "@/components/forms/emailTemplates/submissionTemplate" ;
@@ -15,12 +15,13 @@ export async function submitApplication({
1515 formId : bigint ;
1616 otherUser ?: string ;
1717} ) {
18- const supabase = createClient ( ) ;
19- const userId = otherUser || ( await supabase . auth . getUser ( ) ) . data ?. user ?. id ;
20- const { data , error } = await supabase . auth . getUser ( ) ;
21- if ( ! data . user || error ) {
22- return null ;
18+ const user = await requireUser ( ) ;
19+ // otherUser lets admins submit on behalf of an applicant (support flow) ;
20+ // regular users may only ever act as themselves.
21+ if ( otherUser && ! ( await isAdmin ( user . id ) ) ) {
22+ throw new Error ( "Unauthorized" ) ;
2323 }
24+ const userId = otherUser ? otherUser : user . id ;
2425 const resPromise = db . submissions . findUnique ( {
2526 where : {
2627 user_id_form_id : {
@@ -121,7 +122,7 @@ export async function submitApplication({
121122 await sendEmail ( {
122123 from : "no-reply@ubclaunchpad.com" ,
123124 fromName : "No-reply UBC Launch Pad" ,
124- to : data . user . email ! . toString ( ) ,
125+ to : user . email ! . toString ( ) ,
125126 subject : `${ form . title } - Form Submitted` ,
126127 html : template ,
127128 cc : appEmail ?. email as string ,
@@ -138,12 +139,11 @@ export async function updateApplication({
138139 formId : bigint ;
139140 otherUser ?: string ;
140141} ) {
141- const supabase = createClient ( ) ;
142- const userId = otherUser || ( await supabase . auth . getUser ( ) ) . data ?. user ?. id ;
143- const { data, error } = await supabase . auth . getUser ( ) ;
144- if ( ! data . user || error ) {
145- return null ;
142+ const user = await requireUser ( ) ;
143+ if ( otherUser && ! ( await isAdmin ( user . id ) ) ) {
144+ throw new Error ( "Unauthorized" ) ;
146145 }
146+ const userId = otherUser ? otherUser : user . id ;
147147
148148 const res = await db . submissions . findUnique ( {
149149 where : {
@@ -176,16 +176,15 @@ export async function getUserApplication({
176176 formId : bigint ;
177177 includeApp ?: boolean ;
178178} ) {
179- const supabase = createClient ( ) ;
180- const { data, error } = await supabase . auth . getUser ( ) ;
181- if ( ! data . user || error ) {
179+ const user = await getSessionUser ( ) ;
180+ if ( ! user ) {
182181 return null ;
183182 }
184183 return db . submissions . findUnique ( {
185184 where : {
186185 user_id_form_id : {
187186 form_id : formId ,
188- user_id : data . user . id ,
187+ user_id : user . id ,
189188 } ,
190189 } ,
191190 include : {
@@ -219,16 +218,12 @@ function validateFormAnswers({
219218}
220219
221220export async function startApplication ( { formId } : { formId : bigint } ) {
222- const supabase = createClient ( ) ;
223- const { data, error } = await supabase . auth . getUser ( ) ;
224- if ( ! data . user || error ) {
225- return null ;
226- }
221+ const user = await requireUser ( ) ;
227222 const res = await db . submissions . findUnique ( {
228223 where : {
229224 user_id_form_id : {
230225 form_id : formId ,
231- user_id : data . user . id ,
226+ user_id : user . id ,
232227 } ,
233228 } ,
234229 } ) ;
@@ -240,7 +235,7 @@ export async function startApplication({ formId }: { formId: bigint }) {
240235 where : {
241236 user_id_form_id : {
242237 form_id : formId ,
243- user_id : data . user . id ,
238+ user_id : user . id ,
244239 } ,
245240 } ,
246241 data : {
@@ -250,7 +245,7 @@ export async function startApplication({ formId }: { formId: bigint }) {
250245 }
251246 return db . submissions . create ( {
252247 data : {
253- user_id : data . user . id ,
248+ user_id : user . id ,
254249 form_id : formId ,
255250 status : "pending" ,
256251 } ,
0 commit comments