Next.js Discord

Discord Forum

Next JS with extern auth and login api

Unanswered
Spectacled bear posted this in #help-forum
Open in Discord
Spectacled bearOP
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: { 
           ....
    },
}

1 Reply

Spectacled bearOP
callbacks: {
        jwt: async ({ token, user, account, profile, isNewUser }) => {
            if (user?.accessToken) {
                token.accessToken = user.accessToken;
            }
            return token;
        },
        session: async ({ session, token }) => {
            if (token?.accessToken)
                session.accessToken = token.accessToken as string;


            const res = await fetch('https://127.0.0.1:8000/auth/token', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    username: session.user?.name,
                }),
            });
            const user = await res.json();
            if (res.ok && user) {
                let accessToken = user.token;
                session.accessToken = accessToken as string;
            }


            return session;
        },
    },


in callbacks i can set the session but i need to password too?

how could i make a secrued request to the api?