We'rejustgoingtodo a simple J W T authenticationwillrecreatetokens, senttokenstousersandauthenticatethosetokensonourserver.
Andinthesecondpart, we'regonnatalkabouthowwecanuse a refreshtokeninordertoautomaticallyrefreshourJWTtokenstoincreasethesecurityofourserver, andalsorevokedprivilegesfromusersthatwenolongerwanttoaccessourserversimilartohow a logoutfunctionworks.
Sowedon't havetomanuallycloseandrestartourserverourselvesAndassoonasthatfinishesdownloading, we'regonnacreate a scriptwhichisgoingtoallowustostartourserverusingnodemoneyreallyeasily.
Andwe'regonnadothaton a filecalledserverdotJason, thisisgoingtobeourmainserverfile.
Butas I mentioned, that's goingtobein a separatevideowhich I'vealreadydoneandyoucancheckoutnowthatweknowthisuserhasaccesstoourapplicationandhaspassedthecorrectusernameandpassword.
Now, ifyou'renotalreadyfamiliarwithJasonWebbtokensandhowtheywork, I haveanentirevideotalkingaboutwhyyoushoulduseJasonWebbtokensandhowtheywork.
Soyoucancheckthatoutagain, linkedinthecardsandthedescriptionbelowforthispurposeofthevideo, we'rejustgonnaworryabouttheimplementationofJasonWebbtokens, andit's reallyactuallyquiteeasytocreate a JasonWebbtoken.
Wejustusethat J W T libraryandwepassitDOTsign, andthesignisfirstgoingtotakeourpayload, whichisessentiallywhatwewanttoserialize, andwewanttoserialize a userobject.
Solet's justcreate a user, andthisuserisgoingtobeequaltohearjustanobjectwhichhasname, andwewanttopassthatasourusername.
Forgetpostsowecancallthatauthenticatetokenfunctionasourmiddlewheresoweknowwehavethatinourpostandnowinsideofthisfunction, weneedtogetthetoken, andthistokenisgoingtocomefromtheheaderandwe'regonnahave a headercalledBear.
Soifourtokenisno, wejustwanttoreturntotheuserandaircode, sayingthattheydon't haveaccess, sowecansaysendstatusandwewanttosend a 41 statuscodesothatweknowthattheyhavenotsent a tokentowus.
Nowifwegettotheportionafterthistokencheck, weknowthatwehave a validtoken.
Sowecanjustsayprocessdot n v dotaccesstokensecretandthisisgoingtotake a callback, whichhasanerror, andit's goingtohavethevalueweserialized, whichinourcase, isthisuserobjectsoit's gonnahaveouruseraswolvesandair.
Soweknowwehave a userandwecouldjustcallnextjustlikethatsothatwecanactuallymoveonfromourmiddleWherenow, ifwesavethat, makesurethatthisisnextinsteadofnexttax.
Accesswouldbelikegivingupyour a p i kewhenyou'reaccessing a P I.
Theuserjustnowhasaccessforever.
Theideaof a refreshtokenisthatyousavetherefreshtokenin a safespot, andthenyournormalaccesstokensherehave a veryshortexpirationdate, sothatifsomeonegetsaccesstoyouraccesstoken, theyonlyhaveaccesstoyouraccountformaybe a fewminutesbeforetheaxisisrevoked.
Andthentheusermustusetherefreshtokentoget a newtoken.
Andnow, ifwegoovertoourrequestandwemake a postonlocalhost, thisisgoingtobe 4000 toelogin.
Ifwesendthatrequest, youcanseewenowgettingaccesstokenand a refreshtokenback.
Andlet's makesurewechangethisbackto 3000 forpost.
Andifwetrytomake a request, thisismostlikelygonnafail, since I don't think I got a quickenough.
Okay, I did.
Soithasn't expiredyet, butifwekeepsending a request, youcanseeafter 15 seconds, we'renowforbiddenfromaccessingthisroute, andweneedtouse a refreshtokentocreate a newone.
Sonowlet's goovertoouroffserverandweneedtocreate a newfunction.
There's gonnabeaptOutpostandwewanttoposttoken.
Soessentially, thisisgoingtobeforcreating a newtoken.
It's gonnabe a requestand a response.
Andinsidehere, we'regonnatakein a refreshtokensowecanget a refreshtoken.
It's gonnabeequaltorequeststopbodydottoken.
Andthisrequestisgoingtolookjustlikethis.
Lookslikejustcreate a reallysamplerequestwe'regonnaposttohttpslashslashlocalhost 4000.
Wewant a posttotoken, andwe'regoingto, ofcourse, makesureourcontenttypeisgoingtobesettoJasonandinsideofourJason.
We'regonnapass a token, andthattokenisgoingtobeourrefreshtoken.
Rightnowwedon't haveone.
Butifweweretomake a requesthere, forexample, here's a refreshtokenwecanuse, andwecouldpacethatinthere, andthiswouldpassupthatrefreshtokentoouroffserverhereandweneedtousethatrefreshtokenandchecktoseeifwealreadyhave a refreshtokenthatexistforthat.
Soourauthentication, whereweloginandcreaterefreshtokensandhandlerefreshingourtokensallhappenson a differentserverthatareactualAP, I whichisgreat.
Nowthenextthing I wanttotalkaboutishowtoactually d authenticaterefreshtokensbecauserightnow, foreverandeverandever, user, justclickthissendrequestbutton, createinfiniteaccesstokensforuser's nomatterwhat, andaslongastheyhavethatrefreshtoken, theycandothat.
So, inordertopreventthis, weneedtohavesomeformofdeletefunction.
We'rejustgonnacallthis a deletehereandwewanttodo, Forexample, logoutwhatthisisgoingtoallowustodoisactuallydeletethoserefreshtokens.
Andthisisreallyeasy.
Normallyyouhavedidn't leavethemfromsomedatabase.
Butsincewejustarestoringthemin a variableherecalledrefreshtokens, wecouldjustsaywewanttosetourrefreshtokensequalto a filteredversionofourrefreshtokens.
Whereallwe'redoingiswe'rejustcheckingme.
Justcloseoutofthishere.
Whereischeckingtomakesurethetokenthatisinside a refreshedtokensisnotequaltoourrequestThatbodydottokenthatwepasseduptoitandthenallwewanttodoisjustsend a statuswe'regonnasendtoOhforsayingthatwesuccessfullydeletedthistoken.
Let's saythatandactuallymake a requestforthatsowecanjustsaydeleteandwewanttogotolocalhopes.