src/index.js
/**
	 * Authenticates a request by delegating its provided access token to Discord to retrieve the user profile; if an
	 * only a refresh token could be parsed, this method will first attempt to exchange it for an access token
	 * @param {Object} req = HTTP request object
	 */
Authenticates a request by delegating its provided access token to Discord to retrieve the user profile; if an
only a refresh token could be parsed, this method will first attempt to exchange it for an access token
@param {Object} req = HTTP request object
Authenticates a request by delegating its provided access token to Discord to retrieve the user profile ; if an only a refresh token could be parsed this method will first attempt to exchange it for an access token @param { Object } req = HTTP request object
authenticate(req) {
		let accessToken = this.lookup(req, this._accessTokenField)
			|| (this._oAuth2HeaderFallback && this.constructor.parseOAuth2Header(req));
		let refreshToken = this.lookup(req, this._refreshTokenField);
		if (!accessToken && !refreshToken) {
			return this.fail({
				message: 'Neither access token nor refresh token could be parsed from the request',
			});
		}
		if (!accessToken && !this._refreshTokenFallback) {
			return this.fail({
				message: 'Access token could not be parsed from the request (refresh token exchange disabled)',
			});
		}
		const loadUserProfile = () => {
			this._loadUserProfile(accessToken, (error, profile) => {
				if (error) return this.error(error);
				const done = (err, user, info) => {
					if (err) return this.error(err);
					if (!user) return this.fail(info);
					return this.success(user, info);
				};
				const args = [accessToken, refreshToken, profile, done];
				if (this._passReqToCallback) args.unshift(req);
				this._verify(...args);
			});
		};
		if (!accessToken && refreshToken) {
			return this._oauth2.getOAuthAccessToken(
				refreshToken,
				{grant_type: 'refresh_token'},
				(error, _accessToken, _refreshToken) => {
					if (error) {
						return this.error(
							new InternalOAuthError('Failed to exchange refresh token for access token', error),
						);
					}
					accessToken = _accessToken;
					refreshToken = _refreshToken;
					loadUserProfile();
				},
			);
		}
		loadUserProfile();
	}
authenticate ( req ) { let accessToken = this . lookup ( req , this . _accessTokenField ) || ( this . _oAuth2HeaderFallback && this . constructor . parseOAuth2Header ( req ) ) ; let refreshToken = this . lookup ( req , this . _refreshTokenField ) ; if ( ! accessToken && ! refreshToken ) { return this . fail ( { message : 'Neither access token nor refresh token could be parsed from the request' , } ) ; } if ( ! accessToken && ! this . _refreshTokenFallback ) { return this . fail ( { message : 'Access token could not be parsed from the request (refresh token exchange disabled)' , } ) ; } const loadUserProfile = ( ) => { this . _loadUserProfile ( accessToken , ( error , profile ) => { if ( error ) return this . error ( error ) ; const done = ( err , user , info ) => { if ( err ) return this . error ( err ) ; if ( ! user ) return this . fail ( info ) ; return this . success ( user , info ) ; } ; const args = [ accessToken , refreshToken , profile , done ] ; if ( this . _passReqToCallback ) args . unshift ( req ) ; this . _verify ( ... args ) ; } ) ; } ; if ( ! accessToken && refreshToken ) { return this . _oauth2 . getOAuthAccessToken ( refreshToken , { grant_type : 'refresh_token' } , ( error , _accessToken , _refreshToken ) => { if ( error ) { return this . error ( new InternalOAuthError ( 'Failed to exchange refresh token for access token' , error ) , ) ; } accessToken = _accessToken ; refreshToken = _refreshToken ; loadUserProfile ( ) ; } , ) ; } loadUserProfile ( ) ; }
Authenticates a request by delegating its provided access token to Discord to retrieve the user profile; if an
 only a refresh token could be parsed, this method will first attempt to exchange it for an access token
 @param {Object} req = HTTP request object
Authenticates a request by delegating its provided access token to Discord to retrieve the user profile ; if an only a refresh token could be parsed this method will first attempt to exchange it for an access token @param { Object } req = HTTP request object
/* Called once an access token is obtained (either immediately or after refresh token exchange) */ /* Exchange the refresh token */