Skip to content

Commit

Permalink
setting auth session for query
Browse files Browse the repository at this point in the history
  • Loading branch information
t0nkisa committed Jan 10, 2024
1 parent 104e348 commit 285e3f7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
28 changes: 25 additions & 3 deletions adapters/supabaseAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@ const supabase = createClient(
process.env.SUPABASE_KEY
);

export async function getEventsData() {
const { data, error } = await supabase.from('events').select();
export async function getEventsData(access_token, refresh_token) {
const {sessionData, sessionError} = supabase.auth.setSession({
access_token,
refresh_token,
})
if (sessionError) {
console.error('session error', sessionError);
throw sessionError;
}
const {
data: { user },
} = await supabase.auth.getUser();
const { data, error } = await supabase.from('new_events').select();
if (error) {
console.error('query error', error);
throw error;
}
return data;
return {data, user};
}

export async function getEventIdData(eventId){
Expand All @@ -29,5 +40,16 @@ export async function getEventIdData(eventId){
throw error;
}

return data;
}

export async function postEvent(supabase) {
const { data, error } = await supabase.from('new_events').insert({

}).select();
if (error) {
console.error('query error', error);
throw error;
}
return data;
}
34 changes: 32 additions & 2 deletions controllers/eventsViaSupabase.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { getEventsData, getEventIdData } from "../adapters/supabaseAdapter.js";
import { getEventsData, getEventIdData, postEvent } from "../adapters/supabaseAdapter.js";
import { createClient } from '@supabase/supabase-js';

export async function getEvents(req, res) {
try {
const data = await getEventsData();
const cookies = req.headers.cookie;
const access_token = cookies.split('; ')[0].split('=')[1];
const refresh_token = cookies.split('; ')[1].split('=')[1];
const data = await getEventsData(access_token, refresh_token);
console.log(data);
res.set("Access-Control-Allow-Credentials", "true");
res.set("Access-Control-Allow-Origin", "http://localhost:5173");
res.status(200).json(data);
} catch (err) {
res.send(`error in viaSupabase: ${err}`);
Expand All @@ -18,3 +25,26 @@ export async function getEventId(req, res) {
res.status(500).send(error.message);
}
};

export async function postNewEvent(req, res) {
try {
const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_KEY,
{
global: {
headers: { "set-cookies": req.headers.cookie },
},
}
);
const {
data: { user },
} = await supabase.auth.getUser();

const newEvent = await postEvent(supabase);
res.status(200).json(newEvent);
} catch (error) {
res.status(500).send(error.message);
}

}
2 changes: 2 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ router.options('/events', (req, res, next) => {
res.header({
allow: 'GET, POST, OPTIONS',
'Content-type': 'application/json',
'Access-Control-Allow-Credentials': "true",
"Access-Control-Allow-Origin": "http://localhost:5173",
Data: Date.now(),
'Content-length': 0,
});
Expand Down

0 comments on commit 285e3f7

Please sign in to comment.