0

I am going with an online course, there's a part where the instructor says he is trying to find/ validate a user using User.findOne and passes some conditions to find the requested user. To pass the value, he uses object destructuring. Heres the specific code:

const token = req.header("Authorization").replace("Bearer ", "");
const decoded = jwt.verify(token, "secret key here!"); 

// issue is here, look at the second property of the findOne function's argument.
const user=await User.findOne({_id: decoded._id, 'tokens.token': token})

The instructor is using a string key in 'tokens.token'. He's saying that, mongodb will loop over the all the tokens available in specified user object to check if the given token matches.

And if you are wondering, here's an example of a single user which contains auth tokens:

  {
        "name": "Prottay",
        "_id": "5e27f23b6b549b4c28b8ac35",
        "password": "$2a$08$gUfMwk6TNWViHihrcxjKg.8EXD04lLkGIWXqzrf8wYokdLQXHxpdy",
        "tokens": [
            {
                "_id": "5e27f23b6b549b4c28b8ac36",
                "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZTI3ZjIzYjZiNTQ5YjRjMjhiOGFjMzUiLCJpYXQiOjE1Nzk2NzYyMTl9.-PWXzlEoPlEZn9F_awtzqrXOtByxUCW9RCdchHF1yKE"
            },
            {
                "_id": "5e280429596e742dcc2f9e30",
                "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZTI3ZjIzYjZiNTQ5YjRjMjhiOGFjMzUiLCJpYXQiOjE1Nzk2ODA4MDl9.7W-QZ55Cc3NFd_-NPyJ0VW_5F1UVrDWAV4xHX63D6tc"
            },
            {
                "_id": "5e280435596e742dcc2f9e31",
                "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZTI3ZjIzYjZiNTQ5YjRjMjhiOGFjMzUiLCJpYXQiOjE1Nzk2ODA4MjF9.vppisFiNNC_DYHtGK0IURzEOCCC5zcWl1v9yD6l1D4I"
            }
        ],
        "__v": 3
    },

To me it looks like by using 'tokens.token': token instructor is trying to loop over on the user's tokens array to match the correct token.

Am I right? If I am how can he be using loop in object destructuring?

1 Answers1

0

The instructor is not looping over an array using object destructuring but using the mongodb syntax for searching for a document within an array where you do not know the index before hand.

If you do not know the index position of the document nested in the array, concatenate the name of the array field, with a dot (.) and the name of the field in the nested document.

https://docs.mongodb.com/manual/tutorial/query-array-of-documents/#specify-a-query-condition-on-a-field-embedded-in-an-array-of-documents

wlh
  • 3,426
  • 1
  • 16
  • 32
  • Thanks, one more thing. Your website has dark mode and it stays darkmode even after refreshing. How do you do that? Using localstorage to save the state? –  Jan 22 '20 at 16:59
  • Yep, using `localStorage` is one way of handling it, that's what I currently use. You can also add media queries for `prefers-color-scheme`. There a loads of articles on this topic when you google it. – wlh Jan 22 '20 at 18:21
  • I tried using localstorage, but somehow it isn't working, can you show me an example of code on how you set up your context for dark mode? –  Jan 23 '20 at 05:56
  • @ProttayRudra I just sent you an email – wlh Jan 24 '20 at 15:31