#Next JS with extern auth and login api

2 messages · Page 1 of 1 (latest)

gray island
#

I need help with the Next auth Options.

I need inside a component a token which i need for hooks which currently should use

        const response: AxiosResponse = await axios.get('https://localhost:8000/api/metadata', {
            headers: {
                Authorization: `Bearer ${token}`,
            },
        });

in the options i was not able to update the session user with a token..

export const options: NextAuthOptions = {
    providers: [
        GitHubProvider({
            clientId: process.env.GITHUB_CLIENT_ID as string,
            clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
        }),
        CredentialsProvider({
            name: 'Credentials',
            credentials: {
                username: { label: 'Username', type: 'text' },
                password: { label: 'Password', type: 'password' },
            },
            async authorize(credentials) {
                let credentials_mapped = {
                    username: credentials?.username,
                    password: credentials?.password,
                };

                const res = await fetch('https://127.0.0.1:8000/auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(credentials_mapped),
                });
                const user = await res.json();
                if (res.ok && user) {
                    let token = user.token;
                    user.accessToken = token;
                    return user;
                }
                console.error('error', user);
                return null;
            }
        }),
    ],
    secret: process.env.SECRET,
    callbacks: { 
           ....
    },
}
distant summitBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

      🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in <id:customize>

      ✅ You can mark a message as the answer for your post with `Right click -> Apps -> Mark Solution`
      (if you don't see the option, try refreshing Discord with Ctrl + R)