I am attempting to login users automatically into my rocketchat server via my angular 6 application/component ( where users are already required to login).
Does anyone have any experience directly manipulating the mongodb to login a user with rocket chat? I can update the database with a generated token, but cannot get iframe to read/accept the generated token.
I have direct access to the mongodb/rocketchat database which I am attempting to manipulate to store a token for login access. I am storing a generated token (I am using a UUID) onto the user document under the fields services: { iframe: "generated-token" } where generated-token is replaced with my uuid.
I set the same token under window.localstorage and post a message as requested in the documentation here: https://rocket.chat/docs/developer-guides/iframe-integration/authentication/#managing-mongodb-directly
For the life of me I cannot get this to work. I am not getting any errors in the console - so it doesnt appear to be a CORS issue.
The rocketchat server whilst in development is hosted on a separate machine on my network (http://project-mgr:3000).
I am using my local machine for development (http://localhost:4000).
//component
login() {
//retrieve a list of rocketchat users from mdb
this.rocketService.allUsers().subscribe((au) => {
//retrieve current user from mdb
let user = au[this.user.mail]; //
//generate token
let token = API.UUID();
//manipulate rocketchat mdb entry with token info
user.services = { iframe: token }
//update mdb entry for user
this.rocketService.login(user).subscribe(() => {
//set local storage with token info
window.localStorage.setItem("Meteor.userId", user._id);
window.localStorage.setItem("Meteor.loginToken", token);
//post message
window.parent.postMessage({
event: 'try-iframe-login'
}, 'http://localhost:4000');
});
})
}
ngOnInit() {
window.addEventListener('message', (event:any) =>
console.log(event)
);
this.subscriptions.push(
//retrieve currently logged in user
this.authService.user.subscribe((u) => {
this.user = new User(u);
//login to rocket chat
this.login();
}))
}
//html - not safe, but for development only
<div fxLayout="column" fxLayoutAlign="stretch"
style="height:calc(100vh - 80px);width:100%;">
<iframe #rocketChat frameborder="0" width="100%" height="100%"
sandbox="allow-same-origin allow-scripts allow-forms allow-top-navigation allow-popups
allow-popups-to-escape-sandbox" [src]="'http://project-mgr:3000' | safe: 'resourceUrl'">
</iframe>
</div>
The login interface for rocketchat does not change ( or login ) but there are two messages generated from the event subscription.
They have event data:
data: "" eventName: "unread-changed"
and
data: true eventName: "startup"
I have not been able to figure out if that is useful or irrelevant.