Possibility to remove Parse.User.current() storing in localStorage - reactjs

Is it possible to remove the object storing automatically in localStorage upon user logged in? Since this is in their documentation.

Parse.User.logOut() does this automatically but I suppose you are looking for a manuel way to do it.
Information of current user stores in localStorage as "Parse/APP_ID/currentUser"
APP_ID is the key you use when you initialize your Parse SDK
Parse.initialize("APP_ID", "JAVASCRIPT_KEY");
You can use removeItem() method in localStorage.
removeItem("Parse/APP_ID/currentUser")
If you want to do it with every login
Parse.User.logIn("myname", "mypass").then(user=>{
localStorage.removeItem("Parse/APP_ID/currentUser");
})

Related

How do i use this fetched data response?

I Am using react. I have a fetch setup in an useEffect to grab the data from a database from the backend. It pulls the account and sends it back to the front where i can access it from there.
From the File I want to access the user information i have a console log
I want to be able to grab like the firstname, lastname individually and use them in the script. how do i go about that. I did try to console log UserData.user and it gave me this result
However when i went to try to get firstname like userData.user.firstname i was met with an error. Im pretty new to react and pushing myself with things ive never done before to learn and any help would be great
Thank you everyone who does help it means alot
Please note all information in screenshots are fictitious and are just prop data.
EDIT:
This may be because you are trying to read the user information before the api returns the data. so check if the array has data before trying to access it . You can use save navigation to check for undefined or null
UserData.user[0]?.firstname
Based on your screenshot the data you are getting back is an array of user objects. To access it you will need to use:
userData.user[0].firstname
Note that this will access the first user object in the array - you will likely need checks to ensure the data data exists before accessing it, e.g.:
if (userData.user.length > 0) {
// Access data as needed
}
user is an array so you can acces like this
userData.user[0].firstname;
it will be better if you update your endPoint to get an object instead, so you can have access and check easier

Trying to access information within localStorage

I am trying to access information with-in localStorage from one component to another, specifically the name of the user on their profile. I am able to access up to the object holding all of the profile information like name, email, address etc. However when I try to extract just the name I am unsure of how to do so, or if its even possible.
right now im trying...
console.log(localStorage.document)
this gets me the an object holding all the data in localStorage...
{"name":"John Doe","email":"johndoe#aol.com","phone":"0000000000","address1":"123","address2":"123","addresscitystate":"Seattle, WA","addresszip":"00000"}
yet I cant seem to access any further then that. I attempted to just do [0] at the end to get the index but im getting just the letter back for that specific index not the actual object I want.
Any advise is appreciated!
May be you can try this one, For extracting the specific property from localStorage you can do this:
let item = localStorage.getItem("name")
The way to do this is.
First you need to extract the localStorage item
let savedItem = localStorage.getItem("username")
"Username" is the name of the item that is save in you localstorage.
The Complete guide for the localstorage usage is as follow.
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

Angularjs 1.5 localstorage getting overwrite when i open application in new tab with some other credentials

In angularjs 1.5 I have used local storage to store session related information.
But when I open new tab with some other credentials, initial information is getting erased and getting replaced by the new one, is there any way to prevent this ?
Yes there is, What you are doing is replacing the same key with new data that is why it is getting replaced. What you should be doing is create new 'Key' for each session.
Example
//localStorage.setItem('key', 'value' )
//What you should do is for each login user create a new key
//sessionId can be any id you want for new session, this way you won't overwrite the previous session data
localStorage.setItem(sessionId, loginSessionData);
Since you didn't provide any code sample it's hard to tell what is going on, but i guess when the app starts you set some value to localstorage, so each time you open a new tab you set it again and override the previous value.
All you need to do is check if the value exists in localstorage and if it does do nothing:
if (!localStorage.getItem('myItem')){
localStorage.setItem('myItem', myValue));
}

how to do terms and agreement show only once for one user?

I have a login page,
when a user login for the first time it should show the terms and agreement popup with accept button.
if user is accepted it for the first time . when user login for next time popup should not come.
i want to use local storage for this function no db changes allowed.
so what i have to save in localstorage. the scenario is complex when this has to work for diffrent users.
in angularjs i m using ngstorage plugin.
i can save data into local storage like
$localStorage.data="username";
in this case how i have to save for diffrent diffrent user?
You can have a key value pair, where key indicates the username and value indicate whether it has been loaded or not and push them into an array.
Something like this,
var testObj = { 'John': true };
// Put the object into storage
localStorage.setItem('John', JSON.stringify(testObject));
Similarly for all the users.
Then retrieved by using username,
var retrievedObject = localStorage.getItem('John');
console.log('User has already loaded: ', JSON.parse(John));
DEMO APP USING angular-local-storage

how to find out my current user id in other page controller after i login?

i am planing to set a permission on my event index page, which just allow certain user to view which had set when i add the event. After user click into my event, the event controller will 1st check the user id and check the event database which control the user can see which event in his calendar. The permission is added when user create a event and share to other user. Beside, how can i find the current user id to compare with my event database which is the accurate 1?
any suggestion for me to did this function?
i need to know the code and concept how i get the current user id to compare with all the event database, and allow the current user see the certain event.
thanks alot for your information.
The recommended approach for getting logged in user data is via the AuthComponent itself:
// in any controller
$userId = $this->Auth->user('id');
See Accessing the logged in user in the Auth section of the CakePHP Book.
Use sessions to save and read data for a user between pages.
Within Controllers:
// store a user id in the session
$this->Session->write('User.id', $userId);
// read a user id from the session
$userId = $this->Session->read('User.id');
Within Views:
// read a user id from the session
$userId = $session->read('User.id');
You can use any key you want if you prefer something over "User.id". I simply use this since it is what the AuthComponent defaults to if you are using that.
What you're looking for are ACLs (Access Control Lists). There's an AclComponent built into Cake which you should look into. It works together with the AuthComponent, which will hold the user id. It's a little complicated at first, but worth the hassle.
Also, for a simple approach, have a look at the model and controller settings of AuthComponent::authorize. This allows you to define an isAuthorized() method in your controller or model (your choice) which will store logic that determines access (should return true if access allowed and false if denied).
to see sessions, queries, data, and everything else that is passed from page to page in cake use this amazing little helper http://thechaw.com/debug_kit

Resources