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
Declare a function expression named saveToken()
that has three parameters: token
, client
, and user
using ES6 Arrow function expressions..
Set token.client
equal to an object in which the id
attribute is equal to client.clientId
.
Set token.user
equal to an object in which the username
attribute is equal to user.username
.
Add token
to the tokens
array in db
.
Then, return the token from the function.
Export the saveToken()
from model.js using the module.exports
object.
Export saveToken()
as saveToken
.