Learn

Inside app.js, where we have included the oauth2-server package, we’ll create an instance of the OAuth2Server object and store it in a variable named oauth.

const oauth = new OAuth2Server();

The OAuth2Server object requires a model object which contains functions to access, store, and validate our access tokens. We’ll be writing them separately in a file named model.js.

Inside the constructor of OAuth2Server, pass an object with an attribute named model, and we’ll import model.js using the require() function as the value.

const oauth = new OAuth2Server({ model: require('./model.js') });

OAuth2Server can be supplied with additional options in the constructor. To pass tokens inside the URL, we’ll set the allowBearerTokensInQueryString attribute to true:

const oauth = new OAuth2Server({ model: require('./model.js'), allowBearerTokensInQueryString: true })

The access token lifetime can also be configured as an option using the accessTokenLifetime attribute. The lifetime is set in seconds, and we can set the access token lifetime to one hour like this:

const oauth = new OAuth2Server({ model: require('./model.js'), allowBearerTokensInQueryString: true, accessTokenLifetime: 60 * 60 })

Instructions

1.

Create a new OAuth2Server instance and store it in a variable named oauth.

2.

Inside the OAuth2Server constructor, include the model in model.js using the require() function.

3.

Set allowBearerTokensInQueryString to true inside the constructor options.

4.

Set the lifetime of the access token to one hour.

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?