How to create session variables after Login and destroy it as well after Logout?


#1

Good Day!
Currently I have my application connected to a PHP server using MySQL as my backend service.

I wonder If someone knows on how could I have my session inside my application.
What i want to achieve is/are:

  • Have my LOG-IN Session Variables (Username or/and Password).
  • Use my Session Variables to display a certain user’s Profile details.

Please help me out. I want to start a session at every page whenever I logged in and destroy it as well after i logged out of my application.


#2

Hi there :smile:

I would recommend looking into HTML5 local storage:

http://www.w3schools.com/html/html5_webstorage.asp

You can create a local storage object like so:

$window.localStorage['myStorageObject'] = JSON.stringify(myArray);

And retrieve it like so:

return JSON.parse($window.localStorage['myStorageObject'] || '[]'); 

You could create an object with your required properties on login and save the object in local storage. From there when you logout you can just destroy the local storage object.

I would recommend creating a factory with all your local storage operations and dependency injecting it into which ever controllers would require it.

Hope this help you :smile:


#3

I use this for my project to save local data


#4

I have used ionic’s factory to store the session variable. Got some idea from the link. Thanks!


#5

Got some idea from that. But I have solved my problem using Angular’'s factory service. Thanks a lot!


#6

sir can i ask you did you solve your problem? because i also want to have a login session and destroy it when i logout thank for your response :smiley:


#7

iam waiting too @Pajimola :smiley:


#8

Hi guys!
You might consider using Secure Storage to keep track of your login information, here are two links that I hope will help you.


#9

thx, but i realy apriciate if you have some example code, i feel more confident learn with actual code…


#10

i already solve my problem…