Last active
May 19, 2022 12:49
-
-
Save sahilkashyap64/e88a9cdc2a4ee52c0ab2325f8a232922 to your computer and use it in GitHub Desktop.
Social login(Google,FB) Backend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//react frontend | |
import { GoogleLogin } from 'react-google-login'; | |
import FacebookLogin from 'react-facebook-login'; | |
class LoginPage extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
userId: '', | |
password: '', | |
} | |
this.responseSuccessGoogle = this.responseSuccessGoogle.bind(this); | |
this.responseErrorGoogle = this.responseErrorGoogle.bind(this); | |
this.responseSuccessFB = this.responseSuccessFB.bind(this); | |
} | |
render() { | |
const GOOGLE_CLIENT_ID="xxxxx"; | |
const FB_APP_ID="xxxxx"; | |
return ( <div> <GoogleLogin | |
clientId={GOOGLE_CLIENT_ID} | |
buttonText="Sign in with google" | |
onSuccess={this.responseSuccessGoogle} | |
onFailure={this.responseErrorGoogle} | |
cookiePolicy={'single_host_origin'} | |
/><FacebookLogin | |
appId={FB_APP_ID} | |
autoLoad={false} | |
fields="name,email" | |
callback={this.responseSuccessFB} /> | |
</div>) | |
} | |
responseSuccessGoogle(resp){ | |
console.log("googleresponse",resp); | |
let result= await axios.post('/googleauthenticate',resp); | |
console.log(result) | |
} | |
responseSuccessFB(response){ | |
// console.log("fb",response); | |
let payload={accessToken:response.accessToken,userID:response.userID}; | |
let result= await axios.post('/fbauthenticate',payload); | |
console.log(result) | |
} | |
responseErrorGoogle(resp){ | |
console.log("google error",resp); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//backend | |
import SocialLogin from './socialLogin.js'; | |
const moment = require("moment"); | |
var signUPGoogleCustomer = endPointHandler(async function (req) { | |
var customer = req.body; | |
const {tokenId,googleId,accessToken}=customer; | |
const GoogleVerifyTicket=await SocialLogin.googleVerifyId(tokenId); | |
const {email_verified,email,given_name,family_name}=GoogleVerifyTicket; | |
let googlePeopleAPiData= await SocialLogin.axiosGet(`https://people.googleapis.com/v1/people/${googleId}?personFields=birthdays,genders,age_range&access_token=${accessToken}`); | |
let agerange=googlePeopleAPiData.ageRange; | |
const {year,month,day}=googlePeopleAPiData.birthdays[0].date; | |
let googlecustomer={}; | |
googlecustomer.email=email; | |
googlecustomer.name={first:given_name,last:family_name}; | |
googlecustomer.password=Math.random().toString(36).slice(2, 10); | |
googlecustomer.dob=`${year}-${month}-${day}`; | |
if(!year){ | |
if(agerange=='TWENTY_ONE_OR_OLDER'){ | |
googlecustomer.dob=`1998-${month}-${day}`; | |
} | |
} | |
googlecustomer.isEmailVerified=email_verified; | |
// TODO:enter the googlecustomer obj in DB or generte jwt token | |
return res.status(200).json({ message: "user created successfully(google)",status:200 }); | |
}); | |
var signUPFBCustomer = endPointHandler(async function (req) { | |
var customer = req.body; | |
const {userID,accessToken}=customer; | |
let fbUrl=`https://graph.facebook.com/${userID}?fields=id,name,email,picture,birthday&access_token=${accessToken}`; | |
let fbAPiData= await SocialLogin.axiosGet(fbUrl); | |
const {name,email,birthday}=fbAPiData; | |
var birthdayFormated = moment(birthday, 'MM/DD/YYYY').format('YYYY-MM-DD'); | |
let fbcustomer={}; | |
fbcustomer.email=email; | |
fbcustomer.name={first:name,last:""}; | |
fbcustomer.password= Math.random().toString(36).slice(2, 10); | |
fbcustomer.dob=birthdayFormated; | |
fbcustomer.isEmailVerified=true; | |
// TODO:enter the googlecustomer obj in DB or generte jwt token | |
return res.status(200).json({ message: "user created successfully(fb)",status:200 }); | |
}); | |
var FBCustomerLogin= async function (req, res, next) { | |
var customer = req.body; | |
const {userID,accessToken,birthday}=customer; | |
let fbUrl=`https://graph.facebook.com/${userID}?fields=id,name,email&access_token=${accessToken}`; | |
let fbAPiData= await SocialLogin.axiosGet(fbUrl); | |
const {email}=fbAPiData; | |
let result= Table.findOne({email: email}) | |
.then( function(user) { | |
try { | |
return res.status(200).json({ | |
user: user, | |
message:"Login succesful fb" | |
}); | |
} catch(e) { | |
return res.status(400).json({ message: "Something went wrong while login",error:e,status:400 }); | |
} | |
}) | |
}; | |
var GoogleCustomerLogin= async function (req, res, next) { | |
var customer = req.body; | |
const {tokenId,googleId,accessToken}=customer; | |
const GoogleVerifyTicket=await SocialLogin.googleVerifyId(tokenId); | |
const {email_verified,email,given_name,family_name}=GoogleVerifyTicket; | |
let result= Table.findOne({email: email}) | |
.then( function(user) { | |
try { | |
return res.status(200).json({ | |
user: user, | |
message:"Login succesful google" | |
}); | |
} catch(e) { | |
return res.status(400).json({ message: "Something went wrong while login",error:e,status:400 }); | |
} | |
}) | |
}; | |
var base = "/customers"; | |
Router.route(base+"/googlesignup") | |
.post(limiter, signUPGoogleCustomer); | |
Router.route(base+"/googleauthenticate") | |
.post(limiter, GoogleCustomerLogin); | |
Router.route(base+"/fbsignup") | |
.post(limiter, signUPFBCustomer); | |
Router.route(base+"/fbauthenticate") | |
.post(limiter, FBCustomerLogin); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//react frontend | |
import { GoogleLogin } from 'react-google-login'; | |
import FacebookLogin from 'react-facebook-login'; | |
class SignupPage extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
userId: '', | |
password: '', | |
} | |
this.responseSuccessGoogle = this.responseSuccessGoogle.bind(this); | |
this.responseErrorGoogle = this.responseErrorGoogle.bind(this); | |
this.responseSuccessFB = this.responseSuccessFB.bind(this); | |
} | |
render() { | |
const GOOGLE_CLIENT_ID="xxxxx"; | |
const FB_APP_ID="xxxxx"; | |
return ( <div> <GoogleLogin | |
scope = 'https://www.googleapis.com/auth/user.birthday.read https://www.googleapis.com/auth/user.gender.read https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/profile.agerange.read' | |
clientId={GOOGLE_CLIENT_ID} | |
buttonText="Sign up with google" | |
onSuccess={this.responseSuccessGoogle} | |
onFailure={this.responseErrorGoogle} | |
cookiePolicy={'single_host_origin'} | |
/> | |
<FacebookLogin | |
appId={FB_APP_ID} | |
textButton="Sign up with Facebook" | |
autoLoad={false} | |
fields="name,email,picture,birthday" | |
scope="user_birthday" | |
callback={this.responseFacebook} /> | |
</div>) | |
} | |
responseSuccessGoogle(resp){ | |
console.log("googleresponse",resp); | |
let result= await axios.post('/googlesignup',resp); | |
console.log(result) | |
} | |
responseSuccessFB(response){ | |
// console.log("fb",response); | |
let payload={accessToken:response.accessToken,userID:response.userID}; | |
let result= await axios.post('/fbsignup',payload); | |
console.log(result) | |
} | |
responseErrorGoogle(resp){ | |
console.log("google error",resp); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment