I need some help to get me started with NextAuth.js
Following this guide I am trying to add Google authentication with existing Next.js app. I registered an app via Firebase and got GOOGLE_ID and GOOGLE_SECRET which I set in the app. Here is my [...nextauth].js:
import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";
export const authOptions = {
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
}),
// ...add more providers here
],
};
export default NextAuth(authOptions);
.env.local file:
GOOGLE_ID=xxx
GOOGLE_SECRET=xxx
NEXTAUTH_URL=http://localhost:3000
Then in the component I import:
import { signIn, signOut, useSession } from "next-auth/react";
Then I call it like this:
<div className="link" onClick={signIn}>
<p>Hello Alex Smith</p>
<p className="font-extrabold md:text-sm">Account & Lists</p>
</div>
Then weird stuff begins. Then I click on the div, I get to see button: Login With Google and URL is:http://localhost:3000/api/auth/signin?callbackUrl=http%3A%2F%2Flocalhost%3A3000%2F
When I click on it, I get Internal Server Error and URL is:http://localhost:3000/api/auth/signin/google
Also in terminal I see: ReferenceError: ReadableStream is not defined popping up. Don't know if that is relevant.
Strangely, in Google console I see no trace of app registered in Firebase.
I tried creating OAuth client manually and getting different set of credentials, switching Google account - to no avail.
I always hit that error and cannot go further. They say NextAuth is super easy to integrate, however...
Found the cause was do do with version of Node.
Updated it to the latest version and that fixed the issue.
Now can can log in.
Related
I have a two basic next apps that uses next-auth for its authentication along with keycloak provider.
i've been trying to use the feature of SSO (single sign on) that keyckoak provides, so i have a website running on http://localhost:3000/ and another one on http://localhost:3001/, and everytime i try to login using the second app, it redirects me to localhost 3000, not localhost 3001, which is a weird behavior, let me show you some code.
api/auth/[...nextauth].ts
(note: i'm using same realm and clientId for both apps)
import NextAuth from "next-auth/next";
import KeycloackProvider from "next-auth/providers/keycloak";
export default NextAuth({
debug: true,
providers: [
KeycloackProvider({
clientId: "react-client-1",
clientSecret: "react-auth",
issuer: "http://localhost:8080/realms/react-auth",
enter image description here }),
],
});
Function to sign in
here, the callbackUrl just doesn't work, no matter what i put there, it's just not going to redirect me to that url
const handleKeycloackSignIn = (): void => {
signIn("keycloak", {
callbackUrl: "http://localhost:3001/",
});
};
next-auth cookies for redirecting on localhost:3001
photo #1
Keycloack config
photo #2
What could be the reason for always redirecting to localhost:3000 ? is it some next-auth config that i'm missing or, maybe is related to keycloak ?
How can i redirect to wherever i want after a successful login/logout
I've tried hardcore google search around this topic, nothing really related to my specific issue.
You need to add some variables that next-auth need to your .env file, something like this.
NEXTAUTH_URL=http://localhost:3000/
NEXTAUTH_SECRET=PRUEBA
this provides the url to redirect and a secret to your jwt token
This is the logs
I have built an amazon clone and when i deployed it to vercel the signin with google is not working, and i got this error:
Server error
There is a problem with the server configuration.
Check the server logs for more information.
i have added my environement variables
Kindly share screenshot of the browser console so we can get some more clarity on the error.
I had also faced a similar error once, I'll tell you how I resolved it. It may work out for you as well.
Check in your .env file if you have changed the callback URL to the deployment url and not localhost
You may need to generate a Nextauth secret key. You can easily do so by running the below command.
openssl rand -base64 32
This will give you a alphanumeric string. Copy and paste that as NEXTAUTH_SECRET = 'generated-value' in the .env file
Then in your pages/api/auth/[...nextauth.js] file, add this secret as well like shown below.
import NextAuth from "next-auth"
import GoogleProvider from "next-auth/providers/google"
export default NextAuth({
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
secret: process.env.NEXTAUTH_SECRET
],
})
If this works out it's good, else you have to share your console log's screenshot so we can debug further if needed.
I have resolved this issue , if added uri on the cridentials of my project on google cloud platform and i added also a NEXTAUTH_SERET and redeployed the build
THank you everyone
hi I had the same problem and I added NEXTAUTH_SECRET and it show an error with secret: process.env.NEXTAUTH_SECRET
I followed some simple instructions to set-up next-auth and cognito as per https://www.youtube.com/watch?v=U4hEflgix9c&t=8s&ab_channel=EvanDoesTech
It works on localhost but then I tried to deploy to Vercel and I get 404 error when clicking sign-in -> [vercel website domain]/api/auth/providers returns 404. This is the error message: ["providers",{"name":"SyntaxError","message":"Unexpected token < in JSON at position 0","stack":"SyntaxError: Unexpected token < in JSON at position 0"}]
On Vercel, I've manually configured env variables including setting NEXTAUTH_URL to https://[my custom domain name].vercel.app
This is my code so far (pages/api/auth/[...nextauth].js) and I used next-auth V3 to keep it consistent with the YouTube tutorial:
import NextAuth from 'next-auth';
import Providers from 'next-auth/providers';
export default NextAuth({
providers: [
Providers.Cognito({
clientId: process.env.COGNITO_CLIENT_ID,
clientSecret: process.env.COGNITO_CLIENT_SECRET,
domain: process.env.COGNITO_DOMAIN,
}),
],
secret: process.env.NEXTAUTH_SECRET,
});
I am trying to deploy a NextJs application to vercel, I am using next auth to authenticate via discord.
Locally it is working fine. The right callback URL's are all configured and NEXT_AUTH_URL is configured too.
export default NextAuth({
providers: [
DiscordProvider({
clientId: process.env.DISCORD_CLIENT_ID,
clientSecret: process.env.DISCORD_CLIENT_SECRET
})
],
secret: process.env.SECRET,
jwt: {
signingKey: process.env.JWT_SIGNING_PRIVATE_KEY,
},
database: process.env.DATABASE_URL,
adapter: PrismaAdapter(prisma),
})
When trying to log in on the deployed site, however, I am receiving a 500 CLIENT_FETCH_ERROR.
Maybe it is something with keys in enviroment:
use keys in this format : process.env.NEXT_PUBLIC_JWT_SIGNING_PRIVATE_KEY
I have found what it was.
I had made changes to the prisma schema before it stopped working.
Vercel had cached the old prisma client, which was generated with the old schema.
I had to update my build settings and perform a redeployment to clear the cache and regenerate the prisma client to be up to date.
This is the build command I configured on vercel, after that I simply had to trigger a redeployment.
prisma generate && next build
I wanted to receive data to my site on React through the API.I did everything as stated in the documentation, performed the installation of npm install --save woocommerce-api, created the object with parameters as in the documentation http://woocommerce.github.io/woocommerce-rest-api-docs/?javascript#pagination
import React, { Component } from 'react';
import '../App.css';
import WooCommerceAPI from 'woocommerce-api';
class Goods extends Component {
WooCommerce = new WooCommerceAPI({
url: 'http://portland.com/wp/', // Your store URL
consumerKey: '**KEY**', // Your consumer key
consumerSecret: '**KEY**', // Your consumer secret
wpAPI: true, // Enable the WP REST API integration
version: 'wc/v2' // WooCommerce WP REST API version
});
render() {
return(
<div className="GoodsMain">
<div className="Goods">
<img src="/images/photo.png" alt="appletv"/><br/>
<div className="TextAlign">
<span className="NameOfGood">{WooCommerce.get('products/1').name}</span><br/>
<span className="StyleOfGood">black</span><br/>
<span className="PriceOfGood">$49.99</span>
</div>
</div>
</div>
);
}
}
export default Goods;
But I get Line 20: 'WooCommerce' is not defined no-undef
Can u help me integrate correctly API in my site?
I Think you have to use the oauth authentication for getting the data from the woo-commerce rest api because your wordpress is on the server where there is no ssl certificate.
Try this way:
1). Use axios library (https://alligator.io/react/axios-react/) to call the WooCommerce Rest API.
2). To simplify the authentication process, make sure you enable SSL on your Wordpress hosting (I tried authenticate on the non SSL, it appears the process was not that easy, compared the SSL one).
3). Follow the steps from this link to generate the WooCommerce Rest API key
https://woocommerce.github.io/woocommerce-rest-api-docs/?javascript#authentication
4). Once everything setup, test retrieve list of products using Postman (https://www.getpostman.com/).
[GET]
https://{{host}}/{{wordpressFolder}}/wp-json/wc/v2/products/
[HEADER]
// Inside your React code, do this:
const basicToken = btoa(PRODUCTS_CONSUMER_KEY+':'+PRODUCTS_CONSUMER_SECRET)
Authorization: Basic ${basicToken}