I am currently working on a project for a client. I'm trying to assign roles to users at the time they sign up for an account.
handleSubmit(e){
e.preventDefault();
this.setState({message:'',messageClass:'hidden'});
var that = this;
var first_name = ReactDOM.findDOMNode(this.refs.first_name).value.trim();
var last_name = ReactDOM.findDOMNode(this.refs.last_name).value.trim();
var email = ReactDOM.findDOMNode(this.refs.email).value.trim();
var password = ReactDOM.findDOMNode(this.refs.password).value.trim();
var type = 'fan';
var user = {email:email,password:password,profile:{fullname:(first_name + last_name).toLowerCase(),firsname:first_name,lastname:last_name,avatar:'http://placehold.it/150x150',friends:[],type:type}};
Accounts.createUser(user,function(e){
if (e) {
Materialize.toast(e.reason, 5000);
} else {
FlowRouter.go('/dashboard');
}
})
}
Above is my handle submit event for the signup form. Below is the hook that I added to assign the role to the user after the user is inserted.
Meteor.users.after.insert(function (userId, doc) {
if (doc.profile.type === "fan") {
Roles.addUsersToRoles(doc._id, [ROLES.Fan])
}
});
What I'm finding is that the user gets created but instead of being redirected to the dashboard, the user remains on the signup page and receives an error in the toaster message stating internal server error. When I go into the console to find the user that was created, the profile type is assigned but the user is missing their email address.
This is the error I'm getting in my terminal window:
Exception while invoking method 'createUser' ReferenceError: ROLES is
not defined I20160402-16:04:42.482(-4)? at Object.
(both/collection_hooks/hooks.jsx:3:35) I20160402-16:04:42.482(-4)?
at packages/matb33_collection-hooks/insert.js:35:1
I20160402-16:04:42.482(-4)? at Array.forEach
(packages/es5-shim/.npm/package/node_modules/es5-shim/es5-shim.js:417:1)
I20160402-16:04:42.483(-4)? at Function..each..forEach
(packages/underscore/underscore.js:105:1) I20160402-16:04:42.483(-4)?
at after (packages/matb33_collection-hooks/insert.js:34:1)
I20160402-16:04:42.483(-4)? at
Object.CollectionHooks.defineAdvice.self
(packages/matb33_collection-hooks/insert.js:49:1)
I20160402-16:04:42.485(-4)? at Object.collection.(anonymous
function) [as insert]
(packages/matb33_collection-hooks/collection-hooks.js:117:1)
I20160402-16:04:42.485(-4)? at [object
Object].Mongo.Collection.(anonymous function) [as insert]
(packages/mongo/collection.js:590:1) I20160402-16:04:42.485(-4)?
at AccountsServer.Ap.insertUserDoc (accounts_server.js:1248:25)
I20160402-16:04:42.486(-4)? at createUser
(password_server.js:980:25)
Does anyone have any idea what exactly is happening?
The variable ROLES isn't defined in the context of your hook. It's either not a global or you're using Meteor 1.3 and you haven't exported it from the file you've defined it in and imported it into the file where you defined your hook.
Related
The Strophe onMessage handler that I added to the connection doesn't seem to trigger whenever a message gets send. I can't seem to find the problem. I can't find a lot of other info either and the info I do find seems to suggest my code is correct. I can send messages, so I know the connection works, but I cannot receive messages.
I create the connection like this, and then call the login function if a new connection is made:
XMPPChatConnection() {
if (this.#connection === undefined) {
this.#connection = new XMPPHelper.Strophe.Connection(
"wss://xxxxxxxxxxxxxxxxxxxxxxx",
{protocol: "wss"}
);
this.login();
}
return this.#connection;
}
The login function calls the chatListeners function which should setup all the listeners that are required when the user is logged in:
login() {
let jid = this.composeJabberIdentifier();
let token = this.getXMPPToken();
this.#connection.connect(jid, token, (status) => {
if (status === XMPPHelper.Strophe.Status.CONNECTED) {
this.chatListeners();
}
});
}
The messageListener is an imported function and currently only contains a console log:
import messageListener from "../classes/listeners/xmpp/chat/messageListener";
chatListeners() {
this.XMPPChatConnection().addHandler(messageListener, null, 'message', 'chat');
}
messageListener:
export default function messageListener(message) {
console.log(message);
}
What did I do wrong?
So I found the cause of my problems. I was using the Xabber client to send messages back, but it turned out Xabber sent the messages to the wrong resource.
On top of that I should have set my presence after login with a priority of >= 0.
this.XMPPChatConnection().send($pres().c("priority").t("0"));
I can't link two email accounts using Firebase.
It sends back to me an error -"Uncaught TypeError: Cannot read property 'linkWithCredential' of null" !
Can you help me?
My code is :
<script>
function linknew(email, password){
var authcredential = firebase.auth.EmailAuthProvider.credential(email, password);
var auth = firebase.auth();
auth.currentUser.linkWithCredential(authcredential).then(function(usercred) {
var user = usercred.user;
console.info("Account linking success", user);
}).catch(function(error) {
console.error("Account linking error", error);
});
}
</script>
your currentUser variable might be not initialized, hence it is null and you cannot access the property linkwithcredential, to guard yourself from this situation, you can add an observer to your auth object, like this:
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
} else {
// No user is signed in.
}
});
check firebase: manage users, check the first star note as well:
Note: currentUser might also be null because the auth object has not finished initializing. If you use an observer to keep track of the user's sign-in status, you don't need to handle this case
Check whether page elements which you are trying to access in JS is loaded before scripting.
$(window).bind("load", function() {
// code here
});
or
document.addEventListener("DOMContentLoaded", function(event) {
console.log("DOM fully loaded and parsed");
// code here
});
Check this Link for more info
Need to send a custom error message in keycloak script based authenticator.
On failure it showing same error message Incorrect email or password. Please check and try again. How to send a custom error message?
Code:
function authenticate(context) {
var username = user ? user.username : "anonymous";
var authShouldFail = false;
if (username=="anonymous") {
context.failure(AuthenticationFlowError.INVALID_CLIENT_CREDENTIALS);
return;
}
context.success();
}
I searched source code of keycloak repository and finally came up with a solution. The answer is to use setError method to show custom error messages and use context.failureChallenge function instead of context.failure like the following code:
// import the required Java classes
AuthenticationFlowError = Java.type("org.keycloak.authentication.AuthenticationFlowError");
Response = Java.type("javax.ws.rs.core.Response");
Errors = Java.type("org.keycloak.events.Errors");
function authenticate(context) {
var showCustomError = true; // you need to make your own controls to set this property
if (showCustomError) {
var errorMessage = "this is custom error message"; // set your custom error message
context.getEvent().error(Errors.IDENTITY_PROVIDER_ERROR);
var challengeResponse = context.form().setError(errorMessage, []).createErrorPage(Response.Status.INTERNAL_SERVER_ERROR);
context.failureChallenge(AuthenticationFlowError.IDENTITY_PROVIDER_ERROR, challengeResponse);
return;
}
context.success();
}
Another quickblox question - I am getting the following error when trying to run my application:
Uncaught TypeError: Cannot read property 'sigBytes' of undefinedquickblox.js:10707 c.HMAC.i.extend.initquickblox.js:10703 i.Hasher.a.extend._createHmacHelperquickblox.js:101 signMessagequickblox.js:35 createSessionquickblox.js:867 QuickBlox.createSessionchat.html:122 (anonymous function)
This error prevents successful authentication of the user. The code being executed is below(insecure as hell - proof of concept only!):
var chatToUser = window.localStorage.getItem("chatToUser");
var username = window.localStorage.getItem("user");
var password = window.localStorage.getItem("pwd");
var params = {login:username,password:password};
console.log("PARAMS: ",params);
$(document).ready(function(){
var chatService = new QBChat(params);
});
// JavaScript SDK initialization
QB.init(QBAPP.appID, QBAPP.authKey, QBAPP.authSecret);
// QuickBlox session creation
QB.createSession(params, function(err, result) {
if (err) {
console.log("ERROR:", err.detail);
} else {
chatUser = {
id: result.user_id,
pass: params.password
};
connectChat();
}
});
function connectChat() {
chatService = new QBChat({
onConnectFailed: onConnectFailed,
onConnectSuccess: onConnectSuccess,
onConnectClosed: onConnectClosed,
onChatMessage: onChatMessage
});
console.log("Chat Service: ", chatService);
console.log("Chat User: ", chatuser);
// connect to QB chat service
chatService.connect(chatUser);
}
/* Callbacks
------------------------------------------------------*/
// Connection is failed
function onConnectFailed() {
alert("We're having some difficulty talking to the server. Please try again later, or get in touch if th eproblem persists.")
}
// Connection is success
function onConnectSuccess() {
//green dot - live - status
alert("Connected.")
}
// Connection is closed
function onConnectClosed() {}
What do i need to do differently in order to successfully log the user in for chat?
If it helps, I have previously generated a session for them based on their 'user' credentials (username/password). Is it possible to simply reuse this session instead of creating a fresh one?
It just hit me! (adding a few console.logs helped...)
The app is looking for QBAPP.(etc) and the case/underscore formatting of those parameters did not line up after copying from an examples, hence the 'undefined'-s coming back and the sigbytes thingy falling over itself. So note to anyone else having this error - check your code carefully, for case and style before nagging the community with stupid questions!
I hope someone might learn from this.
I am trying to show a pop up error message to user if folder creation fails because it already exists, but access Denied exception is thrown on msg.showAsync()
sf.getFolderAsync(KorN_Name).then(function (kn) {
return kn.createFolderAsync(username, Windows.Storage.CreationCollisionOption.failIfExists)
}).done(function (user) {
//some code
},
function (error) {
var md = Windows.UI.Popups.MessageDialog;
var msg = new md("User already exists", "Ooops!");
msg.showAsync();
});
How to fix it?
Try this code. It might be failing because you initialized the messageBox differently:
var msg = new Windows.UI.Popups.MessageDialog("User already exists","Oops!");
msg.showAsync();
See this link if you have not already done so.