How to update the current user (Accounts) - javascript

I'm using Accounts (accounts-hithub).
Now this is working fine, but now I would like to update the current users.
I've tried things like
Accounts.update({_id: Meteor.user()._id}, {...});
Which gives an error, Accounts doesn't have a method update
So I tried to create a users collection
Users = new Meteor.Collection('users');
This is also not allowed, it gives me the following error:
Error: A method named '/users/insert' is already defined
So how do I update the current user ?

User details are in the Meteor.users collection. So you can edit user details using Meteor.users.update() (http://docs.meteor.com/#meteor_users).

Related

How do I add a user of my module to the coin contract with (coin.create-account) in a way that is similar to (coin.transfer-create)?

I have a module that some users may not have any KDA in their account, and thus don't exist on the coin contract. To prove ownership of a token, I am using (enforce-guard (at "guard" (coin.details account))) which returns "with-read: row not found: k:9584f4... if the user does not exist on the coin contract.
I want to call (coin.create-account) on behalf of a user of my module, so that when I check (enforce-guard), the response is valid, and afterwards the user exists on the coin contract just like if they had received KDA in their wallet.
From my understanding, I would call coin.create-account "k:9584f4..." (read-keyset "k:9584f4...") and then pay the gas, and they would then exist on the coin contract without having any KDA. I think my problem is: Where can I get the keyset to pass into (read-keyset) if my user is calling the contract through a front-end?
More directly: where do I get the guard to pass into coin.create-account?
To be able to fill out the parameters you need to define a keyset first in the ENV tab. Type a name and select a key like this:
then open the module explorer and the token you want to create the account on and reference the keyset:
If you need to perform the same steps from code take a look at the example from the docs that does exactly that: https://docs.kadena.io/build/frontend/pact-lang-api-cookbook#create-account

parse server add current user to relation in users class (javascript SDK)

i want to do something like who visit your profile function in my ionic application, simply this function take the current user parse object and insert it in a relation column present in users class called who visited
so what i would like to do first is to query the relation to see if the current user already there and if not insert it in the relation
tried the following code:
var currentUser = Parse.User.current(); //current user object
var user = this.navParams.get('user'); // parse user object passed through navparams
var relation = user.relation("whoVisited");
relation.add(currentUser)
user.save()
but it gives me POST error 400 Bad Request
After searching for 6 hours i found the following
User class is protected and no user can modify other user data thats why i was getting the error
I managed to do the same function using cloud code and the power of master key

Best approach/design to web application?

I have been taking a Node.js course on Udemy and would like to apply some of the knowledge I have gained to create a simple web application. I would like to have a user register, which leads him to an admin panel, this part I already have.
This user (requester) can then refer users (invitees) to this website using a unique link. For example he would click a button to generate a unique hyperlink (my idea for this link was to be the http://websiteurl/userid of the requester who is sending the link).
The requester can then send this link through email to their friends to invite them to the website, once they click the link they are taken to a sign up form and when they fill in the form they are linked (added to an array under the original user).
How would I go about setting up this "session", as in make sure that the form that the invitees fill out is linked to the original requester? How can those forms be generated dynamically on the requester's hyperlink?
I'm not looking for the exact code to do this, but rather validation if my idea for the url is a good approach or if there are other approaches I should consider.
Thanks in advance!
Well, this would require you changing the schema for your database. A user will need to have a property like:
{
referred: []
}
The referred array should contain ids or some sort of reference to a user's referred users.
On the "/:userid" route, the form should submit to the route where a new user is created and have a parameter with the user ID. In this case, I am using query parameters.
So if a person were to visit /foo, they would get a form that would post to a URL like /new?userid=foo.
In that route, you can do something like this (assuming Express):
app.post("/new", (req, res) => {
const userID = req.query.userid;
const newUser = // create user normally
if(userID) {
// `userID` referred this user
const referrer = getUser(userID);
referrer.referred.push(newUser);
save(referrer);
}
});
The getUser function should returning the current user, and you should modify the referred property with the new user. This code was merely an outline and you should update the user in your database.
In a nutshell, a form should post to /new?userid=foo, and when creating a new user, if this parameter is present, you can update the database entry for the user id in the parameter with the id of the new user.

firebase javascript injection

I want ask something about firebase security. How to handle following situations?
User is creating account with createUserWithEmailAndPassword() function, then i save his username,email,created_at...to realtime db. But what if data are not saved correctly. His account is created and he is logged in automatically but data is not stored.
I have some registration logic... for example unique usernames... so before creating acc i check if this username exist in realtime db. But he still can call createUserWithEmailandPassword() from js console and account is created.
For situation one:
According to the firebase docs (https://www.firebase.com/docs/web/api/firebase/createuser.html), creating a user does not automatically authenticate them. An additional call to authWithPassword() is required first. In order to ensure that a user isn't authenticated without valid data, you could run a check to the server to make sure the data is saved correctly before authenticating.
Edit: Nevermind that; looks like firebase does auto-auth now - take a look at what I wrote below.
Now a concern with this approach would be if your app allowed people to authenticate with an OAuth provider like gmail, then there is no function for creating the user before authenticating them. What you may need to do is pull the user data from the firebase, determine if it's valid, and if its not valid show a popup or redirect that lets the user fix any invalid data.
For situation two:
If you wanted to make sure that in the case of them calling createUserWithEmailAndPassword() from the console a new user is not created, you could try something like this with promises;
var createUserWithEmailAndPassword = function(username, password) {
var promise = isNewUserValid(username, password);
promise.then(function() {
// Code for creating new user goes here
});
}
In this way, you never expose the actual code that makes a new user because it exists within an anonymous function.
I don't think that this could solve the problem entirely though because firebases API would let anyone create an account using something
var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.createUser({
email: "bobtony#firebase.com",
password: "correcthorsebatterystaple"
}
(Taken from https://www.firebase.com/docs/web/api/firebase/createuser.html)
If you wanted to make sure that server side you can't ever create a user with the same user name, you'd need to look into firebases's rules, specifically .validate
Using it, you could make sure that the username doesn't already exist in order to validate the operation of creating a username for an account.
Here's the firebase doc on rules: https://www.firebase.com/docs/security/quickstart.html
And this is another question on stack overflow that is quite similar to yours. Enforcing unique usernames with Firebase simplelogin Marein's answer is a good starting point for implementing the server side validation.
First save the user credentials in the realtime database before you create the user:
var rootRef = firebase.database().ref('child');
var newUser = {
[name]: username,
[email]: useremail,
[joined]: date
};
rootRef.update(newUser);
After adding the Usersinfo into the realtime database create a new user:
firebase.auth().createUserWithEmailAndPassword(useremail, userpassword).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// ...
});
When an error occured while inserting the data in the realtime database, it will skip the createuser function.
This works fine for me, hope this helps!

Parse: code 111 - can't add a non-pointer to a relation

I have a bunch of entries in a list view that are created by users. I want users to be able to flag them for review so I set up a 'flaggedBy' relationship like so:
var relation = entry.relation('flaggedBy');
relation.add(Parse.User.current());
entry.save(null,{
success:function(flag){
alert('Entry flagged for review');
}
});
However, when I try to save, I get the error:
code 111 - can't add a non-pointer to a relation
All the other answers for this problem say I'm trying to add a relationship to an object that hasn't been saved yet but I do have a user account. Any ideas?
Turns out Parse.User.current() was null because I was working locally and I was using Facebook as my login mechanism (I haven't set up a local way to do this).

Categories