Angular-Full-Stack icon indicating copy to clipboard operation
Angular-Full-Stack copied to clipboard

Link cats with users

Open felixchristo opened this issue 7 years ago • 18 comments

How to maintain session with this so that each user can maintain their own data

felixchristo avatar Dec 26 '17 07:12 felixchristo

The session of the login is already mantained with the token in localstorage. Each user that login have its own token.

DavideViolante avatar Dec 26 '17 14:12 DavideViolante

Yes sir, But the data of one user is visible to other. How to show the data which is related to that particular user alone

felixchristo avatar Dec 27 '17 09:12 felixchristo

But the data of one user is visible to other

What do you mean? Tell me what you need to do in details.

DavideViolante avatar Dec 27 '17 12:12 DavideViolante

If X login and Enter details and then Y login, Y able to see X's details and X able to see Y's details.

felixchristo avatar Dec 27 '17 12:12 felixchristo

Are you talking about the cat data? Add user id to each cat and amend the queries to filter data unique to each user?

On 27 Dec 2017 12:17, "felixchristo" [email protected] wrote:

If X login and Enter details and then Y login, Y able to see X's details and Y able to see X's details.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/DavideViolante/Angular-Full-Stack/issues/145#issuecomment-354105825, or mute the thread https://github.com/notifications/unsubscribe-auth/ACm_CVH7uT-I-TK3Xj0QyDJ6OX7wcp4qks5tEjVpgaJpZM4RMk4G .

dougal83 avatar Dec 27 '17 12:12 dougal83

I think I need to edit base.js for that?

felixchristo avatar Dec 27 '17 12:12 felixchristo

If you could do that and update it, It will be very useful to all and the project will look meaningful

felixchristo avatar Dec 27 '17 13:12 felixchristo

I think this would be a good challenge to learn for you as this project is a starter to build on.Base.js would be a good start.

When personalising the cat data that is held in the database, you might want to consider whether you actually need a database? Local storage would be an option.

On Wed, Dec 27, 2017 at 1:09 PM, felixchristo [email protected] wrote:

If you could do that and update it, It will be very useful to all and the project will look meaningful

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/DavideViolante/Angular-Full-Stack/issues/145#issuecomment-354111695, or mute the thread https://github.com/notifications/unsubscribe-auth/ACm_CQI7Is_WB0yW6BYo1gNfm08ZD-R3ks5tEkGXgaJpZM4RMk4G .

dougal83 avatar Dec 27 '17 13:12 dougal83

The user data is private, the cat page and data is visible to all users, including the guests. I though about the cats as the "products" that can be seen by everyone. Currently there is no association between users and cats, since this is just a starter project and is meant to be simple. If you want to create a link between them, you should edit the user schema as follows: cats: [{ type: Schema.Types.ObjectId, ref: 'Cat' }] The important word here to note is ref. This creates a reference between User schema and Cat schema. So each user in this way will have an array of cats, their cats. You can find some tutorials online about this topic, search for "mongoose ref" http://mongoosejs.com/docs/populate.html

DavideViolante avatar Dec 27 '17 13:12 DavideViolante

I am just getting this "_id" : ObjectId("5a446ab43533970b8489e1ac"), "username" : "Chris", "email" : "[email protected]", "password" : "$2a$10$ogerY6OiCRKy9TjPYERaOugUJeqelBl.yToJ4ZBX3ac2MVZQpsKOu", "role" : "user", "cats" : [ ], "__v" : 0

If I do cats: [{ type: Schema.Types.ObjectId, ref: 'Cat' }] in user.js

felixchristo avatar Dec 28 '17 04:12 felixchristo

It's not that simple, I just gave you an hint. Every time you do the add new cat you should update the cats array of the user that added that cat.

DavideViolante avatar Dec 28 '17 11:12 DavideViolante

Still I couldn't do this :(

felixchristo avatar Feb 19 '18 02:02 felixchristo

you should add createdBy in cat model , save userid on it then use ngif to filter cats list it's already worked for me : 1- in authService for me i call it current code : get current() { return this.currentUser; } 2- in cat component add current = this.authService.current._id; add in ngOninit() : this.addCatForm = this.formBuilder.group({ . . . createdBy: this.authService.current._id });

3- in cat html file use this condition : code : tbody *ngIf="!isEditing" tr *ngFor="let cat of cats" span *ngIf="current == cat.createdBy" td>{{cat.title}}</td td>{{cat.content}}</td td> #note : don't forget to import the services and add createdby in ur server side

issambaccouch avatar Feb 22 '18 14:02 issambaccouch

logically sounds good buddy.. Let me try and get back to you

felixchristo avatar Feb 23 '18 06:02 felixchristo

yeaahhh it's working but the thing has to be done from server side buddy not from client side, which is not safe..

felixchristo avatar Feb 28 '18 03:02 felixchristo

I'd recommend posting somewhere like stackoverflow.com for questions such as this. There are plenty of helpful people on those question sites and maybe already an answer that can help you.

On 28 Feb 2018 03:01, "felixchristo" [email protected] wrote:

yeaahhh it's working but the thing has to be done from server side buddy not from client side, which is not safe..

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/DavideViolante/Angular-Full-Stack/issues/145#issuecomment-369105455, or mute the thread https://github.com/notifications/unsubscribe-auth/ACm_CV3DyLGQhUIFyZ9fUQSIrzuHls0Bks5tZMF6gaJpZM4RMk4G .

dougal83 avatar Feb 28 '18 09:02 dougal83

I think you can use the save(user) function ! I wanted to do same thing as you and I did it like that :

1)Add array property cats to user.model ( as davideviolante said above you can also use populate method, which may be more appropriate) 2) In cats.component.html add form (ngSubmit)="save(user)" and input [(ngModel)]="user.cats" 3)In cats.component.ts : import user.modele and user.service 4) In cats.component.ts : add getUser() and save(user: User) 5)Add {{user.cats}} to cats.component.html

you can check my repo I did it with film.detail.component.ts (instead of your cats I used films)

adwulfran avatar May 21 '18 16:05 adwulfran

yeaahhh it's working but the thing has to be done from server side buddy not from client side, which is not safe..

Can you provide me with the project after you modifided it?

wxyzed avatar May 07 '19 12:05 wxyzed