Skip to content

Instantly share code, notes, and snippets.

@bdbergeron
Created October 24, 2024 18:35
Show Gist options
  • Save bdbergeron/ca638cd3a2ab111f6a7dedac87142f1d to your computer and use it in GitHub Desktop.
Save bdbergeron/ca638cd3a2ab111f6a7dedac87142f1d to your computer and use it in GitHub Desktop.
Supabase auth FE->BE exchange
import { createClient, type Session } from "@supabase/supabase-js";
async onAuthenticate({ token }: onAuthenticatePayload) {
const session = JSON.parse(token) as Pick<
Session,
"access_token" | "refresh_token"
>;
const {
data: { user },
error,
} = await this.client.auth.setSession(session);
if (error) {
console.error(error);
throw new Error("Not authenticated.");
}
return user;
}
export const getAuthToken = cache(async () => {
const supabase = await supabaseClient();
const {
data: { session },
error,
} = await supabase.auth.getSession();
if (error) {
console.error(error);
return { error };
}
if (!session) {
return { token: null };
}
const { access_token, refresh_token } = session;
const token = JSON.stringify({
access_token,
refresh_token,
});
return { token };
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment