ghoshnirmalya / nextjs-strapi-boilerplate

:art: Boilerplate for building applications using Strapi and Next.js
https://nextjs-strapi-boilerplate.vercel.app
MIT License
312 stars 84 forks source link

Postgres connection problems after fresh repository cloned #4

Closed oskar-gmerek closed 4 years ago

oskar-gmerek commented 4 years ago

Hello, first of all, thank you that you have decided to fill in this lack of a suitable tool for combining strapi authentication with next-auth authorization.

I have such a problem. Freshly cloned from GitHub, my google auth data in .env and when I try to create an account or sign in then I got the error: "Try signing with a different account." (logs from nextjs on the bottom)

Google?NextJS? can't connect to database, but I don't know why? I didn't chance anything... in env: NEXT_PUBLIC_DATABASE_URL=postgres://strapi:strapi@localhost:5432/strapi

by the way... Can you explain how this is working? Does next-auth connect directly to the database? Completely disregarding the Strapi? It should working identical without Strapi?

`event - build page: /api/auth/[...nextauth]

wait - compiling... event - compiled successfully [next-auth][debug][profile_data] { id: 'xxx', email: 'xxx@gmail.com', verified_email: true, name: 'xxx', given_name: 'xxx', family_name: 'xxx', picture: 'https://xxx.x/photo.jpg', locale: 'xx' } [next-auth][debug][oauth_callback_response] { profile: { name: 'xxx', email: 'xxx@gmail.com', image: 'https://xxxx.x/photo.jpg' }, account: { provider: 'google', type: 'oauth', id: 'xxx', refreshToken: undefined, accessToken: 'xxx.xxxx.xxx', accessTokenExpires: null }, OAuthProfile: { id: 'xxxx', email: 'xxx@gmail.com', verified_email: true, name: 'xxx', given_name: 'xxx', family_name: 'xxx', picture: 'https://xxx.x/photo.jpg', locale: 'xx' } } [next-auth][error][adapter_connection_error] error: role "strapi" does not exist at Parser.parseErrorMessage (/Users/xxxx/test-env/nextjs-strapi-boilerplate/frontend/node_modules/pg-protocol/dist/parser.js:278:15) at Parser.handlePacket (/Users/xxx/test-env/nextjs-strapi-boilerplate/frontend/node_modules/pg-protocol/dist/parser.js:126:29) at Parser.parse (/Users/xxx/test-env/nextjs-strapi-boilerplate/frontend/node_modules/pg-protocol/dist/parser.js:39:38) at Socket. (/Users/xxx/test-env/nextjs-strapi-boilerplate/frontend/node_modules/pg-protocol/dist/index.js:8:42) at Socket.emit (events.js:314:20) at addChunk (_stream_readable.js:303:12) at readableAddChunk (_stream_readable.js:279:9) at Socket.Readable.push (_stream_readable.js:218:10) at TCP.onStreamRead (internal/stream_base_commons.js:188:23) { length: 98, severity: 'FATAL', code: '28000', detail: undefined, hint: undefined, position: undefined, internalPosition: undefined, internalQuery: undefined, where: undefined, schema: undefined, table: undefined, column: undefined, dataType: undefined, constraint: undefined, file: 'miscinit.c', line: '607', routine: 'InitializeSessionUserId' } https://next-auth.js.org/errors#adapter_connection_error [next-auth][error][oauth_callback_handler_error] TypeError: Cannot destructure property 'manager' of 'connection' as it is null. at Object. (/Users/xxx/test-env/nextjs-strapi-boilerplate/frontend/node_modules/next-auth/dist/adapters/typeorm/index.js:102:9) at Generator.next () at asyncGeneratorStep (/Users/xxxx/test-env/nextjs-strapi-boilerplate/frontend/node_modules/next-auth/dist/adapters/typeorm/index.js:28:103) at _next (/Users/xxxx/test-env/nextjs-strapi-boilerplate/frontend/node_modules/next-auth/dist/adapters/typeorm/index.js:30:194) at processTicksAndRejections (internal/process/task_queues.js:93:5) https://next-auth.js.org/errors#oauth_callback_handler_error`

ghoshnirmalya commented 4 years ago

@oskar-gmerek Thank you for creating the issue. Please let me know the following so that I can help you:

  1. Is your Strapi server up and running?
  2. Can you visit http://localhost:1337/admin and log into the Admin Panel of Strapi?

How NextAuth works with Strapi

  1. NextAuth is responsible for signing the user in
  2. If the sign in is successful, NextAuth will do an API call to fetch the token from Strapi here.
  3. Strapi will return a JWT token will be injected into the token as well as into the session object of NextAuth.
  4. Every time, a request is made to Strapi from the Front-end Next.js application, the token will be passed in the header.

I hope that this helps. Let me know if you need further clarification.

oskar-gmerek commented 4 years ago

@ghoshnirmalya

  1. Yes, strapi server is up and running
  2. Yes, I can login to admin dashboard without any problems

About how it works together:

  1. So if I understand good. Is possible to use only frontend from the boilerplate and other backend instance? (I mean, there is no changes at the backend?) I asking because I want to use mongodb instead of postgres, and instance of strapi that already exists.
sebacampos commented 4 years ago

Hi @oskar-gmerek I was having the same problem and just solved like this:

If you look closely, you will find this on your console:

[next-auth][error][adapter_connection_error] error: role "strapi" does not exist

That means, no user "strapi" found for DB conection. So in order to solve it, make sure that your env variables matches your DB settings:

NEXT_PUBLIC_DATABASE_URL=postgres://strapi:strapi@localhost:5432/strapi

If you are running on Docker, just make sure that you can connect to the DB with the NEXT_PUBLIC_DATABASE_URL. I personally ended up running the strapi backend locally instead of using Docker. So I set up my local Postgres Server instead of dockerizing it (just for convinience because I already had some other strapi projects in which I use local Postgress Server) ... That means:

  1. Set up my DB env vars for strapi backend under backend/app/.env
  2. cd backend/app && yarn install && yarn develop (Before running this make sure your local DB server is up and running, and you have your DB created as pointed in .env...)

Now once that solved, I was still having "Try signing with a different account" but with a different error log now : (logs from nextjs and strapi on the bottom)

image

[next-auth][debug][typeorm_get_user_by_provider_account_id] [ 'google', 'XXXXXXXXXX' ] <-- My OAuthProfile.id here

[next-auth][error][get_user_by_provider_account_id_error] QueryFailedError: relation "accounts" does not exist

Haven't used NextAuth in any other projects, but basically, TypeORM (NextAuth default DB adapter) couldn't find the 'Accounts' Table in DB... After having a look on NextAuth Documentation I realized that some further config was needed to setup DB. If you are running postgres here you'll find the schema you need: https://next-auth.js.org/schemas/postgres

After updating the schema, I could make it work but still with some tweaks not DB problems so will leave them for another issue maybe. e.g Now I'm dealing with an error on session management but still trying to understand why it is happening: [next-auth][error][jwt_session_error] TypeError: payload must be an object.

For more info on the errors have a look on this Error page on NextAuth documentation which I just found from doing a search on the documentation, because apparently it is not available from the navigation menu 🤷‍♂️

@ghoshnirmalya maybe it would be nice to add some more clarification on this DB settings on the documentation part.

Hope this helps anyone having trouble with the DB Connection.

ghoshnirmalya commented 4 years ago

@sebacampos Thank you for your help. Really appreciate it.

@oskar-gmerek I've created a Pull Request to update the documentation which should fix your issue. Please let me know if you're still facing this issue after following the steps in the readme.md file.

oskar-gmerek commented 4 years ago

@ghoshnirmalya I cloned the repository with the new version, followed exactly as in the directions and ...

Unfortunately, the same error was popping up. Using the @sebacampos hint, I changed the port for postgres into docker-compose and .env

Another error has occurred. Here @sebacampos comes in handy again with his PR #6 , after removing '/graphql' from NEXT_PUBLIC_API_URL in .env I can log in, but is still not working as expected. Im logged, but instead of content I got "Error: Invalid token" Its probably because an user isn't created in strapi. I guess somewhere is missed '/graphql' in path after fixing like that in .env for previous error

ghoshnirmalya commented 4 years ago

@oskar-gmerek Did you append ?synchronize=true to your NEXT_PUBLIC_DATABASE_URL like this?

I've merged https://github.com/ghoshnirmalya/nextjs-strapi-boilerplate/pull/6.

oskar-gmerek commented 4 years ago

@ghoshnirmalya Yes.

ghoshnirmalya commented 4 years ago

@oskar-gmerek Will it be possible for you to upload your code to a repository?

oskar-gmerek commented 4 years ago

@ghoshnirmalya That will be not help at all in my opinion.

But here we go: https://github.com/oskar-gmerek/next-strapi-boilerplate

sebacampos commented 4 years ago

@ghoshnirmalya I cloned the repository with the new version, followed exactly as in the directions and ...

Unfortunately, the same error was popping up. Using the @sebacampos hint, I changed the port for postgres into docker-compose and .env

Another error has occurred. Here @sebacampos comes in handy again with his PR #6 , after removing '/graphql' from NEXT_PUBLIC_API_URL in .env I can log in, but is still not working as expected. Im logged, but instead of content I got "Error: Invalid token" Its probably because an user isn't created in strapi. I guess somewhere is missed '/graphql' in path after fixing like that in .env for previous error

@oskar-gmerek any console log when you get that error?

Since the error is "Error: Invalid token" I would suggest:

oskar-gmerek commented 4 years ago

@sebacampos Strange. I was add console.log('token: ', token); after that line. Saved, after reset it started work correctly. I can login now and see the feeds. After remove that line still working, so I have no idea what happened here.

I will try again from the scratch

sebacampos commented 4 years ago

Often happens 😄. It must have been that NEXT_PUBLIC_DATABASE_URL change that didn't take effect until the server was reset or smth like that

oskar-gmerek commented 4 years ago

@ghoshnirmalya @sebacampos Thanks for help. Thats look like working fine now. I have cloned last version again, follow all steps and is working so I close the issue as it no longer an issue. Thanks one more time.

ghoshnirmalya commented 4 years ago

@sebacampos Thank you for your help. @oskar-gmerek I'm glad that it's working fine for you.

longbkit commented 3 years ago

I have same error but found out the reason turned out to be incorrect postgres connection string (the password has special @ character and even though I have escaped it with %40 it is still not working - while the URI works well with psql command). After many other attempts and document reference (https://next-auth.js.org/configuration/databases) I have decided to change the configuration to json values like below and it works well:


database : {
    type: 'postgres',
    host: process.env.NEXT_PUBLIC_DB_HOST,
    port: process.env.NEXT_PUBLIC_DB_PORT,
    username: process.env.NEXT_PUBLIC_DB_USER,
    password: process.env.NEXT_PUBLIC_DB_PASS,
    database: process.env.NEXT_PUBLIC_DB_NAME
    // synchronize: true
  },

Of course this means I have to add corresponding variables in env.local.

I think digging deeper into the pg library could give the actual reason but since I am so eager to explore nextjs + strapi combination I would accept this solution by now. Hope this helps.