After implementing the basic authentication experience from supabase, I'm trying to create a simple page that's only available to authenticated users and shows their user data.
I'm consistently faced with a React Hydration Error despite implementing exactly what the docs say.
The error occurs when logged in. When logged out I'm successfully routed to /wip
Here's my simple page code.
import {NextPage} from 'next';
import styles from '../styles/Home.module.css';
import {withPageAuth} from '#supabase/auth-helpers-nextjs';
export const getServerSideProps = withPageAuth({redirectTo: '/wip'});
const Profile: NextPage = ({user}) => {
return (
<div className={styles.container}>
<main className={styles.main}>
<h1 className={styles.title}>Welcome back!</h1>
<p>
<pre>{JSON.stringify(user, null, 2)}</pre>
</p>
</main>
</div>
);
};
export default Profile;
Package.json
"dependencies": {
"#supabase/auth-helpers-nextjs": "^0.2.8",
"#supabase/auth-helpers-react": "^0.2.4",
"#supabase/supabase-js": "^1.35.7",
"#supabase/ui": "^0.36.5",
"next": "12.3.1",
"pocketbase": "^0.7.1",
"react": "18.2.0",
"react-dom": "18.2.0"
}
Related
So here I am developing a web app. I use supabase as the database and SWR as the React Hooks. I have followed the step by step on YouTube, but then I encountered the following problem. In the index.tsx file there is an error regarding the rest parameter in the following fetcher function, even though the function is a template function provided by SWR. The error says, "Rest parameter 'args' implicitly has an 'any[]' type." .How should I fix it?
const Home: NextPage = () => {
const [myPosts, setMyPosts] = useState([])
const fetcher = (...args) => fetch(...args).then(res => res.json())
const {data, error} = useSWR('/api/get-posts', fetcher, {refreshInterval: 200})
console.log(data)
useEffect(() => {
if (!data) return
setMyPosts(data.data)
}, [data])
return (
<div className={style.wrapper}>
<Header />
<Banner />
<main className={style.main}>
<div className={style.content}>
<CreatePost />
<Feed posts={myPosts}/>
</div>
<div className={style.infoContainer}>
<About />
</div>
</main>
</div>
)
}
export default Home
I hope that my data in database can be fetched and rendered in my Next.js project.
I'm trying to connect my app with facebook login. currently i'm using the developer environment under meta developers account.
I have a login component which looks like below -
import Image from "next/image"
import { signIn } from "next-auth/react"
function Login() {
return (
<div className="grid place-items-center">
<Image
src="https://upload.wikimedia.org/wikipedia/commons/4/44/Facebook_Logo.png?20170210095314"
width={300}
height={300}
style={{objectFit:"contain"}}
alt="logo"
/>
<h1
className="p-5 m-5 bg-blue-500 rounded-full text-white text-center cursor-pointer"
onClick={signIn}>Login with Facebook</h1>
</div>
)
}
export default Login
and here is my code from [...nextauth.js]
import NextAuth from "next-auth"
import FacebookProvider from "next-auth/providers/facebook";
export const authOptions = {
providers: [
FacebookProvider({
clientId: process.env.FACEBOOK_CLIENT_ID,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
}),
],
}
export default NextAuth(authOptions)
When i click on login with facebook button it throws error
Any help .....
If anyone encountered the same problem below solution works for me.
I made a silly mistake. The Problem was with NEXT_AUTH_URL.
The url was pointing to https://localhost:3000 which is creating the issue.
I have updated url to http://localhost:3000.
And everything working fine.
How do I troubleshoot this problem this deployment issue? I am following this tutorial. My node_modules and .next are ignored and not pushed to github. It works locally but can't seem to deploy. I have supplied both the component code as well as the page it's exported on. Let me know if you can see what I am missing.
https://www.youtube.com/watch?v=V4SVNleMitE
deployment errors
Error occurred prerendering page "/components/BlogPosts". Read more: https://nextjs.org/docs/messages/prerender-error
TypeError: Cannot read property 'fields' of undefined
at BlogPosts (/vercel/path0/.next/server/chunks/130.js:39:12)
at d (/vercel/path0/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:33:498)
at bb (/vercel/path0/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:36:16)
at a.b.render (/vercel/path0/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:42:43)
at a.b.read (/vercel/path0/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:41:83)
at Object.exports.renderToString (/vercel/path0/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:52:138)
at Object.renderPage (/vercel/path0/node_modules/next/dist/server/render.js:673:46)
at Object.defaultGetInitialProps (/vercel/path0/node_modules/next/dist/server/render.js:315:51)
at Function.getInitialProps (/vercel/path0/.next/server/pages/_document.js:645:16)
at Object.loadGetInitialProps (/vercel/path0/node_modules/next/dist/shared/lib/utils.js:69:29)
component blog posts
export default function BlogPosts({post }) {
const {title, information,slug , thumbnail} = post.fields
return (
<div>
<div className='container w-50 h-25 mt-4'>
<Image
className='nav'
src={'https:' + thumbnail.fields.file.url}
width={thumbnail.fields.file.details.image.width}
height={thumbnail.fields.file.details.image.height}
/>
</div>
<div>
<div>
<h4 className=''>{title}</h4>
<Link href={'/contentslug/' + slug}>
<a className='btn btn-primary text-white'>Read more</a>
</Link>
</div>
</div>
</div>
)
}
Pages/Posts
import {createClient} from 'contentful'
import BlogPosts from './components/BlogPosts'
import Nav from './components/Nav'
import Logo from './components/Logo'
export async function getStaticProps() {
const client = createClient({
space: process.env.NEXT_PUBLIC_CONTENTFUL_ID,
accessToken: process.env.NEXT_PUBLIC_CONTENTFUL_TOKEN,
})
const res = await client.getEntries({content_type: 'posts'})
return {
props: {
posts: res.items ,
revalidate: 1
}
}
}
export default function Home({posts}) {
console.log(posts);
return (
<div>
<Logo/>
<Nav/>
<div className="container text-center display-5">
{posts.map(post => (
<BlogPosts key={post.sys.id} post={post}/>
))}
</div>
</div>
)
}
You have fields of undefined. this might be caused because of some strange deploying behavior if you are 100% sure your code works.
How to fix (probably):
Build your project locally. if it works, follow the next step
Comment your code in BlogPosts, inside the exported component. The code must work, so your exported component will be empty but working.
Push this code to Vercel.
Uncommit your code. (done at point 2)
Push again.
P.S. this behavior with API is sometimes caused because of API middleware you reworked.
I have social auth on a chat app I've built. The Google auth works perfectly fine but the Facebook auth mysteriously hits another URL for a second and then redirects back to the login page. I know that the App id and App secret are correct in firebase authentication for Facebook. When I was just running the app locally on http://localhost:3000, it gave me this error:
I assumed hosting it to a secure site with https would remedy the situation but now it just hits the url and returns to the login page with no errors logged in the console. Here is the code that handles the login:
import React from 'react';
import { GoogleOutlined, FacebookOutlined } from '#ant-design/icons';
import 'firebase/app';
import { auth } from '../firebase';
import firebase from 'firebase/app';
const Login = () => {
return (
<div id='login-page'>
<div id='login-card'>
<h2>Welcome to Unichat!</h2>
<div
className='login-button google'
onClick={() =>
auth.signInWithRedirect(new firebase.auth.GoogleAuthProvider())
}
>
<GoogleOutlined /> Sign In with Google
</div>
<br /> <br />
<div
className='login-button facebook'
onClick={() =>
auth.signInWithRedirect(new firebase.auth.FacebookAuthProvider())
}
>
<FacebookOutlined /> Sign In with Facebook
</div>
</div>
</div>
);
};
export default Login;
And here is the hosted site to test out the Facebook login: https://chatapplication.netlify.app
I'm trying to implement Pikaday in Meteor React. I've searched through numerous solutions and I can't get any of them to work. As I understand it, this is supposed to work:
I installed pikaday as follows: npm install -- save react react-pikaday.
Below is my code - What am I doing wrong?
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import Pikaday from 'react-pikaday';
export default class TestForm extends Component {
ComponentDidMount() {
new Pikaday({
field: ReactDOM.findDOMNode(this.refs.TestForm),
format: 'DD/MM/YYYY',
firstDay: 0,
minDate: new Date(new Date()),
maxDate: new Date('2050-12-31'),
yearRange: [2000,2050],
});
}
render() {
return(
<div>
<form>
<div className="row">
<div className="input-field col s6">
<input ref="TestForm" type="text" />
</div>
</div>
</form>
</div>
)
}
}
From the github page, there is a component that can be used:
<Pikaday value={date} onChange={this.handleChange} />
If you want to do the componentDidMount way, add an id to the div tag. and use document.getElementById('textId'); instead of using ReactDOM.
I also noticed a typo in ComponentDidMount() {. It should be componentDidMount (c - lowercase).