-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
149 additions
and
0 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
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" | ||
} | ||
] | ||
} |
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,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', | ||
}; | ||
} | ||
}; |
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,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, | ||
}; |