title: How to schedule the update of access token (using refresh token), in node? (Please check answer.)
body: <p><strong>Flow:</strong></p>
  <ul>
  <li>User attempts to login via Spotify (OAuth), and grants access to our web app (nodejs).</li>
  <li><code>accessToken</code>, <code>refreshToken</code>, <code>expires_in</code> and <code>profile</code> data is returned in exchange for the Authorization Code. <em>(using Passport.js)</em></li>
  <li><code>accessToken</code>, <code>refreshToken</code> and some other data is stored on mongoDb Atlas (cloud), forming a Document. <em>(using mongoose)</em></li>
  <li>The user logs out.</li>
  <li>The same user logs back in.</li>
  <li><code>accessToken</code>, <code>refreshToken</code>, <code>expires_in</code> and <code>profile</code> data is returned in exchange for the Authorization Code.</li>
  <li>The Document in the cloud database, is not updated.</li>
  </ul>
  <p>We are using these access tokens to create API calls in our main application <code>app.js</code>.</p>
  <p><strong>Drawback:</strong></p>
  <p>If the user logs out, and logs back in <strong>after an hour</strong>. He can no longer make API calls. The access token expires in an hour.</p>
  <p><strong>Required update for the app:</strong></p>
  <ol>
  <li>The access token, in our database should get updated every time the user logs in. (No new 'Document' or 'instance' of the same user should be created in our database.) <em><strong>[solved, check answer]</strong></em></li>
  <li>The refresh token should update the value of access token, in our database, for first hour from login; then every hour afterwards for as long as the user stays logged in.</li>
  <li>(Optional) If the user logs out and logs back in within one hour from latest access token, point '1' shouldn't execute, and the timer for point '2' shouldn't re-start.</li>
  </ol>
  <hr />
  <p><strong>passport-setup.js:</strong> This file is 'required' in 'app.js'.</p>
  <pre class="lang-js prettyprint-override"><code>const passport = require(&quot;passport&quot;);
  const SpotifyStrategy = require(&quot;passport-spotify&quot;).Strategy;
  const keys = require(&quot;./keys&quot;);
  const User = require(&quot;../models/user-model&quot;);
  
  // Creating cookie
  passport.serializeUser((user, done) =&gt; {
    done(null, user.id);
  })
  
  // Acccessing the created cookie
  passport.deserializeUser((id, done) =&gt; {
    User.findById(id).then((user) =&gt; {
      done(null, user);
    })
  })
  
  passport.use(
    new SpotifyStrategy({
      // Options for Spotify Strategy
      clientID: keys.spotify.clientID,
      clientSecret: keys.spotify.clientSecret,
      callbackURL: &quot;/auth/spotify/redirect&quot;
    }, (accessToken, refreshToken, expires_in, profile, done) =&gt; {
      // Passport callback function
      console.log(&quot;Reached Passport callback function, inside of 'passport-setup.js'.&quot;);
  
      // Check if user already exists in our database or if there is need to create a new one.
      User.findOne({
        spotifyId: profile.id
      }).then((existingUser) =&gt; {
        if (existingUser) {
          console.log(&quot;User already exists in database. Latest record:&quot;);
          console.log(existingUser);
          done(null, existingUser);
        } else {
          new User({
            displayName: profile.displayName,
            email: profile._json.email,
            spotifyId: profile.id,
            accessToken: accessToken,
            refreshToken: refreshToken
          }).save().then((newUser) =&gt; {
            console.log(&quot;New user created:&quot;);
            console.log(newUser);
            done(null, newUser);
          }).catch((err) =&gt; {
            console.log(&quot;Error occured: &quot; + err);
          })
        }
      })
    })
  )
  
  </code></pre>
  <hr />
  <p><em>I tried referencing the official documentation for mongoose, and watched some tutorials online. But all I got returned with, was errors. Hence, seeking help. I'm an absolute beginner, any help would be appreciated. :)</em></p>