Next.js Discord

Discord Forum

Is this the correct approach to get the bearer access token?

Genio posted this in #help-forum
Open in Discord
I am new to next auth, my back end expects an auth bearer token to allow the request

Right now it works but i was wondering if this is the most efficient approach. I have almost 30 fetch requests so this means i have to add the
    const session = await getServerSession(authOptions)

Every singe time, what can i do better ?

This is one of my fetch methods
'use server'
import { getServerSession } from 'next-auth';
import { baseUrl } from '../constants';
import { unstable_noStore as noStore } from 'next/cache';
import { authOptions } from '@/app/[lang]/(api)/auth/[...nextauth]/route';

const getProviders = async ({
}: {
    token: string;
    status?: string;
}) => {
    const session = await getServerSession(authOptions)
    try {

        const response = await fetch(`${baseUrl}/providers`, {
            headers: {
                Authorization: `Bearer ${session?.accessToken}`
        if (!response.ok) {
            throw new Error('Could not fetch providers');

        const data = await response.json();
        // console.log(data);
        return { data };
    } catch (error: any) {
        return { error };

export default getProviders;

0 Replies