-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move supabase query in verify-card to nextjs api route, extract redir…
…ect url to env var
- Loading branch information
1 parent
34d0e3a
commit 2a7f994
Showing
4 changed files
with
86 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { HibiscusSupabaseClient } from '@hibiscus/hibiscus-supabase-client'; | ||
import { NextApiHandler } from 'next'; | ||
import { container } from 'tsyringe'; | ||
|
||
const handler: NextApiHandler = async (req, res) => { | ||
const { userId } = req.query; | ||
const userIdString = userId.toString(); | ||
|
||
if (req.method === 'PUT') { | ||
const supabase = container.resolve(HibiscusSupabaseClient); | ||
supabase.setOptions({ useServiceKey: true }); | ||
|
||
const resEmail = await supabase | ||
.getClient() | ||
.from('user_profiles') | ||
.select('email') | ||
.eq('user_id', userIdString); | ||
|
||
if (resEmail.error != null) { | ||
return res.status(500).json({ message: resEmail.error.message }); | ||
} | ||
|
||
if (resEmail.data.length === 0) { | ||
return res.status(400).json({ message: 'Invalid user ID' }); | ||
} | ||
|
||
const email = resEmail.data[0].email as string; | ||
|
||
const resRole = await supabase | ||
.getClient() | ||
.from('user_invites') | ||
.select('role') | ||
.eq('email', email); | ||
|
||
if (resRole.error != null) { | ||
return res.status(500).json({ message: resRole.error.message }); | ||
} | ||
|
||
if (resRole.data.length === 0) { | ||
return res.status(200).json({ | ||
message: 'User does not have special role; no role change executed', | ||
}); | ||
} | ||
|
||
const role = resRole.data[0].role as number; | ||
|
||
const resUpdate = await supabase | ||
.getClient() | ||
.from('user_profiles') | ||
.update({ role }) | ||
.eq('user_id', userId); | ||
|
||
if (resUpdate.error != null) { | ||
return res.status(500).json({ message: resUpdate.error.message }); | ||
} | ||
|
||
return res.status(200).json({ message: 'Success' }); | ||
} else { | ||
return res.status(405).json({ message: 'Invalid request type.' }); | ||
} | ||
}; | ||
|
||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SUPABASE_SERVICE_KEY= | ||
SUPABASE_URL= | ||
INVITE_REDIRECT_URL= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters