Log in from a computer to take this course

You'll need to log in from a computer to start User Authentication & Authorization in Express. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store
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 equal an object in which 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 equal to an object with the username attribute. We set the username attribute equal 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:

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 module.exports.

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 in which the id attribute is equal to client.clientId.

3.

Set token.user equal to an object in which the username attribute is equal to user.username.

4.

Add token to the tokens array in db.

Then, return the token from the function.

5.

Export the saveToken() from model.js using the module.exports object.

Export saveToken() as saveToken.

Take this course for free

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

Or sign up using:

Already have an account?