How to get access token in server? #6723
Answered
by
GaryAustin1
hallee9000
asked this question in
Questions
|
How can I get the user's access token on the server? Or, how can I set the file's owner when uploading files to storage on the server? |
Answered by
GaryAustin1
May 7, 2022
Replies: 2 comments 11 replies
|
Not 100% clear on what you are asking, but most likely you need to pass the users jwt to the server and then use supabase.auth.setAuth(jwt) on the server, then make your storage call. This thread might have some useful info: |
9 replies
Answer selected by
hallee9000
|
if anyone comes here in the future type StateType = {
session: Session | null;
status: "loading" | "authenticated" | "unauthenticated";
};
export const sessionState = atom<StateType>({
session: null,
status: "loading",
});
export default function NextAuthProvider({ children }: PropsWithChildren) {
const [_, setSession] = useAtom(sessionState);
const [cookie, setSessionCookie, deleteSessionCookie] = useCookie("session");
useEffect(() => {
supabaseclient.auth
.getSession()
.then(({ data, error }) => {
setSessionCookie(data.session?.access_token || "")
setSession({
session: data.session,
status: !data.session ? "unauthenticated" : "authenticated",
});
supabaseclient.auth.onAuthStateChange((event, session) => {
setSessionCookie(session?.access_token || "")
if (event === "SIGNED_OUT") deleteSessionCookie()
setSession({
session: event == "SIGNED_OUT" ? null : session,
status: event == "SIGNED_OUT" ? "unauthenticated" : "authenticated",
});
});
})
.catch(() => {
deleteSessionCookie()
setSession({
session: null,
status: "unauthenticated",
});
});
}, []);
return <>{children}</>;
}state hook "use client";
import { sessionState } from "./Provider";
import { useAtom } from "jotai";
function useSessionState() {
const [s] = useAtom(sessionState);
return s;
}
export default useSessionState; |
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Not 100% clear on what you are asking, but most likely you need to pass the users jwt to the server and then use supabase.auth.setAuth(jwt) on the server, then make your storage call.
This thread might have some useful info:
#1094