Skip to content

Commit

Permalink
added session helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-Manasse committed Jan 12, 2024
1 parent 6808b6b commit 86c4d30
Showing 1 changed file with 66 additions and 3 deletions.
69 changes: 66 additions & 3 deletions getCurrentSession.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
export async function getCurrentSession(supabase, req) {

import * as dotenv from "dotenv";

import { createClient} from "@supabase/supabase-js";

dotenv.config({ path: 'variables.env' });
const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_KEY
);

export async function getCurrentSession(req) {

const cookies = req.headers.cookie;
if (cookies == null) return { code: 1, error: "cookies error"};
const access_token = cookies.split('; ')[0].split('=')[1];

const refresh_token = cookies.split('; ')[1].split('=')[1];
Expand All @@ -12,7 +25,7 @@ export async function getCurrentSession(supabase, req) {

console.error('session error', sessionError);

throw sessionError;
return { code: 1, error: "supabaseError"};

}

Expand All @@ -21,5 +34,55 @@ export async function getCurrentSession(supabase, req) {
data: { user },

} = await supabase.auth.getUser();
return supabase;
return { code:0, client: supabase};
}

export async function getAnimals(req, res) {
const session = await getCurrentSession(req);
if (session['code'] == 1) res.send(`error in session: ${session['error']}`);
else {
const supabaseInstance = session['client'];

if (error) res.send(`query error in supabase: ${error.message}`);
else {
res.set("Access-Control-Allow-Credentials", "true");
res.set("Access-Control-Allow-Origin", "http://localhost:5173");
res.status(200).json(data);
}
}
}
export async function getDataFrom(req, res, tableName, id = null) {
const {data, error} = await chooseData(supabase, tableName, id);
sendToClient(res, await data, await error);
}

export async function getAuthDataFrom(req, res, tableName, id = null) {
const session = await getCurrentSession(req);
if (session['code'] == 1) res.send(`error in session: ${session['error']}`);
else {
const supabaseInstance = session['client'];
const {data, error} = await chooseData(supabaseInstance, tableName, id);
sendToClient(res, await data, await error, true)
}
}

async function chooseData(supabaseInstance, tableName, id) {
if (id == null) {
return await supabaseInstance.from(tableName).select();
} else {
return await supabaseInstance.from(tableName).select().eq("id", id).single();
}
}
function sendToClient(res, data, error, isAuth = false)
{

if (error) res.send(`query error in supabase: ${error.message}`);
else {
if (isAuth) {
res.set("Access-Control-Allow-Credentials", "true");
res.set("Access-Control-Allow-Origin", "http://localhost:5173");
}
res.status(200).json(data);
}
}

0 comments on commit 86c4d30

Please sign in to comment.