How to get the Authentication Provider for actions-on-google on Node using account linking with Auth0? - actions-on-google

I have Javascript App running under Node v8.11.2 which uses the Actions-On-Google library. I'm using the V2 API. I have account linking set up with Auth0 and am using the SignIn helper intent. Auth0 is set up to use Google, Facebook and Twitter.
The scopes I use are OPENID, OFFLINE_ACCESS, PROFILE and EMAIL.
Everything is working fine and when the User is authenticated I get an Access Token returned.
My question is, how do I get the Authentication Provider that was selected by the User so that I can use the Access Token correctly to retrieve profile elements such as the display name, email address etc??
The signin object passed to the Sign In Confirmation intent handler just contains the following regardless of the provider selected: -
{"#type":"type.googleapis.com/google.actions.v2.SignInValue","status":"OK"}
Any help greatly appreciated as I have a deadline and this is driving me a bit crazy now!
Thanks,
Shaun

If your question is about how to get the required information when you have your accessToken available then you could use what is shown in this answer.
In node this looks like that:
let link = "https://www.googleapis.com/oauth2/v1/userinfo?access_token="+accessToken;
return new Promise(resolve => {
request(link,(error, response, body) => {
if (!error && response.statusCode === 200) {
let data = JSON.parse(body);
let name = data.given_name ? data.given_name : '';
conv.ask(new SimpleResponse({
speech: "Hello "+ name + "!",
text: "Hello "+ name + "!"
}));
resolve();
} else {
console.log("Error in request promise: "+error);
resolve();
}
})
})
Everything you need should be in the data object.
Hope it helps.

Related

Expo Facebook Login- Producing Invalid Tokens

I followed the docs to use the Facebook Login SDK. Everything works except for the fact that the token that is generated does not work for our API. Our API takes the user-generated token as a header so that it can make requests. I know the reason why it isn't working is that I am logging in with Facebook, but there is no account created on our API. Facebook will not give me access to a user's password (for obvious reasons), so I can't generate an account from the information provided from Facebook.
I think the answer lies with App Access Tokens. If someone could help me out I would really appreciate it!
code:
export const logInFB = () => async dispatch => {
const { type, token } = await Expo.Facebook.logInWithReadPermissionsAsync('**Blocking this out**', {
permissions: ['public_profile'],
});
console.log('token');
console.log(token);
if (type === 'success') {
// Get the user's name using Facebook's Graph API
const response = await fetch(
`https://graph.facebook.com/me?access_token=${token}`);
console.log('fb token')
console.log(response);
dispatch(fbLoginToken(token));
Alert.alert(
'Logged in!',
`Hi ${(await response.json()).name}!`,
);
}
}
Looks like you are missing the fields property in the URL. ex: &fields=id,name,email,about,picture
Please refer to this example: https://snack.expo.io/#bacon/facebook
const response = await fetch(
`https://graph.facebook.com/me?access_token=${token}&fields=id,name,email,about,picture`
);
const responseJSON = JSON.stringify(await response.json());
this.setState({ responseJSON });

Error in Google Sign in for the Assistant

I am using "Google Sign-in for the Assistant" for Account Linking, and implement the Google Sign-In only flow. At the first time when user invoke an app (e.g: "Talk to my Test App"), it works fine ask for permission for taking name, profile picture, email from the google and Successfully get the info. But when i invoke an app second time it does not responding and receiving error in the logs: "App with account linking returned authentication error. Removing stored OAuth token." i didn't understand whats happening here, following is my code i am using as a webhook,
const app = actionssdk({
debug: true
})
app.intent('actions.intent.MAIN', (conv) => {
conv.ask(new SignIn('To get your account details'))
conv.ask('Hello')
})
app.intent('Default Welcome Intent', conv => {
conv.ask(`Hello test version`)
})
app.intent('actions.intent.SIGN_IN', (conv, input, signin) => {
if (signin.status === 'OK') {
const payload = conv.user.profile.payload
conv.ask(`I got your account details, ${payload.name}. What do you want to do next?`)
} else {
conv.ask(`I won't be able to save your data, but what do you want to do next?`)
}
})
Your app makes a user sign in every time a user launches your app. So if a user is a returning user (second time), system tries to remove/recreate profile about the user because that person is already signed in. So you should write in a way that the user does not fall into the sign in flow after the second time.
app.intent('welcome', conv => {
const payload = conv.user.profile.payload;
if (!paylaod) {
return conv.ask(new SignIn('to sign in);
}
conv.ask('hi! ${payload.given_name}, how are you?')
})
This might work fo you.

Firebase signInWithCredential failed: First argument "credential" must be a valid credential

I am using the react-native-facebook-login package to log users in. Currently the flow is working well and after the user enters their details, I successfully see an object returned with their information.
When I try and create an account in Firebase with signInWithCredential, I receive the following error message:
signInWithCredential failed: First argument "credential" must be a valid
I can't seem to find a breakdown of how that credential needs to be passed - is it a string, an object, an array etc. Is it just the token or do I need to pass other details (i.e. the provider)?
The credentials object I am currently getting back has:
permission: Array
token: String
tokenExpirationDate: String
userId: String
Any help would be much appreciated - thanks!
Feeling pretty pleased - finally cracked the nut.
They key bit is the token needs to be changed first before being a relevant credential. See code below:
onLogin={function(data){
let token = firebase.auth.FacebookAuthProvider.credential(data.credentials.token);
firebase.auth().signInWithCredential(token)
.then((user) => {
console.log(user)
}).catch((err) => {
console.error('User signin error', err);
});
}}
to answer your question, based on the documentation of firebase:
where GoogleAuthProvider could be any of your setup / supported auth providers
// Build Firebase credential with the Google ID token.
var credential = firebase.auth.GoogleAuthProvider.credential(id_token);
// Sign in with credential from the Google user.
firebase.auth().signInWithCredential(credential).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;
// ...
});
On a side note, as you are using react-native and firebase, did you already try react-native-firestack? makes a lot of things easier.

Sails Rest API using Passport-Facebook-Token Strategy Driving Me Nuts

I have a mobile front-end that already has facebook authetication working. I have a Sails REST API that stores user data, posts etc.. I want to add security where facebook users can only POST GET DELETE PUT their own data.
I've read a almost every tutorial for facebook authenticating a web-app, but haven't found many for authenticating with a mobile app to protect the user data. I've tried to get Passport-Facebook-Token working but I just don't understand the little documentation available. I'm coming from a objective-C background so in the node learning curve now.
Here's the link to what I'm working with but I'm obviously missing something: https://github.com/drudge/passport-facebook-token
I have:
AuthController.js
module.exports = {
facebook: function(req, res) {
passport.authenticate('facebook-token', function(error, user, info) {
// do stuff with user
res.ok();
})(req, res);
}
};
api/services/protocols/passport.js
(with some other stuff from default passport sails-generate-auth)
var FacebookTokenStrategy = require('passport-facebook-token');
passport.use('facebook-token', new FacebookTokenStrategy({
clientID : "<my_id>",
clientSecret : "<my_secret>"
},
function(accessToken, refreshToken, profile, done) {
// console.log(profile);
var user = {
'email': profile.emails[0].value,
'name' : profile.name.givenName + ' ' + profile.name.familyName,
'id' : profile.id,
'token': accessToken
}
// You can perform any necessary actions with your user at this point,
// e.g. internal verification against a users table,
// creating new user entries, etc.
return done(null, user); // the user object we just made gets passed to the route's controller as `req.user`
}
));
Do I have to do something with config/routes to make sure it only allows users with access_tokens? I just can't find any resources out there. Passport doesn't even list Passport-Facebook-Token strategy as an option on their site.
thank you for the help

JS SDK getLoginStatus doesn't return userID nor signedRequest

I'm using PhoneGap/Cordova with the facebook plugin. Everything seems to work except for getLoginStatus who is not working as defined here and here. When called, it returns some data, but not all: it doesn't return userID nor signedRequest.
Here is the code:
FB.getLoginStatus(function(response) {
if (response.status == 'connected') {
var fb_uid = response.authResponse.userID;
var fb_signedRequest = response.authResponse.signedRequest;
alert('logged in');
} else {
alert('not logged in');
}
});
userID is filled with ellipsis (...), while signedRequest is undefined.
I managed to get userID with a graph call to /me:
FB.api('/me', function(me){
if (me.id) {
var fb_uid = me.id;
}
});
I wasn't able to find any way in the documentation to get a signed_request, which I have to use to authenticate the facebook user to a remote service to whom the user already connected to with facebook (I already made a login call so user is OK).
Basically the problem is that my call to getLoginStatus returns
{
status: 'connected',
authResponse: {
session_key: true,
accessToken: 'a long string...',
expiresIn:'a number',
sig: '...', //exactly this string
userID:'...' //exactly this string
secret:'...' //exactly this string
expirationTime:'a long number'
}
}
instead of what documented
As a background, when authentication happens using the plugin then the JavaScript SDK API calls the iOS/Android SDK to handle the authorization and then pass response auth data back to the JS part. The native (iOS/Android) SDKs do not get signed requests back to be able to pass this on to the JS. This is why it's empty.
If you use the latest plugin you should at least now be seeing the user ID. The one from June likely did not pass this back. Otherwise as a work around, you could perform a call to the /me endpoint when authentication is successful in your JS code to get the user id. See the Hackbook example that does this.