Passport strategy for authenticating with Microsoft one time authorization code using the OAuth 2.0 API.
npm install passport-microsoft-authcodepassport-microsoft-authcode Strategy along with passport
js
var passport = require('passport');
var MicrosoftAuthCodeStrategy = require('passport-microsoft-authcode').Strategy;
`
#### Configure Strategy
The Microsoft authentication strategy authenticates users using a Microsoft
account and OAuth 2.0 tokens. The strategy requires a verify callback, which
accepts these credentials and calls done providing a user, as well as
options specifying a app ID and app secret.
`js
passport.use(new MicrosoftAuthCodeStrategy({
clientID: MICROSOFT_CLIENT_ID,
clientSecret: MICROSOFT_CLIENT_SECRET
},
function(accessToken, refreshToken, profile, done) {
User.findOrCreate({ microsoftId: profile.id }, function (err, user) {
return done(err, user);
});
}
));
`
#### Authenticate Requests
Use passport.authenticate(), specifying the 'microsoft-authcode' strategy, to authenticate requests.
`js
app.post('/auth/microsoft/authcode',
passport.authenticate('microsoft-authcode'),
function (req, res) {
// do something with req.user
res.send(req.user? 200 : 401);
}
);
`
The post request to this route should include a JSON object with the key code` set to the one time authorization code you receive from microsoft.