Wednesday, 2 January 2019

Passing and updating data along node-express app

How can I pass and update data along node-express app without using DB.

So I am using passport to authenticate (consider this to be in src/google-passport.js),

passport.use(new GoogleStrategy({
    clientID: process.env.GOOGLE_CLIENT_ID,
    clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    callbackURL:  process.env.GOOGLE_CALLBACK_URL,
    userProfileURL: 'https://www.googleapis.com/oauth2/v3/userinfo',
    accessType: 'offline'
  }, (accessToken, refreshToken, params, profile, cb) => { 
        let profileSort = extractProfile(profile)
         mongooeHelperFunction.findUserByEmail(profileSort.email).then(response => {
           if (!response) {
            mongooeHelperFunction.createNewUser(profileSort)
            .then(res => {
               let newRes = {...res._doc}
                newRes["accessToken"] = accessToken
                cb(null, newRes)
            })
            .catch(error => {  throw error  })
           } else {
                let newRes = {...response._doc}
                newRes["accessToken"] = accessToken
                cb(null, newRes)
           }
        })
        .catch(error => {  throw error  })
    }
))

From Passport, I am getting an access token and refresh token. Usually Google access token is valid for an hour.

So I want to store the time when I receive my access token and If my access token is expired, I want to use a refresh token to get a new access token and then update the time after new access token is generated.

consider an api route

app.get("/something", isTokenValid, (req, res) => {

where isTokenValid is a middleware function and inside that function I could have when my passport token was created and then i could compare it with current time.

Aditionally if the token have expired, I have a function which would send the refresh token to get new access token and update the previous data/time of access token to the new date/time

Question: how can I pass and update data along node-express app



from Passing and updating data along node-express app

No comments:

Post a Comment