Skip to content

Commit

Permalink
added linking
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick6464 committed May 10, 2024
1 parent a65b626 commit cdc9a7a
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
16 changes: 16 additions & 0 deletions linkPiToUser/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": ["post"]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
86 changes: 86 additions & 0 deletions linkPiToUser/index.js
Original file line number Diff line number Diff line change
@@ -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',
};
}
};
47 changes: 47 additions & 0 deletions supabaseAuth.js
Original file line number Diff line number Diff line change
@@ -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,
};

0 comments on commit cdc9a7a

Please sign in to comment.