An abstracted approach to handling Facebook OAuth using Hooks and Typescript!
Requires react >= 16.8.0
Installation
Install the package using your package manager of choice.
$ npm install react-easy-facebook
$ yarn add react-easy-facebook
Usage
Basic demo
importreactfrom"react";
importuseFacebookfrom"react-easy-facebook";
constApp=()=>{
/* Initializing the hook by giving it the facebook app id. */
const{response,login,logout}=useFacebook({
appId:"5135128098923510",
});
consthandleLogin=()=>{
login();
};
consthandleLogout=()=>{
logout();
};
/*
response is a react state so subscribe to its updates however you'd like to.
Here, I'm printing it inside a useEffect.
*/
React.useEffect(()=>{
if(response)console.log(response);
},[response]);
/* Trigger the login action by calling login() */
/* Similarly, trigger the logout action by calling logout() */
return(
<>
<buttononClick={handleLogin}>login</button>
<buttononClick={handleLogout}>logout</button>
</>
);
};
Using with custom scopes
const{response,login}=useFacebook({
appId:"5135128098923510",
options:{
scope:["email","user_birthday"],
},
});
Or
const{response,login}=useFacebook({
appId:"5135128098923510",
options:{
scope:"email,user_birthday",
},
});
Using with custom scopes and fields
const{response,login}=useFacebook({
appId:"5135128098923510",
options:{
scope:["email","user_birthday"],
},
fields:["id","name","email"],
});
Or
const{response,login}=useFacebook({
appId:"5135128098923510",
options:{
scope:"email,user_birthday",
},
fields:"id,name,email",
});
Generally, you should use the array syntax to get Intellisense hints. But you'll always need to use the string syntax when hitting an edge like picture.