deprecated.cookie
This is a topic that many people are looking for. thevoltreport.com is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, thevoltreport.com would like to introduce to you OAuth (Passport.js) Tutorial #16 – Cookie Session. Following along are instructions in the video below:
hey gang welcome to your, 16th Alworth tutorial and in this video were going to talk about our cookie session alright so in the last video we had a look at these two functions right here and serialize user is taking a user right here from the callback function and its saying well okay I want to take and identify and piece of information from that user so that we can stuff it in a cookie send it to the browser so we still not its grab the ID and its called the done method which is said okay Im done here Im just gonna use this to stuff in the cooking vice versa deserialize user is taking an ID when the browser sends the cookie back and we receive that ID and then were finding the user based on the ID and were calling done and passing in that user so what this is gonna do for is later on is attach the user property to the request object inside this thing down here okay so that we can access it in a route Handler and were gonna see that later on but for now I want to set up our application so that we can use session cookies so to do that were gonna have to install another module and thats called cookie session so lets
go to app KS were gonna have to require that there in a minute but the first thing I want to do is open up the console and Im gonna say npm install and its called cookie – session okay so thatll just take a minute to install ill see you when its complete thats all done Maya now so the next thing I need to do is require cooking session inside this app J so Im going to come under keys and Ill say Const and call this cookie session and set that equal to require and its cookie session okay so were gonna use this cookie session to control our session I will use this session right so its gonna take our cookie set a max age to it and encrypt it using a key and send it to the browser because we dont want to send our normal ID to the browser because then anyone can see it its using that browser we want to encrypt it first of all using the key and then send thats the browser then when they cookie comes back to us we can decrypt it okay so the way were going to set up this cookie session to run is by using it down here so Ill say app dont use and were gonna use cookie session like so and
this takes in an object as a parameter and this object needs two properties first of all the max age so this is gonna be the max age of the cookie that we send out so for example if I want one day this value needs to be in milliseconds so Ill say 24 for 24 hours in the day times 64 minutes in an hour times 64 seconds in a minute and then times 1000 for milliseconds in a second so now I have a day in milliseconds inside this max age profit awesome the next property is going to be a property called keys so like I said we want to encrypt our cookie we dont just want to send the user ID straight to the browser so this array right here can take any number of keys and its going to use one of those keys to encrypt that ID so that its encrypted by the time it reaches the browser so we can pass in a string here of anything you want like that and thats going to encrypt it however Im not going to store this key right here Im going to store it in our keys file okay so we can keep it a little bit more private so Ill delete that from here and save it and go over to keys yes
all right here is where Im going to add in the key for our cookie session so well add in a property called session which is an object then the cookie key is gonna be right there and lets just make him a random string so Im gonna say the net ninja is awesome I guess Im guessing no ones gonna guess that that comes to my website so thats going to be my cookie key but its going to use to encrypt the ID so lets use this now over here weve already imported the keys so I can say Keys dot session dot cookie key okay so were going to use that now this thing right here to encrypt our cookie when we send it to the browser so the next thing we need to do inside this file is to say okay we want passport to initialize first of all and then to use cookies we want to use session cookies right so just two quick lines we need to do to say that so lets just say initialize passport right here and then underneath Im going to say app use and weve already imported passport half way no weve not so lets import that first of all stay down here Const passport is equal to require passport like so then down here Im gonna say
app dot use and this is some middleware were using right here Passport dot initialize so were initializing passport in our application right here I can never spell this initialize okay so thats a function and then the next line we need to write is app dot use again to say that we want to use passport dot session to control or login in okay so we use in session cookies okay so thats really all we need to do now to set up our cookies so now whats going to happen is when we see realize this user were going to attach this user ID to a cookie then this thing over here is gonna encrypt that cook in and make sure that its just a day long then its going to send it to the browser and browser is going to receive that cookie ok and this is all happening once a user is logged in because remember once theyve logged in were firing this callback function right here which then skips over here it says dawn etc and it goes on and on and it sends that response to the user alright so now weve set up our cookies in the next video were gonna test it out and see if these cookies are reaching the browser and what happens when the browser makes another request
tags:
oauth, oauth 2, passport, passport.js, passportjs, passport js, oauth passport, passport tutorial, oauth tutorial, authentication, node auth, node passport, …
Thank you for watching all the articles on the topic OAuth (Passport.js) Tutorial #16 – Cookie Session. All shares of thevoltreport.com are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.