how to assign roles to users with meteor? - meteor

how to assign roles to users with meteor?
I installed the package meteor add alanning: roles, and then I went into command prompt and typed the meteor command shell to access the meteor console server side. and i typed this code:
Var = myId Meteor.users.findOne ({username: "Elsa"}) ._ id
Roles.addUsersToRoles (myId, [ 'admin'])**
to assign the right to the admin user and Elsa in Mongolia I see no added admin roles in the collection

If you use the package
https://github.com/alanning/meteor-roles
Please be sure that username in database is unique.
Try these inside Meteor.startup code much before in server folder, place these
var id = Meteor.users.findOne({username: "Elsa"});
Roles.addUsersToRoles(id._id, ['admin'], 'default-group');
Hope these help

Related

Get custom fields from the directoy

How to access (read) custom fields from the directoy?
If I create the Directory Model the custom field of the directory are not shown.
Has someone done this before with App Maker?
Best regards
Karl
Directory Model is read only. You can try to create Calculated Datasource and call AdminDirectory advanced service to get user records with custom fields:
Enable 'Google Admin Directory API'
Click Settings gear button on the top right
Select App Settings tab
Scroll to Advanced Services section
Add 'Google Admin Directory API' service
Create Calculated Model with directory fields you need (let's name it CustomDirectory)
In the calculated model's datasource server script you can add code like this to query user record from the Directory:
var email = query.filters.Email._equals;
var user = AdminDirectory.Users.get(email);
var record = app.models.CustomDirectory.newRecord();
record.Email = email;
record.FieldA = user.FieldA;
record.FieldB = user.FieldB;
...
return [record];
Query calculated datasource from the client:
var ds = app.datasources.CustomDirectory;
ds.query.filters.Email._equals = 'bob#example.com';
ds.load(function() {
console.log(ds.item);
});
You can even avoid writing client side code, if you use bindings magic.
Notes:
Since I'm not Directory admin, I had no chance to test if all links of this chain work.
Most likely you'll need to deploy your app as developer to let all your app access Google Admin Directory API.
Further reading:
https://developers.google.com/apps-script/advanced/admin-sdk-directory
https://developers.google.com/appmaker/security/identity
https://developers.google.com/appmaker/models/calculated

How to verify users current password?

So, maybe I missed this somewhere in the docs but I couldn't find anything of the sort.
I wan't my users to have to type in their current password to be able to create a new one. From what I understand if the user is authenticated he is able to update his password without providing his current one.
Even if this might be somewhat secure I would rather have him type his old one to prevent people from going on already authenticated sessions from say family members or so and changing the pw.
Is there any way to do this?
(I have no problem using the Admin SDK since I already set up a server for these kind of things)
UPDATE: (Use - reauthenticateWithCredential)
var user = firebaseApp.auth().currentUser;
var credential = firebase.auth.EmailAuthProvider.credential(
firebase.auth().currentUser.email,
providedPassword
);
// Prompt the user to re-provide their sign-in credentials
user.reauthenticateWithCredential(credential).then(function() {
// User re-authenticated.
}).catch(function(error) {
// An error happened.
});
PREVIOUS VERSION
you can use reauthenticate API to do so. I am assuming you want to verify a current user's password before allowing the user to update it. So in web you do something like the following:
reauthenticateAndRetrieveDataWithCredential- DEPRECATED
firebase.auth().currentUser.reauthenticateAndRetrieveDataWithCredential(
firebase.auth.EmailAuthProvider.credential(
firebase.auth().currentUser.email,
providedPassword
)
);
If this succeeds, then you can call
firebase.auth().currentUser.updatePassword(newPassword);

access Mongo replicaset through meteor using username and password

I am using mongo replicaset through my meteor app.
Here is my connection string and code:
var collectionsDB = new MongoInternals.RemoteCollectionDriver("mongodb://usr:pwd#10.9.111.111:27017,user:pwd#10.9.111.112:27017,use:pwd#10.9.111.113:27017/myDB?replicaSet=myRelicaset&authSource=myDB&readPreference=primaryPreferred&w=1");
coll = new Mongo.Collection('<myCollection>',{ _driver:collectionsDB });
Problem:
When I am trying to insert to mongoDB, it tries to insert into Admin db rather myDB. The user usr has readwrite permission for myDB. I fail to understand why it is targeting Admin. As it does not have permission in Admin it through permission error.
However it works fine for individual mongo server without replicaset.
Any thing I am missing in connection string?
thanks
Nihar

Meteor: How can I use the user account information for my app to login to a fork of the Telescope app?

I have an app where users have already signed up. When a user clicks the telescope button in my app, they will be transferred to another server in which I am running my own fork of the telescope app.
Upon going to telescope I want to be able to automatically log the user in with the same account information as in my app. How can this be done? I can adjust the code for both Meteor apps since I am running them myself.
You need to set the Telescope app to use the accounts database of your other application.
You can do this by altering the Accounts.connection to use another Meteor app (your main app)
Add this line into your telescope app somewhere in the /server/lib directory:
var myOtherAppDDP = DDP.connect("http://your-other-app.com");
Accounts.connection = myOtherAppDDP;
This way the DDP connection for your accounts section of your meteor app will use your main apps database.
Next you need to transfer the login-tokens to the other app. For this you need to make a special link with the users current login token:
Template.registerHelper("telescopeUrl", function() {
return "https://your-telescope-app.com/?auto-login?token=" + localStorage.getItem("Meteor.loginToken") + "&userid=" + localStorage.getItem("Meteor.userId");
});
Then you can create a url:
Telescope App
Lastly, you need telescope to intercept these links and log in, you can create a route for this with iron router.
Again, create a file in /client/lib that contains something like this:
somefile.js
Router.map(function() {
this.route("loggin_in", {
onBeforeAction: function() {
var token = this.params("token");
var userId = this.params("userid");
localStorage.setItem("Meteor.loginToken", token);
localStorage.setItem("Meteor.userId", userId);
Tracker.flush();
window.location.href="/";
}
});
});
somefile.html
<template name="logging_in">
<h3>Logging you in</h3>
</template>
I've never tried it personally, but hopefully it is helpful.

How can I store login username as a variable using Meteor?

How can I make a variable for the current user's username as they log in to create a database document for them to store their info?
You need to setup a custom function to configure user creation in server side, see Accounts.onCreateUser on docs.meteor.com
In this function you can initialize your user database document, either in user.field or user.profile.field.
The username is automatically stored in user.username, you do not need to create it.
Then to modify the user record client side, simply call a server method that will update the Meteor.users collection, ie
server/users.js
Meteor.methods({
updateUser:function(fields){
if(!this.userId){
// error : no user logged in
}
check(fields,{/* fields verification */});
Meteor.users.update(this.userId,{
$set:fields
});
}
});
client/main.js
Meteor.call("updateUser",{
"username":"foo",
"profile.bar":"bar"
});
Note that Meteor built-in user accounts greatly simplify all this process : it is well documented so I encourage you re-read that particular section in the docs.

Resources