How to use validToken method in Best

Best JavaScript code snippet using best

main.test.js

Source:main.test.js Github

copy

Full Screen

...87 });88 return token;89};90const singleListingId = async () => {91 const { listings } = await getTry('/listings', 200, {}, await validToken(USER1));92 return listings[0].id;93};94const singleBookingId = async () => {95 const { bookings } = await getTry('/bookings', 200, {}, await validToken(USER1));96 return bookings[0].id;97};98describe('Test the root path', () => {99 beforeAll(() => {100 reset();101 });102 beforeAll(() => {103 server.close();104 });105 /***************************************************************106 Auth Tests107 ***************************************************************/108 describe('Sign Up and Log In', () => {109 test('Registration of initial user', async () => {110 const body = await postTry('/user/auth/register', 200, {111 email: USER1.email,112 password: USER1.password,113 name: USER1.name,114 });115 expect(body.token instanceof String);116 });117 test('Registration of secondary user', async () => {118 const body = await postTry('/user/auth/register', 200, {119 email: USER2.email,120 password: USER2.password,121 name: USER2.name,122 });123 expect(body.token instanceof String);124 });125 test('Inability to re-register a user', async () => {126 await postTry('/user/auth/register', 400, {127 email: USER1.email,128 password: USER1.password,129 name: USER1.name,130 });131 });132 test('Login to an existing user', async () => {133 const body = await postTry('/user/auth/login', 200, {134 email: USER1.email,135 password: USER1.password,136 });137 expect(body.token instanceof String);138 });139 test('Login attempt with invalid credentials 1', async () => {140 await postTry('/user/auth/login', 400, {141 email: 'hayden.smith@unsw.edu.a',142 password: 'bananapie',143 });144 });145 test('Login attempt with invalid credentials 2', async () => {146 await postTry('/user/auth/login', 400, {147 email: 'hayden.smith@unsw.edu.au',148 password: 'bananapi',149 });150 });151 test('Logout a valid session', async () => {152 const bodyLogout = await postTry('/user/auth/logout', 200, {}, await validToken(USER1));153 expect(bodyLogout).toMatchObject({});154 });155 test('Logout a session without auth token', async () => {156 const body = await postTry('/user/auth/logout', 403, {});157 expect(body).toMatchObject({});158 });159 });160 /***************************************************************161 Listing Tests162 ***************************************************************/163 describe('Creating a single listing', () => {164 test('Initially there are no listings', async () => {165 const body = await getTry('/listings', 200, {}, await validToken(USER1));166 expect(body.listings).toHaveLength(0);167 });168 test('Creating a single listing', async () => {169 await postTry(170 '/listings/new',171 200,172 {173 title: LISTING1.title,174 address: LISTING1.address,175 price: LISTING1.price,176 thumbnail: LISTING1.thumbnail,177 metadata: LISTING1.metadata,178 },179 await validToken(USER1),180 );181 });182 test('Creating a single listing, duplicate title', async () => {183 await postTry(184 '/listings/new',185 400,186 {187 title: LISTING1.title,188 address: LISTING1.address,189 price: LISTING1.price,190 thumbnail: LISTING1.thumbnail,191 metadata: LISTING1.metadata,192 },193 await validToken(USER1),194 );195 });196 test('That there is now one listing', async () => {197 const body = await getTry('/listings', 200, {}, await validToken(USER1));198 const listingid = body.listings[0].id;199 expect(body.listings).toHaveLength(1);200 expect(typeof body.listings[0].id).toBe('number');201 expect(body.listings[0].title).toBe(LISTING1.title);202 expect(body.listings[0].owner).toBe(USER1.email);203 expect(body.listings[0].address).toMatchObject(LISTING1.address);204 expect(body.listings[0].thumbnail).toBe(THUMBNAIL);205 expect(body.listings[0].price).toBe(LISTING1.price);206 expect(body.listings[0].reviews).toMatchObject([]);207 const details = await getTry(`/listings/${listingid}`, 200, {}, await validToken(USER1));208 expect(details.listing.metadata).toMatchObject(LISTING1.metadata);209 expect(details.listing.availability).toMatchObject([]);210 expect(details.listing.published).toBe(false);211 });212 });213 /***************************************************************214 Failing to Create Listings215 ***************************************************************/216 describe('Creating a single listing - FAIL CASES', () => {217 test('Creating a single listing, value missing', async () => {218 await postTry('/listings/new', 400, {}, await validToken(USER1));219 });220 test('Creating a single listing, token missing', async () => {221 await postTry('/listings/new', 403, {222 title: 'LISTING',223 address: 'ADDRESS',224 price: 200,225 thumbnail: THUMBNAIL,226 metadata: { something: 'here' },227 });228 });229 test('Creating a single listing, no title', async () => {230 await postTry(231 '/listings/new',232 400,233 {234 address: 'ADDRESS',235 price: 200,236 thumbnail: THUMBNAIL,237 metadata: { something: 'here' },238 },239 await validToken(USER1),240 );241 });242 test('Creating a single listing, no address', async () => {243 await postTry(244 '/listings/new',245 400,246 {247 title: 'LISTING',248 price: 200,249 thumbnail: THUMBNAIL,250 metadata: { something: 'here' },251 },252 await validToken(USER1),253 );254 });255 test('Creating a single listing, no price', async () => {256 await postTry(257 '/listings/new',258 400,259 {260 title: 'LISTING',261 address: 'ADDRESS',262 thumbnail: THUMBNAIL,263 metadata: { something: 'here' },264 },265 await validToken(USER1),266 );267 });268 test('Creating a single listing, non-numerical price', async () => {269 await postTry(270 '/listings/new',271 400,272 {273 title: 'LISTING',274 address: 'ADDRESS',275 price: 'PRICE',276 thumbnail: THUMBNAIL,277 metadata: { something: 'here' },278 },279 await validToken(USER1),280 );281 });282 test('Creating a single listing, no thumbnail', async () => {283 await postTry(284 '/listings/new',285 400,286 {287 title: 'LISTING',288 address: 'ADDRESS',289 price: '200',290 metadata: { something: 'here' },291 },292 await validToken(USER1),293 );294 });295 test('Creating a single listing, no property metadata', async () => {296 await postTry(297 '/listings/new',298 400,299 {300 title: 'LISTING',301 address: 'ADDRESS',302 price: '200',303 thumbnail: THUMBNAIL,304 },305 await validToken(USER1),306 );307 });308 });309 /***************************************************************310 Creating a Second Listing311 ***************************************************************/312 describe('Creating a secondary listing', () => {313 test('Create a second listing', async () => {314 await postTry(315 '/listings/new',316 200,317 {318 title: LISTING2.title,319 address: LISTING2.address,320 price: LISTING2.price,321 thumbnail: LISTING2.thumbnail,322 metadata: LISTING2.metadata,323 },324 await validToken(USER1),325 );326 });327 test('That there is now two listings', async () => {328 const body = await getTry('/listings', 200, {}, await validToken(USER1));329 expect(body.listings).toHaveLength(2);330 });331 });332 /***************************************************************333 Deleting Listing334 ***************************************************************/335 describe('Deleting a listing', () => {336 test('Try and delete a listing with invalid token', async () => {337 const { listings } = await getTry('/listings', 200, {}, await validToken(USER1));338 const listingid = listings[1].id;339 await deleteTry(`/listings/${listingid}`, 403, {});340 });341 test('Try and delete a listing with invalid listingid', async () => {342 await deleteTry(`/listings/${99999999999999}`, 400, {}, await validToken(USER1));343 });344 test('Try and delete a listing', async () => {345 const { listings } = await getTry('/listings', 200, {}, await validToken(USER1));346 const listingid = listings[1].id;347 await deleteTry(`/listings/${listingid}`, 200, {}, await validToken(USER1));348 });349 test('That there is now one listing again', async () => {350 const body = await getTry('/listings', 200, {}, await validToken(USER1));351 expect(body.listings).toHaveLength(1);352 });353 });354 /***************************************************************355 Updating Listing356 ***************************************************************/357 describe('Updating a listing address', () => {358 test('Update listing address', async () => {359 const listingid = await singleListingId();360 await putTry(361 `/listings/${listingid}`,362 200,363 {364 address: LISTING3.address,365 },366 await validToken(USER1),367 );368 });369 test('Check that address updated', async () => {370 const listingid = await singleListingId();371 const { listing } = await getTry(`/listings/${listingid}`, 200, {}, await validToken(USER1));372 expect(listing.address).toMatchObject(LISTING3.address);373 });374 });375 describe('Updating a listing title and price', () => {376 test('Update listing title and price', async () => {377 const listingid = await singleListingId();378 await putTry(379 `/listings/${listingid}`,380 200,381 {382 title: 'House3',383 price: 250,384 },385 await validToken(USER1),386 );387 });388 test('Check that title and price updated', async () => {389 const listingid = await singleListingId();390 const { listing } = await getTry(`/listings/${listingid}`, 200, {}, await validToken(USER1));391 expect(listing.title).toBe(LISTING3.title);392 expect(listing.price).toBe(LISTING3.price);393 });394 });395 describe('Updating a listing thumbnail and metadata', () => {396 test('Update listing thumbnail and metadata', async () => {397 const listingid = await singleListingId();398 await putTry(399 `/listings/${listingid}`,400 200,401 {402 thumbnail: LISTING3.thumbnail,403 metadata: LISTING3.metadata,404 },405 await validToken(USER1),406 );407 });408 test('Check that thumbnail and metadata updated', async () => {409 const listingid = await singleListingId();410 const { listing } = await getTry(`/listings/${listingid}`, 200, {}, await validToken(USER1));411 expect(listing.metadata).toMatchObject(LISTING3.metadata);412 expect(listing.thumbnail).toBe(LISTING3.thumbnail);413 });414 });415 /***************************************************************416 Publish Listing417 ***************************************************************/418 describe('Publishing a listing', () => {419 test('Try publishing a listing without availability', async () => {420 const listingid = await singleListingId();421 await putTry(`/listings/publish/${listingid}`, 400, {}, await validToken(USER1));422 });423 test('Publish a listing', async () => {424 const listingid = await singleListingId();425 await putTry(426 `/listings/publish/${listingid}`,427 200,428 {429 availability: [430 { from: 'date1', to: 'date2' },431 { from: 'date3', to: 'date4' },432 ],433 },434 await validToken(USER1),435 );436 });437 test('Check that a listing has been published', async () => {438 const listingid = await singleListingId();439 const { listing } = await getTry(`/listings/${listingid}`, 200, {}, await validToken(USER1));440 expect(listing.published).toBe(true);441 });442 });443 /***************************************************************444 Unpublish Listing445 ***************************************************************/446 describe('Unpublish a listing', () => {447 test('Try unpublishing with a fake listingid', async () => {448 const listingid = '12345';449 await putTry(`/listings/unpublish/${listingid}`, 400, {}, await validToken(USER1));450 });451 test('Unpublish a listing', async () => {452 const listingid = await singleListingId();453 await putTry(`/listings/unpublish/${listingid}`, 200, {}, await validToken(USER1));454 });455 test('Check that a listing has been unpublished', async () => {456 const listingid = await singleListingId();457 const { listing } = await getTry(`/listings/${listingid}`, 200, {}, await validToken(USER1));458 expect(listing.published).toBe(false);459 });460 });461 /***************************************************************462 Creating Bookings463 ***************************************************************/464 describe('Creating a single booking', () => {465 beforeAll(async () => {466 // We want to make sure the listing is published467 const listingid = await singleListingId();468 await putTry(469 `/listings/publish/${listingid}`,470 200,471 {472 availability: [473 { from: 'date1', to: 'date2' },474 { from: 'date3', to: 'date4' },475 ],476 },477 await validToken(USER1),478 );479 });480 test('Initially there are no bookings', async () => {481 const body = await getTry('/bookings', 200, {}, await validToken(USER1));482 expect(body.bookings).toHaveLength(0);483 });484 test('Creating a single booking', async () => {485 const listingid = await singleListingId();486 await postTry(487 `/bookings/new/${listingid}`,488 200,489 { dateRange: { from: 'date1', to: 'date2' }, totalPrice: 500 },490 await validToken(USER1),491 );492 });493 test('Check there is now one booking', async () => {494 const body = await getTry('/bookings', 200, {}, await validToken(USER1));495 expect(body.bookings).toHaveLength(1);496 expect(typeof body.bookings[0].id).toBe('number');497 expect(body.bookings[0].totalPrice).toBe(500);498 expect(body.bookings[0].dateRange).toMatchObject({ from: 'date1', to: 'date2' });499 });500 });501 /***************************************************************502 Failing to Create Bookings503 ***************************************************************/504 describe('Create a booking - FAIL CASES', () => {505 test('Creating a single booking, both values missing', async () => {506 const listingid = await singleListingId();507 await postTry(`/bookings/new/${listingid}`, 400, {}, await validToken(USER1));508 });509 test('Creating a single booking, dateRange is missing', async () => {510 const listingid = await singleListingId();511 await postTry(`/bookings/new/${listingid}`, 400, { totalPrice: 500 }, await validToken(USER1));512 });513 test('Creating a single booking, totalPrice is missing', async () => {514 const listingid = await singleListingId();515 await postTry(516 `/bookings/new/${listingid}`,517 400,518 { dateRange: { from: 'date1', to: 'date2' } },519 await validToken(USER1),520 );521 });522 test('Creating a single booking, totalPrice is a char string', async () => {523 const listingid = await singleListingId();524 await postTry(525 `/bookings/new/${listingid}`,526 400,527 {528 dateRange: { from: 'date1', to: 'date2' },529 totalPrice: 'haha',530 },531 await validToken(USER1),532 );533 });534 test('Creating a single booking, totalPrice is less than 0', async () => {535 const listingid = await singleListingId();536 await postTry(537 `/bookings/new/${listingid}`,538 400,539 {540 dateRange: { from: 'date1', to: 'date2' },541 totalPrice: -1,542 },543 await validToken(USER1),544 );545 });546 test('Creating a single booking, listingid is wrong', async () => {547 const listingid = '123456';548 await postTry(549 `/bookings/new/${listingid}`,550 400,551 { dateRange: { from: 'date1', to: 'date2' }, totalPrice: 500 },552 await validToken(USER1),553 );554 });555 test('Creating a single booking, token missing', async () => {556 const listingid = await singleListingId();557 await postTry(`/bookings/new/${listingid}`, 403, {558 dateRange: { from: 'date1', to: 'date2' },559 totalPrice: 500,560 });561 });562 test('Creating a single booking, listing is unpublished', async () => {563 const body = await postTry(564 '/listings/new',565 200,566 {567 title: 'Unpublished',568 address: LISTING1.address,569 price: LISTING1.price,570 thumbnail: LISTING1.thumbnail,571 metadata: LISTING1.metadata,572 },573 await validToken(USER1),574 );575 const listingid = body.listingId;576 await postTry(577 `/bookings/new/${listingid}`,578 400,579 {580 dateRange: { from: 'date1', to: 'date2' },581 totalPrice: 500,582 },583 await validToken(USER1),584 );585 await deleteTry(`/listings/${listingid}`, 200, {}, await validToken(USER1));586 });587 });588 /***************************************************************589 Creating a Second Booking590 ***************************************************************/591 describe('Creating a secondary booking', () => {592 test('Creating a second booking', async () => {593 const listingid = await singleListingId();594 await postTry(595 `/bookings/new/${listingid}`,596 200,597 { dateRange: { from: 'date3', to: 'date4' }, totalPrice: 700 },598 await validToken(USER1),599 );600 });601 test('Check there is now two bookings', async () => {602 const body = await getTry('/bookings', 200, {}, await validToken(USER1));603 expect(body.bookings).toHaveLength(2);604 });605 });606 /***************************************************************607 Deleting Bookings608 ***************************************************************/609 describe('Deleting a booking', () => {610 test('Try and delete a booking with invalid token', async () => {611 const bookingid = await singleBookingId();612 await deleteTry(`/bookings/${bookingid}`, 403, {});613 });614 test('Try and delete a booking with invalid bookingid', async () => {615 await deleteTry(`/bookings/${99999999999999}`, 400, {}, await validToken(USER1));616 });617 test('Try and delete a booking', async () => {618 const { bookings } = await getTry('/bookings', 200, {}, await validToken(USER1));619 const bookingid = bookings[1].id;620 await deleteTry(`/bookings/${bookingid}`, 200, {}, await validToken(USER1));621 });622 test('That there is now one listing again', async () => {623 const body = await getTry('/bookings', 200, {}, await validToken(USER1));624 expect(body.bookings).toHaveLength(1);625 });626 });627 /***************************************************************628 Failing to Accept Bookings629 ***************************************************************/630 describe('Accept a booking - FAIL CASES', () => {631 test('Accepting a booking without a token', async () => {632 const bookingid = await singleBookingId();633 await putTry(`/bookings/accept/${bookingid}`, 403, {});634 });635 test("Accepting a booking by USER1, where listing doesn't belong to USER1", async () => {636 // Creating a listing under USER2637 const body1 = await postTry(638 '/listings/new',639 200,640 {641 title: LISTING1.title,642 address: LISTING1.address,643 price: LISTING1.price,644 thumbnail: LISTING1.thumbnail,645 metadata: LISTING1.metadata,646 },647 await validToken(USER2),648 );649 const listingid = body1.listingId;650 // Publish the listing651 await putTry(652 `/listings/publish/${listingid}`,653 200,654 {655 availability: [656 { from: 'date1', to: 'date2' },657 { from: 'date3', to: 'date4' },658 ],659 },660 await validToken(USER2),661 );662 // Creating a booking from USER1 under USER2 listing663 const body2 = await postTry(664 `/bookings/new/${listingid}`,665 200,666 { dateRange: { from: 'date5', to: 'date6' }, totalPrice: 600 },667 await validToken(USER1),668 );669 const bookingid = body2.bookingId;670 await putTry(`/bookings/accept/${bookingid}`, 400, {}, await validToken(USER1));671 // Clean up672 await deleteTry(`/listings/${listingid}`, 200, {}, await validToken(USER2));673 await deleteTry(`/bookings/${bookingid}`, 200, {}, await validToken(USER1));674 });675 test("Accepting a booking by USER2, where listing doesn't belong to USER2", async () => {676 const bookingid = await singleBookingId();677 await putTry(`/bookings/accept/${bookingid}`, 400, {}, await validToken(USER2));678 });679 test('Accepting a booking with a fake bookingId', async () => {680 const bookingid = '12345';681 await putTry(`/bookings/accept/${bookingid}`, 400, {}, await validToken(USER1));682 });683 });684 /***************************************************************685 Accepting Bookings686 ***************************************************************/687 describe('Accept a booking', () => {688 test('Accepting a booking by USER1 under USER1 listing', async () => {689 const bookingid = await singleBookingId();690 await putTry(`/bookings/accept/${bookingid}`, 200, {}, await validToken(USER1));691 });692 test("Accepting a booking by USER1 that's been accepted", async () => {693 const bookingid = await singleBookingId();694 await putTry(`/bookings/accept/${bookingid}`, 400, {}, await validToken(USER1));695 });696 });697 /***************************************************************698 Failing to Decline Bookings699 ***************************************************************/700 describe('Decline a booking - FAIL CASES', () => {701 test('Declining a booking without a token', async () => {702 const bookingid = await singleBookingId();703 await putTry(`/bookings/decline/${bookingid}`, 403, {});704 });705 test('Declining a booking by USER1, where listing belongs to USER2', async () => {706 // Creating a listing under USER2707 const body1 = await postTry(708 '/listings/new',709 200,710 {711 title: LISTING1.title,712 address: LISTING1.address,713 price: LISTING1.price,714 thumbnail: LISTING1.thumbnail,715 metadata: LISTING1.metadata,716 },717 await validToken(USER2),718 );719 const listingid = body1.listingId;720 // Publish the listing721 await putTry(722 `/listings/publish/${listingid}`,723 200,724 {725 availability: [726 { from: 'date1', to: 'date2' },727 { from: 'date3', to: 'date4' },728 ],729 },730 await validToken(USER2),731 );732 // Creating a booking from USER1 under USER2 listing733 const body2 = await postTry(734 `/bookings/new/${listingid}`,735 200,736 { dateRange: { from: 'date5', to: 'date6' }, totalPrice: 600 },737 await validToken(USER1),738 );739 const bookingid = body2.bookingId;740 // Accept the booking by USER2741 await putTry(`/bookings/accept/${bookingid}`, 200, {}, await validToken(USER2));742 // Now try to decline by USER1743 await putTry(`/bookings/accept/${bookingid}`, 400, {}, await validToken(USER1));744 // Clean up745 await deleteTry(`/listings/${listingid}`, 200, {}, await validToken(USER2));746 await deleteTry(`/bookings/${bookingid}`, 200, {}, await validToken(USER1));747 });748 });749 test('Declining a booking by USER2, where listing belongs to USER1', async () => {750 const bookingid = await singleBookingId();751 await putTry(`/bookings/decline/${bookingid}`, 400, {}, await validToken(USER2));752 });753 test('Declining a booking with a fake bookingId', async () => {754 const bookingid = '12345';755 await putTry(`/bookings/decline/${bookingid}`, 400, {}, await validToken(USER1));756 });757 /***************************************************************758 Listing Reviews759 ***************************************************************/760 describe('Publishing a listing review - FAIL CASES', () => {761 test('Try publishing a listing review with no token', async () => {762 const listingid = await singleListingId();763 const bookingid = await singleBookingId();764 const review = { text: 'Great Place!', rating: 5 };765 await putTry(`/listings/${listingid}/review/${bookingid}`, 403, review);766 });767 test('Try publishing a listing review with a fake bookingid', async () => {768 const listingid = await singleListingId();769 const bookingid = '123456';770 const review = { text: 'Great Place!', rating: 5 };771 await putTry(`/listings/${listingid}/review/${bookingid}`, 400, review, await validToken(USER1));772 });773 test('Try publishing a listing review with no contents', async () => {774 const listingid = await singleListingId();775 const bookingid = await singleBookingId();776 await putTry(`/listings/${listingid}/review/${bookingid}`, 400, {}, await validToken(USER1));777 });778 test('Try publishing a listing review with a fake listingid', async () => {779 const listingid = '123456';780 const bookingid = await singleBookingId();781 const review = { text: 'Great Place!', rating: 5 };782 await putTry(`/listings/${listingid}/review/${bookingid}`, 400, review, await validToken(USER1));783 });784 test('Try publishing a listing review for a listing the user has not stayed at', async () => {785 // Creating a listing under USER2786 const body1 = await postTry(787 '/listings/new',788 200,789 {790 title: LISTING1.title,791 address: LISTING1.address,792 price: LISTING1.price,793 thumbnail: LISTING1.thumbnail,794 metadata: LISTING1.metadata,795 },796 await validToken(USER2),797 );798 const listingid = body1.listingId;799 const bookingid = await singleBookingId();800 const review = { text: 'Great Place!', rating: 5 };801 await putTry(`/listings/${listingid}/review/${bookingid}`, 400, review, await validToken(USER1));802 // Clean up803 await deleteTry(`/listings/${listingid}`, 200, {}, await validToken(USER2));804 });805 });806 describe('Publishing a single listing review', () => {807 test('Publishing a single review', async () => {808 const listingid = await singleListingId();809 const bookingid = await singleBookingId();810 const review = { text: 'Great Place!', rating: 5 };811 await putTry(`/listings/${listingid}/review/${bookingid}`, 200, { review }, await validToken(USER1));812 });813 test('Checking there is now a single review for this listing', async () => {814 const listingid = await singleListingId();815 const { listing } = await getTry(`/listings/${listingid}`, 200, {}, await validToken(USER1));816 expect(listing.reviews).toMatchObject([{ text: 'Great Place!', rating: 5 }]);817 });818 });819 describe('Publishing a secondary listing review', () => {820 test('Publishing a secondary review', async () => {821 const listingid = await singleListingId();822 const bookingid = await singleBookingId();823 const review = { text: 'Just ok, like my life', rating: 3 };824 await putTry(`/listings/${listingid}/review/${bookingid}`, 200, { review }, await validToken(USER1));825 });826 test('Checking there is now a single review for this listing', async () => {827 const listingid = await singleListingId();828 const { listing } = await getTry(`/listings/${listingid}`, 200, {}, await validToken(USER1));829 expect(listing.reviews).toMatchObject([830 { text: 'Great Place!', rating: 5 },831 { text: 'Just ok, like my life', rating: 3 },832 ]);833 });834 });...

Full Screen

Full Screen

middlewares.js

Source:middlewares.js Github

copy

Full Screen

1const { verify } = require("jsonwebtoken");2const validateToken = (req, res, next) => {3 const accessToken = req.header("accessToken");4 if (!accessToken) {5 console.log("User not logged in! from middleware");6 return res.json({ error: "User not logged in!" });7 }8 try {9 const validToken = verify(accessToken, "importantsecret");10 req.tempVariable = validToken;11 if (validToken) {12 console.log("validateToken passed in middleware");13 return next();14 }15 } catch (err) {16 console.log("error in middleware " + err);17 return res.json({ error: err });18 }19};20const validateUserLoggedIn = (req, res, next) => {21 const accessToken = req.header("accessToken");22 try {23 if (accessToken.includes("null")) {24 return next();25 }26 } catch (e) {27 return next();28 }29 try {30 const validToken = verify(accessToken, "importantsecret");31 req.tempVariable = validToken;32 if (validToken) {33 req.userId = validToken.id;34 return next();35 }36 } catch (err) {37 req.userId = null;38 return next();39 }40};41// const validateUserLoggedIn = (req, res, next) => {42// const accessToken = req.header("accessToken");43// req.UserState = "null";44// if (accessToken.includes("null")) {45// req.UserState = "notLoggedIn";46// return next();47// }48// try {49// const validToken = verify(accessToken, "importantsecret");50// req.tempVariable = validToken;51// if (validToken) {52// req.userId = validToken.id;53// req.UserState = "LoggedIn";54// return next();55// }56// } catch (err) {57// req.UserState = "notLoggedIn";58// req.userId = null;59// return next();60// }61// };...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Best automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful