Is there a Client.fetchUser() equivalent in Discord.js? - javascript

I was trying to find a way to get a User object from a user's ID alone. Client#fetchUser() seemed to be what I was looking for, referring to this
older question.
However that function is no longer in the Discord.js Client Object Documentation
And I can't seem to find a way of doing what fetchUser() is supposed to do. I need to create User Object from User IDs because I'm retrieving saved user data and need to be able to send messages to the users I'm retrieving from save data.

There are 2 ways to go about doing this:
Getting the user from the client.users.cache collection
const user = client.users.cache.get(UserID)
Fetching the user using the fetch() method in the UserManager of client.users
const user = client.users.fetch(UserID)

It seems they merged the functionality of UserManager#fetch()
into what I was looking for, in which it is described as
Obtains a user from Discord, or the user cache if it's already available.
PARAMETER TYPE OPTIONAL DEFAULT DESCRIPTION
id Snowflake
ID of the user
cache boolean true
Whether to cache the new user object if it isn't already
Returns: Promise<User>
So it'll either grab the user from the cache by ID, or retrieve it from Discord if it's not in the cache. I'm guessing they did this to make this functionality more efficient.

Related

Firebase generating two different uid's for the same user?

Currently working on a React project that utilizes Firebase to store users. To make the user, I use createUserWithEmailAndPassword(). When the user is created in Firebase, it stores the user under an id that looks like -N4xrZ...
However, when I make a call to get the user's UID (through userCredential.user.uid), it returns a much longer one that looks like a typical UID generated by Firebase and that isn't the same as the user's parent id. Any ideas as to what this may be?
It stores the user under an id that looks like -N4xrZ...
That's the expected behavior since you're using push(). This function generates a unique ID each time is called.
When I make a call to get the user's UID (through userCredential.user.uid), it returns a much longer one that looks like a typical UID generated by Firebase and that isn't the same as the user's parent id.
The UID that comes from the authentication process, it's different than the one that is generated by push(). It's actually the same, each time you launch your app. So when you write data to the database, stop using push() and use the UID:
firebase.database().ref('users/' + userCredential.user.uid).set({
username: name,
email: email,
profile_picture : imageUrl
});

How can I construct a user from a user json?

I am using the CDN import method for SDK v9. I would like to know, given that I have a JSON-serializable representation of a user object, how can I construct a user object, so that I can use it with methods like updateCurrentUser.
If there was a way that I could access the UserImpl class like you can with npm
import { UserImpl } from '#firebase/auth/internal';
I'd be able to do this. However, I am stuck with the CDN import method, which doesn't allow access to the UserImpl class.
I understand that this may be a strange question, but understand that the way this is set up, once the user logs in, its json representation is saved elsewhere and the user object itself is lost.
I guess a better question would be, how can I deserialize a user?
An important piece of information here is that I want to be able able to deserialize users that once logged in. At some point of the app's lifetime, a user logs in(multiple users might log in and out), the user object is lost, and its json representation is saved elsewhere. I need to be able to retrieve the user object from its json representation later.
A potential workaround that seems to work at first glance, is to save a few informations from the first user that ever logs in:
const userInfo = {}
Object.setPrototypeOf(userInfo.stsTokenManager, auth.currentUser.stsTokenManager.constructor.prototype)
userInfo._clone = auth.currentUser._clone;
userInfo.auth = auth.currentUser.auth;
//and then I apply this to any user json I need to later on
user._clone = userInfo._clone;
user.auth = userInfo.auth;
user.stsTokenManager = userInfo.stsTokenManager;
//if I add the _clone,auth and stsTokenManager,I don't get errors andthe
//user json seems to become a user object for all intents and purposes,
//however I don't know if this is very safe and works in all situations.
//would applying the same clone,auth and tokenManager to all json users
//work?
Unfortunately, using the firebase admin sdk is also not an option because this is a client side app.
Is there any better solution? Thank you for your time.

Getting a sub-collection by a specific field value in Firestore

How do I find a subcollection based on a field value? I am currently using this code but it doesnt work:
var user = db()
.collection('myCollection')
.doc()
.collection('private')
.where("nam", "==", this.state.passcode);
What I am trying to achieve is making a custom authentication, so giving a custom username and password to users (in this case just a passcode). So I decided to store those credentials in a separate sub-collection inside a document. How can a user authenticate by comparing the values of username and password with the ones of a sub-collection?
Another question, is sub-collection for credentials a good idea? Will it cost the same to me as if I would store those info in the document?
First of all, what you're doing right now is not secure at all. You should never store user credentials in a database, especially not one that's directly accessible to your web and mobile clients. To do this properly, you should be making use of Firebase Authentication to sign in users. (You tagged this question firebase-authentication, which refers to that product.) In fact, doing security properly is very difficult. Firebase Auth will make sure everything is done correctly.
Secondly, the query you have now will never yield any documents. That's because you're not passing anything to doc(), which means it will return a DocumentReference to a non-existent document with a random ID. If you meant to have some sort of unique identifier for each user, perhaps that's something you would want to pass to doc() so that each user's subcollection would be correctly identified.

Laravel: How to hide/make user id uneditable in JS when using Brain Socket

I have a universal used_id that will be used in brain socket (https://github.com/BrainBoxLabs/brain-socket) in Laravel like this:
window.userId = {{ $user->id }};
This will be used when receiving notifications,messages,etc. Problem is, since it is JS, window.userId can be edited in the DOM (like using firebug). So the user can get notifications from other users, which should not be.
Are there any countermeasures for this? (I'm using Sentry Package to get the user id)
I've been thinking to use unique channels for each user instead, based on their ID, but will that be overkill? And i still have no idea how to do this in laravel?
You can't prevent users to edit javascript. You should use a specific kind of ID that we name "token", it's a key with a lot of every character, it's prevent users to guess the token of an other user.
You have "uniquid" who return a unique string, based on time, it can be guessed, but you can use it to assert unicity of your token. You can add informations such as user id, name, random string, maybe some salt, and then hash it.
You can use hash function
http://php.net/manual/en/function.hash.php
or mcrypt (you'd better use this one) :
http://php.net/manual/en/book.mcrypt.php

Rally App SDK 2.0: Filtering a store of Users by UserPermissions

I am attempting to build a store that consists of only Users that are not disabled and have editor permissions on the selected Project. However, UserPermissions is an array of UserPermission objects, which makes such a filter rather complex. It seems that a UserPermission can be of the WorkspacePermission or the ProjectPermission type. So, it looks like I would first have to check the type of the UserPermission, and then check whether the _refObjectName contains the name of the selected Project and "Editor," OR, alternatively, delve deeper into the ProjectPermission itself and check the Project's name and the Role for "Editor" separately. I have attempted to use the filterBy function, passing in a function that takes a record and an id, but I am unable to hit the breakpoint I set inside that function, so I have no idea whether it is working. The results displayed in a combobox certainly do not seem to match what I am querying for. Any suggestions?
Edit1: I have tried modifying the query to scope to the particular project name, but I receive this error: "Could not parse: Attribute \"Project\" on type ProjectPermission is not allowed in query expressions." Is there any way to get around this?
Here is the modified query, excluding the actual project name:
https://rally1.rallydev.com/slm/webservice/x/ProjectPermission.js?pagesize=1&fetch=true&includeSchema=true&includeMeta=true&query=%28%28Role%20=%20Editor%29%20AND%20%28Project.Name%20%3D%20%22{PROJECT NAME HERE}%22%29%29
Edit2: Just thought someone might want to know. I received this error when trying to add another parameter to the query to filter by a User's DisplayName.
"Could not parse: UserPermission does not support complex queries yet. You can only query by Role OR by User, not both."
Edit3: I am having additional trouble with a call to get all the Project Permissions for a User based on their DisplayName. The query, as far as I'm concerned, is formatted as it should be. However, only results for some users are returned, and I have no idea why other users aren't included. I have even tried modifying it to query on FirstName, and the permissions for the user are not included in the result set.
https://rally1.rallydev.com/slm/webservice/x/ProjectPermission.js?pagesize=200&fetch=true&includeSchema=true&includeMeta=true&query=%28User.DisplayName%20%3D%20%22{INSERT NAME HERE}%22%29
You can query the Permissions endpoint to make sure that the that permission is for an Editor you can see the json for that request by following this link.
To get the list of Users that are active you query them with disabled = false like this.
Once you have Permission data in memory you can filter out the users do not that have Edit permissions and match them with the list of active users.

Categories

Resources