Facebook authorization not working for firebase auth - firebase

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

Related

Facebook login with NextJS next-auth

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.

Nuxt3 data fetching not working after client-side navigation

I am working on a Nuxt3 app locally. I have a page component [slug].vue page component for blog posts which fetches data from a Strapi endpoint. When I navigate from the home page to a blog post via <nuxt-link> I get an empty page. When I reload the page in the browser, the data gets displayed. At this point I am kinda lost and don't know what's wrong. Here is the code of the [slug].vue
<template>
<section id="start" class="min-h-screen flex items-center">
<LayoutMoContainer is-narrow>
<div v-if="pending">
Loading ...
</div>
<div v-else>
<div v-if="post">
<AtomsMoHeadline v-if="post.title" class="text-center" headline-type="h1" :text="post.title" />
<div v-if="post.content" class="dark:text-white">
<div v-html="post.content"></div>
</div>
</div>
</div>
</LayoutMoContainer>
</section>
</template>
<script lang="ts" setup>
/**
* Fetch posts
*/
const config = useRuntimeConfig();
const STRAPI_URL = config.STRAPI_URL;
const route = useRoute();
const slug = route.params.slug;
const { pending, data: post } = await useLazyAsyncData('post', () => $fetch(`${STRAPI_URL}articles/${slug}`), { server: false });
</script>
Nuxt Project Info
- Operating System: `Darwin`
- Node Version: `v18.7.0`
- Nuxt Version: `3.0.0-rc.6`
- Package Manager: `yarn#1.22.19`
- Builder: `vite`
- User Config: `publicRuntimeConfig`, `app`, `css`, `buildModules`, `build`, `vite`
- Runtime Modules: `-`
- Build Modules: `#pinia/nuxt#0.3.1`
The problem was due to a page component having more than one root element.

how to fix nextjs prerender error - vercel deployment?

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.

How to use Next-Auth for login form with axios request instead of default Form submit

I am using Next-Auth in my case user login but after i configs as custom page login and congrats with Form login request by method POST and it reload page when submitted. But i don't want to reload page, i wanna use axios to request data submit to APIs server when login. How i can implement this case?
Instead of submitting a form via <button type="submit">, you can drop the whole form part and use the signIn() function from NextAuth.
Like this:
import { getProviders, signIn } from "next-auth/react"
export default function SignIn({ providers }) {
return (
<>
{Object.values(providers).map((provider) => (
<div key={provider.name}>
<button onClick={() => signIn(provider.id)}>
Sign in with {provider.name}
</button>
</div>
))}
</>
)
}
See: https://next-auth.js.org/configuration/pages#oauth-sign-in

Popup shows up and then it closes instantly

I'm trying follow the guide from angularfire2 docs on github, but when I try to open the popup window for google login it just closes instantly.
I've tried with different browsers, but I can't figure out what's going on.
Here's the HTML of my form component:
<div class="container " >
<h2 class="date">Wallet manager</h2>
<h1>SUBBO</h1>
<form>
<div class="form-group">
<label class="date" for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label class="date" for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<button type="submit" (click)="login()" style="margin-top:20px" class="btn btn-outline-warning">Login</button>
<button type="submit" (click)="logout()" style="margin-top:20px" class="btn btn-outline-warning">Logout</button>
</form>
</div>
and here is the code:
import { Component, OnInit } from '#angular/core';
import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app';
#Component({
selector: 'app-login-form',
templateUrl: './login-form.component.html',
styleUrls: ['./login-form.component.css']
})
export class LoginFormComponent implements OnInit {
constructor(public afAuth: AngularFireAuth) {
}
login() {
this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleProvider());
}
logout() {
this.afAuth.auth.signOut();
}
ngOnInit() {
}
}
With the login button I call Login() and I was expecting the window to popup and stay there, but it close immediately.
I've uploaded the project to Firebase and it give's me the same problem:
https://subbo-wallet-manager.firebaseapp.com/
I had this same problem using Firebase Authentication. The solution for me was to go into the Firebase Control Panel, select my project, select authentication and under "Sign-in method" I had to add my domain to the authorized domain list. Once I did that, and reloaded the page, it worked.
The fire base documentation gives me this example:
firebase.auth().signInWithPopup(provider).then(function(result) {
// This gives you a Google Access Token. You can use it to access the Google API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
// ...
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
Your problem might be the missing then() (it might make it async, not sure). If my guess is wrong, you may still use the catch part to see if any errors occured. Maybe the provider is not valid.
I had the same issue but in my case the login just was failing for Google Chrome, so I tried to update Google Chrome version to the latest one and the login started to work fine again.

Resources