Skip to content

Commit

Permalink
microsoft login
Browse files Browse the repository at this point in the history
  • Loading branch information
Saransh Balyan committed Jul 30, 2024
1 parent 35dcd53 commit 6651834
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 40 deletions.
8 changes: 8 additions & 0 deletions User/Routes/passport.unprotected.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ if (process.env.GOOGLE_ID && process.env.GOOGLE_SECRET) {
});
}

if (process.env.MICROSOFT_ID && process.env.MICROSOFT_SECRET) {
Router.get('/microsoft', passport.authenticate('microsoft'));
Router.get('/microsoft/callback', passport.authenticate('microsoft', { failureRedirect: '/auth/microsoft', session: false }), (req, res) => {
const { accessToken, refreshToken } = req.user;
return res.redirect(`${process.env.WEBSITE_HOME}/login?accessToken=${accessToken}&refreshToken=${refreshToken}`);
});
}

if (process.env.GITHUB_ID && process.env.GITHUB_SECRET) {
Router.get('/github', passport.authenticate('github'));
Router.get('/github/callback', passport.authenticate('github', { failureRedirect: '/auth/github', session: false }), (req, res) => {
Expand Down
31 changes: 29 additions & 2 deletions User/Service/passport.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import passport from 'passport';
import { Strategy as GitHubStrategy } from 'passport-github2';
import { OAuth2Strategy as GoogleStrategy } from 'passport-google-oauth';
import { Strategy as GoogleStrategy } from 'passport-google-oauth20';
import { Strategy as MicrosoftStrategy } from 'passport-microsoft';
import { BasicStrategy } from 'passport-http';
import { Strategy as LinkedInStrategy } from 'passport-linkedin-oauth2';

Expand Down Expand Up @@ -34,7 +35,7 @@ if (process.env.GOOGLE_ID && process.env.GOOGLE_SECRET) {
clientSecret: process.env.GOOGLE_SECRET,
passReqToCallback: true
},
async (req, _accessToken, _refreshToken, _params, profile, done) => {
async (req, _accessToken, _refreshToken, profile, done) => {
try {
const email = profile.emails[0]?.value;
// eslint-disable-next-line sonarjs/no-duplicate-string
Expand All @@ -48,6 +49,32 @@ if (process.env.GOOGLE_ID && process.env.GOOGLE_SECRET) {
passport.use('google', googleStrategyConfig);
}

/**
* Sign in with Microsoft.
*/
if (process.env.MICROSOFT_ID && process.env.MICROSOFT_SECRET) {
const microsoftStrategyConfig = new MicrosoftStrategy(
{
callbackURL: '/auth/microsoft/callback',
clientID: process.env.MICROSOFT_ID,
clientSecret: process.env.MICROSOFT_SECRET,
passReqToCallback: true,
scope: ['user.read'],
},
async (req, _accessToken, _refreshToken, profile, done) => {
try {
const email = profile.emails[0]?.value;
// eslint-disable-next-line sonarjs/no-duplicate-string
const loggedInUser = await loginWithCredentals({ email, isPassRequired: false, rememberMe: true, tenant: req.headers['x-tenant-id'] });
return done(null, loggedInUser);
} catch (err) {
return done(err);
}
}
);
passport.use('microsoft', microsoftStrategyConfig);
}

/**
* Sign in with GitHub.
*/
Expand Down
44 changes: 7 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@
"nodemailer": "^6.7.7",
"passport": "^0.7.0",
"passport-github2": "^0.1.12",
"passport-google-oauth": "^2.0.0",
"passport-google-oauth20": "^2.0.0",
"passport-http": "^0.3.0",
"passport-linkedin-oauth2": "^2.0.0",
"passport-microsoft": "^2.1.0",
"sequelize": "^6.32.1",
"swagger-autogen": "^2.23.7",
"swagger-ui-express": "^5.0.1",
Expand Down

0 comments on commit 6651834

Please sign in to comment.