Firebase checkin system schema - javascript

I am trying to create a checking system using firebase, the way it would work is that it would take the business user uid and the then person checking in uid.
The database looks something like this :
Would this work or is there no way to concurrently get two uids?
EDIT
In my haste I forgot to add the code that I tried:
firebase.auth().onAuthStateChanged(function(user) {
if(user) {
var buisnesscheckedin = db.ref("checkedin/" + user.uid)
checkinButton.addEventListener('click', function(e) {
e.preventDefault()
buisnesscheckedin.push({
status: 'true'
})
console.log("Checked In")
})
}
else {
window.location.href = 'index.html'
}
})
This is what I tried but the problem is that I want to be able to get the uid associated with the business instead of using the consumers uid first.
So the path should look this:
checkedin/buisness_uid/consumer_uid/

Related

How do you add users and additional details to a user node in the Database every time they're registered?

I've tried to create the code by using what I've found online about adding additional details to a user node, but I've come up without a clear cut and definite solution. I can't make heads or tails with Google searches and other such things, but I only found a depreciated solution that's not supported and does not work with the current version of Firebase. So far, here's what I've come up with:
(function(){
var ref = new firebase.database();
ref.onAuth(function(authData) {
if (authData && isNewUser) {
// save the user's profile into Firebase so we can list users,
// use them in Security and Firebase Rules, and show profiles
ref.child("users").child(authData.uid).set({
provider: authData.provider,
name: getName(authData)
});
}
});
var ui = new firebaseui.auth.AuthUI(firebase.auth());
var uiConfig = {
callbacks: {
signInSuccessWithAuthResult: function(authResult, redirectUrl) {
// User successfully signed in.
// Return type determines whether we continue the redirect automatically
// or whether we leave that to developer to handle.
return true;
},
uiShown: function() {
// The widget is rendered.
// Hide the loader.
document.getElementById('loader').style.display = 'none';
}
},
// Will use popup for IDP Providers sign-in flow instead of the default, redirect.
signInFlow: 'popup',
signInSuccessUrl: 'map_menu.php',
signInOptions: [
firebase.auth.EmailAuthProvider.PROVIDER_ID,
],
// Terms of service url.
tosUrl: 'map_menu.php',
// Privacy policy url.
privacyPolicyUrl: 'map_menu.php'
};
ui.start('#firebaseui-auth-container', uiConfig);
})()
You should add a code to get the user data like uid as the main child of the user and all other details as sub child nodes of this node in your database.
A Json structure that looks something like this:
{
"accounts" : {
"userId value" : {
"email" : "",
"userId" : ""
},
"userId value" : {
"email" : "",
"userId" : ""
}
}
To save the users' details in this node, just when they are registered, you can add a code like this:
var uid = firebase.auth().currentUser.uid;
firebase.database().ref().child('accounts').child(uid).set({
email: user.email,
userId: uid
})

(Reddit API) How to get a list of subreddits user is subscribed to, using snoowrap?

So I'm using snoowrap to write a Chrome extension that gets a list of subreddits the user is subscribed, and subscribes to them on a different account.
I'm trying to get the list of subreddits currently but can't figure out how to do it. I've tried simply getting the JSON from https://www.reddit.com/subreddits/mine.json, which returns an empty object (persumably because no auth) and I have no idea how to do it via snoowrap. I looked through the documentation and can't find an option for it.
My code:
document.addEventListener('DOMContentLoaded', function() {
var login = document.getElementById('login');
login.addEventListener('click', function() {
const r = new snoowrap({
userAgent: '???',
clientId: '<id>',
clientSecret: '<clientsecret>',
username: '<username-here>',
password: '<password-here>'
});
r.getHot().map(post => post.title).then(console.log);
});
var getSubs = document.getElementById('get-subs');
getSubs.addEventListener('click', function() {
fetch('https://www.reddit.com/subreddits/mine.json')
.then(function(data) {
console.log(JSON.stringify(data));
})
.catch(function(error) {
console.log('error');
});
});
});
Not sure how else to try. Anyone have suggestions? I'd like to use snoowrap for this ideally.
When using snoowrap as API wrapper, after connecting to the api with:
const r = new snoowrap({...});
They provide a function for getting your own subscribed subreddits:
r.getSubscriptions();
This will return a Listing Object, which you can use like an Array.

Parse-server social login

I am developing application based on Parse-server and I want to offer social login. I found this guide in the documentation http://docs.parseplatform.org/js/guide/#linking-users.
I started to implement the social login by google. I did following steps:
1) I added following lines to the ParseServer settings
var api = new ParseServer({
...
auth:{
google: {}
},
...
});
2) I did the authentication by hello.js on the client side (call user._linkWith function on login)
hello.init({
google: 'My Google id'
});
hello.on('auth.login', function(auth) {
// Call user information, for the given network
hello(auth.network).api('me').then(function(r) {
const user = new Parse.User();
user._linkWith(auth.network, auth.authResponse).then(function(user){
console.log('You are logged in successfully.');
});
});
});
When I debugged it, I found that it fails in _linkWith() function, when provider object is preparing. Object AuthProviders, which should store all providers, is empty. Because of it the statement provider = authProviders['google']; leads to undefined. Invoking provider.authenticate(...); leads to error "Cannot read property 'authenticate' of undefined"
What am I missing or what am I doing wrong?
Thanks for all your answers.
Honza
Did you register the authenticationProvider? You can find examples in our unit tests on how to do so:
https://github.com/parse-community/parse-server/blob/5813fd0bf8350a97d529e5e608e7620b2b65fd0c/spec/AuthenticationAdapters.spec.js#L139
I also got this error and looked at the _linkWith(provider, options) source code. It checks if options has an authData field (which in turn should contain id and credentials). If so, it uses options.authData. Otherwise it falls back on looking up a previously registered authentication provider mentioned in the previous answer.
This is a fragment of the code I'm using:
const authData = {
"id": profile.getId(),
"id_token": id_token
}
const options = {
"authData": authData
}
const user = new Parse.User();
user._linkWith('google', options).then(function(user) {
console.log('Successful user._linkWith(). returned user=' + JSON.stringify(user))
}, function(error) {
console.log('Error linking/creating user: ' + error)
alert('Error linking/creating user: ' + error)
// TODO handle error
})

How to set user data after createUserWithEmailAndPassword (web)

I have search on other questions but I can't find anything, so I'm sorry if this is a duplicated.
I'm trying to save extra user data on sign up to database, the signup is not a problem but I can't store the data, I'm trying this:
//add a real time listener
firebase.auth().onAuthStateChanged(firebaseUser =>{
if (user != null) {
firebase.database().ref().child( firebaseUser.uid ).set( {
/**store data**/
} );
console.log("Sign-in provider: "+ firebaseUser.providerId);
console.log("Provider-specific UID: "+ firebaseUser.uid);
console.log("name: "+firebaseUser.name);
console.log("email: "+firebaseUser.email);
console.log("country:"+firebaseUser.country);
//console.log(" Photo URL: "+firebaseUser.photoURL);
//window.location.href = "index.html";
}else{
console.log('not logged in');
}
});
You can store any of the properties of the Firebase User. This should work:
firebase.database().ref().child( firebaseUser.uid ).set( {
firebaseUser.displayName,
lastSignInTimestamp: Date.now()
});
If that doesn't work, your user might not have permission to write to the location in the database. To troubleshoot that, attach an error listener:
firebase.database().ref().child( firebaseUser.uid ).set( {
firebaseUser.displayName,
lastSignInTimestamp: Date.now()
}.catch(function(error) {
console.error(error);
});
If indeed it's a security problem, see the first blue note in the Firebase documentation about writing data to the database.

Trouble with privileges when adding custom field to a Meteor user

I'm having trouble adding custom user fields to a Meteor user object (Meteor.user). I'd like a user to have a "status" field, and I'd rather not nest it under "profile" (ie, profile.status), which I do know is r/w by default. (I've already removed autopublish.)
I've been able to publish the field to the client just fine via
Meteor.publish("directory", function () {
return Meteor.users.find({}, {fields: {username: 1, status: 1}});
});
...but I can't get set permissions that allow a logged-in user to update their own status.
If I do
Meteor.users.allow({
update: function (userId) {
return true;
}});
in Models.js, a user can edit all the fields for every user. That's not cool.
I've tried doing variants such as
Meteor.users.allow({
update: function (userId) {
return userId === Meteor.userId();
}});
and
Meteor.users.allow({
update: function (userId) {
return userId === this.userId();
}});
and they just get me Access Denied errors in the console.
The documentation addresses this somewhat, but doesn't go into enough detail. What silly mistake am I making?
(This is similar to this SO question, but that question only addresses how to publish fields, not how to update them.)
This is how I got it to work.
In the server I publish the userData
Meteor.publish("userData", function () {
return Meteor.users.find(
{_id: this.userId},
{fields: {'foo': 1, 'bar': 1}}
);
});
and set the allow as follows
Meteor.users.allow({
update: function (userId, user, fields, modifier) {
// can only change your own documents
if(user._id === userId)
{
Meteor.users.update({_id: userId}, modifier);
return true;
}
else return false;
}
});
in the client code, somewhere I update the user record, only if there is a user
if(Meteor.userId())
{
Meteor.users.update({_id: Meteor.userId()},{$set:{foo: 'something', bar: 'other'}});
}
Try:
Meteor.users.allow({
update: function (userId, user) {
return userId === user._id;
}
});
From the documentation for collection.allow:
update(userId, doc, fieldNames, modifier)
The user userId wants to update a document doc. (doc is the current version of the document from the database, without the proposed update.) Return true to permit the change.

Categories

Resources