Sequelize version: 5.9.0
I have two kinds of users AdminUser & ClientUser. There is a Bookmark model that belongs to both types of users. Simply admin users will bookmark client users. Expecting that if Admin user or Client user get deleted Bookmark row will be deleted too. But instead it's just saving null. Any solution?
// In AdminUser Schema -
AdminUser.hasMany(models.Bookmark, { onDelete: 'CASCADE' })
// In ClientUser Schema -
ClientUser.hasMany(models.Bookmark, { onDelete: 'CASCADE' })
// In Bookmark Schema -
Bookmark.belongsTo(models.AdminUser)
Bookmark.belongsTo(models.ClientUser)
Related
I have recently created a website on Wix that requires you to log in to access the "Client Portal" member page I created. The client portal page is essentially a database (collection) in table form that allows users to select and view documents pertaining to their company. The ClientPortal2 collection has 3 fields: the name of the document, the actual document, and the company it is affiliated with. The Client Portal database is shown below. I want to restrict users to only be able to see the documents that are affiliated with their company so I made another collection called Users2. Users2 references all of the users that have an account in a multi reference field (email) and the company that they work for in another field (company). Users2 is also shown below. In summary, I want users to ONLY be able to look at their company's documents and no one else's.
CLIENTPORTAL2
Users2
The code I wrote to sort the documents based on the certain user's affiliated company is as shown below:
import wixUsers from 'wix-users';
import wixData from 'wix-data';
$w.onReady(function () {
let user = wixUsers.currentUser;
user.getEmail()
.then( (currentEmail) => {
let userEmail = currentEmail; // "user#something.com"
} );
user.getEmail()
.then( (currentEmail) => {
console.log(currentEmail);
wixData.query('Users2').include('email')
.find().then((results) => {
for (var i=0;i<results.items.length ;i++) {
for (var j=0;j<results.items[i].email.length ;j++) {
let userLoginEmail = results.items[i].email[j].loginEmail;
if (userLoginEmail === currentEmail){
let userCompany = results.items[i].company
filter(userCompany);
}
}
}
})
})
});
function filter(Company){
wixData.query('CLIENTPORTAL2').eq("company", Company)
.find().then( (results) => {
let tableData = results.items;
$w('#table1').rows = tableData;
console.log(results.items);
console.log(Company);
})
}
The code runs a query on 'Users2' and looks at all of the users' email addresses and compares it to the current logged in user's address. It then runs a query on 'ClientPortal2' using the users affiliated company it just found in the first query. The table (#table1) displayed on the Client Portal page now displays the documents pertaining to the company the user works for. So for example, if 'dew0025#auburn.edu' was logged into the site, that user would only be able to view the documents in the table below because that user is restricted to 'Company 1'
table1 displayed on Client Portal Members Page
So this code works well but it only works on the preview page and not the live published page. What am I doing wrong? Is there an easier way of doing this or am I going about this the wrong way? Thanks in advance.
Preview mode uses the Sandbox database and live sites use the live database. If you insert an item in the sandbox database it will not appear in the live database unless you sync your databases.
Also check for permissions on the database.
I would like to make an application in React Native that allows to work in two modes, parent and child. The initial stage is registration with Firebase, then after adding additional information about the role (parent / child), registering both the child and parent, and after logging in both of them, the child will share location and parent will receive it.
I would like to add additional fields such as role (parent / child) in my application in React Native + Firebase, to later create other functionalities of the application based on the role.
Registration:
firebase
.auth()
.createUserWithEmailAndPassword(email, password)
.then(userCredentials => {
return userCredentials
.user.updateProfile({
displayName: name,
})
.additionalUserInfo.profile = {
role: role,
}
})
Homescreen
const { displayName } = firebase.auth().currentUser;
const { role } = firebase.additionalUserInfo.profile;
this.setState({displayName, role});
and role returns undefined.
The properties that you can store on a user profile are defined by Firebase Authentication. You can't just add additional properties to it as you see fit. At best Firebase will simply ignore those, but likely it will also explicitly reject them (throwing an error).
If you want to store additional information about a user, you have two main options:
Store the additional information as custom claims in the user's authentication token.
Store the additional information in an external database, such as the Firestore and Realtime Database that are part of Firebase.
While storing the data as custom claims is pretty close to what you want to accomplish, you'll need to keep a few things in mind:
Custom claims are used for authorization purposes, and for that reason can only be set from a trusted environment (such as your development machine, a server you control, or Cloud Functions). So you can't simply set the role from within the app, and will need a separate process to add that claim.
After setting a custom claim on a user profile it may take up to an hour before that change is visible in the client. If you need it sooner, you can force the user to sign in again, or refresh their ID token.
Custom claims are sent with every request you make to Firebase resources, and for that reason are very limited in size. There's a maximum size of 1000 bytes for custom claims for a user. While your current role will easily fit into that, it may limit what you can add later.
If instead you store user data in an external database, you'll typically combine it with other information about that user into a users node/collection. In here you'd store a document/node for each user based on that user's UID, and then their profile information.
So something like:
users: {
uidOfAleksandra: {
username: "Aleksandra",
displayName: "Aleksandra Lastname",
role: "parent",
registrationDate: "2020-02-01"
},
uidOfPuf: {
username: "puf",
displayName: "Frank van Puffelen",
role: "child",
registrationDate: "2015-03-07"
},
}
Having this list of user profiles does not only allow you to store the additional information for each user, but would also allow you to query that list of users from within your app, something that the Authentication API doesn't allow from within application code.
I am creating a site using react-redux-firebase, and when a user logs in with facebook I want to create a profile that stores a user choice. I know how to define what to store with the profileFactory, but I don't know how to pass dynamic data coming from the user. In my case I want to save the users' language. My configuration is similar to this:
const config = {
userProfile: 'users', // where profiles are stored in database
profileFactory: (userData, profileData) => { // how profiles are stored in database
const { user } = userData
return {
language: [i need to put here what the user chose]
email: user.email
}
}
}
The configuration was based on this recipe.
When logging in I'm using type redirect.
await firebase.login({
provider: 'facebook',
type: 'redirect'
})
How could I save the user choice when the user is created?
To solve the user custom fields I'm creating a user when the redirect comes back from facebook.
I'm still not totally sure this is the best way to do it. Seems odd that I can define a profileFactory but can't pass user custom fields in some way.
I just want to know that how can i register user in Firebase and store user's additional details like First name, Last name etc i made a typical registration form in Angular I am successfully registering users with their user name and password but i want to store user's detail on success for future use. I've done this in angular 1 but could not find anyway to work with it Angular2
The best way is to create a user object in your Firebase Realtime Database using the Firebase Authentication uid from the newly created user:
this.af.auth.createUser({
// Create Firebase Auth user
email: formData.value.email,
password: formData.value.password
}).then((user) => {
// User created now create Firebase Database user
return this.af.database.object(`/users/${user.uid}`).update({
firstName: formData.value.firstName,
lastName: formData.value.lastName
});
}).then(() => {
// Success
}).catch((error) => {
// Error
console.log(error);
});
(Not had chance to test this)
Depending on how you've done registration you can obviously replace the formData.value with your model data if needed.
Also, this is done using the AngularFire2 library - https://github.com/angular/angularfire2
I have a React/Redux application that creates and saves new users under Firebase as /users/<uid>
However, I also want to have each user have a custom URL for their profile pages, like domain.com/users/john-david or domain.com/users/john-david-1343 (if the original /users/john-david is already taken.)
What's the best way to do this?
My problem is that I was going to store users under /users/<firstname-lastname-hash> in Firebase, but that seems like a bad idea to store them under anything besides uid (is this true?).
But also, if I don't do that, then how do I maintain /users/<uid> in Firebase, while creating new usernames that account for duplicates, and searching if the user exists when visiting domain.com/users/john-david-123
Here is some sample code of where the new user gets saved to Firebase
export function saveUser (data, user) {
let username = data.username
return ref.child(`users/${user.uid}/info`)
.set({
email: user.email,
firstname: data.firstName,
lastname: data.lastName,
admin: false,
// for /users/<username>
username: username,
uid: user.uid
})
.then(() => user)
}
In general we say you should store data in Firebase how you intend to view it, and there's no reason in particular that you must use the UID as your primary key when storing users. What you might want to is:
Store user data in /users/${username} as you mentioned, including a uid field.
When a user signs up, generate the firstname-lastname slug and store in /uids/${uid} = username.
This way you have a simple way to map from UID to user and vice-versa. The downside here is that once you bake a username into your data structure, you should probably never let them change their username.
Alternatively, you could reverse my suggestion and store /usernames/${username} = uid and /users/${uid}. This makes it easier to rename usernames, but requires that you do an extra lookup to go from username to data.