diff --git a/linkPiToUser/function.json b/linkPiToUser/function.json new file mode 100644 index 0000000..5f944bd --- /dev/null +++ b/linkPiToUser/function.json @@ -0,0 +1,16 @@ +{ + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "methods": ["post"] + }, + { + "type": "http", + "direction": "out", + "name": "res" + } + ] +} diff --git a/linkPiToUser/index.js b/linkPiToUser/index.js new file mode 100644 index 0000000..0e94107 --- /dev/null +++ b/linkPiToUser/index.js @@ -0,0 +1,86 @@ +require('dotenv').config(); +const { createClient } = require('@supabase/supabase-js'); + +const { SUPABASE_KEY } = process.env; + +const supabase = createClient( + 'https://itqtginklzbjrhusppwt.supabase.co', + SUPABASE_KEY +); + +module.exports = async function (context, req) { + try { + if (req.method !== 'POST') { + context.res = { + status: 405, + body: 'Method Not Allowed', + }; + return; + } + + if (!req.body) { + context.res = { + status: 400, + body: 'Body required', + }; + return; + } + + const { linkedHwid } = req.body; + + if (!linkedHwid) { + context.res = { + status: 400, + body: 'Invalid body', + }; + return; + } + + // Get the ID of the user who made the request + const { result, errorCode, user } = await supabase.auth.api.getUserByCookie( + req.headers.cookie + ); + + if (!result) { + context.res = { + status: 401, + body: errorCode, + }; + return; + } + + console.log('User: ', user); + + // First try update the garage row with hwid as identifier and edit the ip_address + const { data, error } = await supabase + .from('garages') + .update({ ip_address: ip }) + .match({ hwid: hwid }); + + if (error) { + const { data, error } = await supabase + .from('garages') + .insert({ hwid: hwid, ip_address: ip }); + + if (error) { + context.res = { + status: 500, + body: 'Error registering with HWID', + }; + return; + } + } + + context.res = { + status: 200, + body: 'Pi linked', + }; + } catch (error) { + console.error('Error:', error); + + context.res = { + status: 500, + body: 'Error linking', + }; + } +}; diff --git a/supabaseAuth.js b/supabaseAuth.js new file mode 100644 index 0000000..7e0519c --- /dev/null +++ b/supabaseAuth.js @@ -0,0 +1,47 @@ +const { createClient } = require('@supabase/supabase-js'); +require('dotenv').config(); + +const SUPABASE_KEY = + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Iml0cXRnaW5rbHpianJodXNwcHd0Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3MTQ1NDU2MDgsImV4cCI6MjAzMDEyMTYwOH0.sYFd9abYQhP7zOXCCeddULNsn6ViA7XEKwyZGZuDSQM'; + +const supabase = createClient( + 'https://itqtginklzbjrhusppwt.supabase.co', + SUPABASE_KEY +); + +const verifySession = async (token) => { + let errorCode = ''; + let result = false; + console.log('Verifying session...'); + + // console.log("Headers: ", req.headers); + + // Get the token from the request + if (!token) { + console.log('No token provided'); + errorCode = 'Unauthorized - No token provided'; + return { result, errorCode, user: null }; + } + + // Get the user associated with the token, in turn verifying the token + const { user: data, error } = await supabase.auth.getUser(token); + + // console.log("Data: ", data); + // console.log("Error: ", error); + + // If the token is invalid, return an error + if (error) { + errorCode = 'Unauthorized'; + return { result, errorCode, user: null }; + } + + result = true; + // If the token is valid, continue to the next middleware + console.log('Session verified'); + errorCode = 'Authorized'; + return { result, errorCode, user }; +}; + +module.exports = { + verifySession, +};