commit ceb908f71073963dc31e6f31267d1f3397b056fb
parent 341dff11d6f85f031b04a8a74e08bfe01e4d56c9
Author: Andreas Gruhler <andreas.gruhler@adfinis.com>
Date: Fri, 20 Sep 2024 21:51:40 +0200
feat: mv db func to db file
Diffstat:
M | src/api/db.cjs | | | 90 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- |
M | src/api/server.cjs | | | 72 | ++++-------------------------------------------------------------------- |
2 files changed, 91 insertions(+), 71 deletions(-)
diff --git a/src/api/db.cjs b/src/api/db.cjs
@@ -13,8 +13,92 @@ require('dotenv').config({
]
});
+// will use psql environment variables
+// https://github.com/porsager/postgres?tab=readme-ov-file#environmental-variables
+const sql = pg()
+
+require('dotenv').config({
+ // Configure common config files and modes
+ // - https://www.npmjs.com/package/dotenv
+ // - https://vitejs.dev/guide/env-and-mode
+ path: [
+ '.env.local',
+ '.env.development', '.env.development.local',
+ '.env.test', '.env.test.local',
+ '.env.production', '.env.production.local',
+ '.env'
+ ]
+});
+
+async function invalidateToken(token) {
+ try {
+ const users = await sql`
+ update public.judges set
+ token = null,
+ expires_at = null
+ where token = ${token}
+ `
+ return users
+ } catch (error) {
+ console.log('Error occurred in invalidateToken:', error);
+ throw error.errors[0]
+ }
+}
+
+async function lookupToken(token) {
+ try {
+ const users = await sql`
+ select * from public.judges where
+ token = ${token}
+ `
+ return users
+ } catch (error) {
+ console.log('Error occurred in lookupToken:', error);
+ throw error.errors[0]
+ }
+}
+
+async function saveToken(id, token, exp) {
+ try {
+ const users = await sql`
+ update public.judges set
+ token = ${token},
+ expires_at = ${exp}
+ where id = ${id}
+ `
+ return users
+ } catch (error) {
+ console.log('Error occurred in saveToken:', error);
+ throw error.errors[0]
+ }
+}
+
+// Request a signed magic link
+// https://clerk.com/blog/magic-links#building-a-magic-link-system
+async function getUser(email) {
+ try {
+ const users = await sql`
+ select
+ id,
+ firstname,
+ lastname,
+ token,
+ email
+ from public.judges
+ where email = ${email}
+ `
+ return users
+ } catch (error) {
+ console.log('Error occurred in getUser:', error);
+ throw error.errors[0]
+ }
+}
+
+
module.exports = {
- // will use psql environment variables
- // https://github.com/porsager/postgres?tab=readme-ov-file#environmental-variables
- sql: pg()
+ sql,
+ invalidateToken,
+ lookupToken,
+ saveToken,
+ getUser,
}
diff --git a/src/api/server.cjs b/src/api/server.cjs
@@ -99,7 +99,7 @@ server.on('request', async (req, res) => {
const token = search_params.get('token');
// lookup token in the database
- const users = await lookupToken(token);
+ const users = await db.lookupToken(token);
if (users.length < 1) {
throw {message: "Token not found"}
}
@@ -116,7 +116,7 @@ server.on('request', async (req, res) => {
// verify token signature
const v = jwt.verify(token, jwt_secret);
- await invalidateToken(token);
+ await db.invalidateToken(token);
console.log(" Verified token", v);
if (v.email === user.email) {
@@ -167,7 +167,7 @@ server.on('request', async (req, res) => {
input = JSON.parse(b);
console.log(' Link request for:', input.email);
- const users = await getUser(input.email);
+ const users = await db.getUser(input.email);
if (users.length < 1) {
throw new Error("User not found")
}
@@ -184,7 +184,7 @@ server.on('request', async (req, res) => {
console.log("🔗Magic href:", magic_href)
// save token with expiration time
- await saveToken(user.id, token, exp)
+ await db.saveToken(user.id, token, exp)
// send magic link to user
await transport.sendMail({
@@ -241,70 +241,6 @@ function serverError(res, err) {
}));
}
-async function invalidateToken(token) {
- try {
- const users = await db.sql`
- update public.judges set
- token = null,
- expires_at = null
- where token = ${token}
- `
- return users
- } catch (error) {
- console.log('Error occurred in invalidateToken:', error);
- throw error.errors[0]
- }
-}
-
-async function lookupToken(token) {
- try {
- const users = await db.sql`
- select * from public.judges where
- token = ${token}
- `
- return users
- } catch (error) {
- console.log('Error occurred in lookupToken:', error);
- throw error.errors[0]
- }
-}
-
-async function saveToken(id, token, exp) {
- try {
- const users = await db.sql`
- update public.judges set
- token = ${token},
- expires_at = ${exp}
- where id = ${id}
- `
- return users
- } catch (error) {
- console.log('Error occurred in saveToken:', error);
- throw error.errors[0]
- }
-}
-
-// Request a signed magic link
-// https://clerk.com/blog/magic-links#building-a-magic-link-system
-async function getUser(email) {
- try {
- const users = await db.sql`
- select
- id,
- firstname,
- lastname,
- token,
- email
- from public.judges
- where email = ${email}
- `
- return users
- } catch (error) {
- console.log('Error occurred in getUser:', error);
- throw error.errors[0]
- }
-}
-
// Create websocket listeners
// https://github.com/websockets/ws?tab=readme-ov-file#multiple-servers-sharing-a-single-https-server
const wss1 = new ws.WebSocketServer({ noServer: true});