Created
October 24, 2024 18:35
-
-
Save bdbergeron/ca638cd3a2ab111f6a7dedac87142f1d to your computer and use it in GitHub Desktop.
Supabase auth FE->BE exchange
This file contains hidden or 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
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; | |
} |
This file contains hidden or 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
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