project_next_14_ai_prompt_sharing icon indicating copy to clipboard operation
project_next_14_ai_prompt_sharing copied to clipboard

Acces Denied Issues

Open tynoschuck opened this issue 1 year ago • 5 comments

Hello there! Nothing new here, same issue as already discussed, I tried everything already mentioned in the other posts, and I came to a dead end. Here's my user.js Screenshot 2023-12-03 at 14 10 49

route.js Screenshot 2023-12-03 at 14 11 17

and database Screenshot 2023-12-03 at 14 14 54

Hope that someone would be able to help. Thank in advance!

tynoschuck avatar Dec 03 '23 14:12 tynoschuck

  1. copy the code below and past in your [...nextauth] router.js

import NextAuth from 'next-auth'; import GoogleProvider from 'next-auth/providers/google';

import User from '@models/user'; import { connectToDB } from '@utils/database';

const handler = NextAuth({ providers: [ GoogleProvider({ clientId: process.env.GOOGLE_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET, }) ], callback: { async session({ session }) {

  const sessionUser = await User.findOne({ email: session.user.email });
  session.user.id = sessionUser._id.toString();

  return session
},
async signIn({ user, account, profile, email, credentials }) {
  try {
    await connectToDB();
    const userExists = await User.findOne({ email: profile?.email });
    // if not, create a new document and save user in MongoDB
    if (userExists === null) {
      await User.create({
        email: profile?.email,
        username: profile?.name.replace(/\s+/g, '').toLowerCase(),
        image: user.image
      })
    }

    return true
  } catch (error) {
    console.log(error);
    return false
  }
  
} 

} })

export { handler as GET, handler as POST }

  1. in your next.config.js copy the code and paste below // next.config.js const nextConfig = { images: { domains: ['lh3.googleusercontent.com'], }, };

module.exports = nextConfig;

aabu960 avatar Dec 17 '23 21:12 aabu960

Thanks for the reply! Unfortunately changing callbacks to callback does not solve the problem, I do not receive errors but it does nopt connect to the database as it should.

tynoschuck avatar Dec 19 '23 09:12 tynoschuck

Thanks for the reply! Unfortunately changing callbacks to callback does not solve the problem, I do not receive errors but it does nopt connect to the database as it should.

same here stuck at this issue for ages now. I have tried everything but no luck so far.

Pannu786 avatar Dec 23 '23 16:12 Pannu786

I fixed it following others topic.... Changing the regex at user.js (/models), to allow less than 8 characters... and removing the brackets on MONGODB_URI when changing the password at .env file

shuuwolf avatar Dec 26 '23 17:12 shuuwolf

This issue is also caused by any Google account that has non-latin letters in the user name! The solution is to completely disable limitations on the user name at at user.js (/models). This will allow duplicate user names, but I have no problem with that.

I've spent 2 hours on this before solving it... Thanks a lot for the help, shuuwolf

I fixed it following others topic.... Changing the regex at user.js (/models), to allow less than 8 characters... and removing the brackets on MONGODB_URI when changing the password at .env file

nimroddanielmaayan avatar Dec 28 '23 10:12 nimroddanielmaayan