Learn

The saveToken() function must be implemented for all grant types in the model used by OAuth2Server. This function stores the access token as an object to a database when an access token is obtained.

The saveToken() function is implemented with three arguments: token, client, and user. We set the token.client to an object where the id attribute is equal to the passed client’s clientId. The client is formatted like below:

const saveToken = (token, client, user) => { token.client = { id: client.clientId } }

The token.user is set to an object with the username attribute. We set the username to the username of the passed user object. The username is formatted like below:

token.user = { username: user.username }

With the token formatted, we can save the token to our database by pushing the token to our db.tokens array and returning the token.

db.tokens.push(token); return token

Our final saveToken() function looks like below:

const saveToken = (token, client, user) => { token.client = { id: client.clientId } token.user = { username: user.username } db.tokens.push(token); return token; }

We’ll also export the saveToken() function from models.js using the modules.export.

Instructions

1.

Declare a function expression named saveToken() that has three parameters: token, client, and user using ES6 Arrow function expressions..

2.

Set token.client equal to an object where the id attribute is set to client.clientId.

3.

Set token.user equal to an object where the username attribute is set to user.username.

4.

Add token to the tokens array in db and return the token from the function.

5.

We’ll have to export the saveTokens() from model.js using the module.exports object. Export saveTokens() as saveTokens.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?