Don't return empty array for no oauth providers

This commit is contained in:
rijkvanzanten
2020-10-02 14:04:44 -04:00
parent 6b5349af1e
commit a2bb2db180

View File

@@ -204,33 +204,39 @@ router.post(
})
);
router.get('/oauth', asyncHandler(async (req, res, next) => {
const providers = env.OAUTH_PROVIDERS.split(',');
res.locals.payload = { data: providers };
return next();
}));
router.get(
'/oauth',
asyncHandler(async (req, res, next) => {
const providers = env.OAUTH_PROVIDERS.split(',').filter((p: string) => p);
res.locals.payload = { data: providers.length > 0 ? providers : null };
return next();
})
);
router.use(
'/oauth',
session({ secret: env.SECRET as string, saveUninitialized: false, resave: false })
);
router.get('/oauth/:provider', asyncHandler(async(req, res, next) => {
const config = { ...grantConfig };
delete config.defaults;
router.get(
'/oauth/:provider',
asyncHandler(async (req, res, next) => {
const config = { ...grantConfig };
delete config.defaults;
const availableProviders = Object.keys(config);
const availableProviders = Object.keys(config);
if (availableProviders.includes(req.params.provider) === false) {
throw new RouteNotFoundException(`/auth/oauth/${req.params.provider}`);
}
if (availableProviders.includes(req.params.provider) === false) {
throw new RouteNotFoundException(`/auth/oauth/${req.params.provider}`);
}
if (req.query?.redirect && req.session) {
req.session.redirect = req.query.redirect;
}
if (req.query?.redirect && req.session) {
req.session.redirect = req.query.redirect;
}
next();
}));
next();
})
);
router.use(grant.express()(grantConfig));
@@ -249,11 +255,16 @@ router.get(
accountability: accountability,
});
const email = getEmailFromProfile(req.params.provider, req.session!.grant.response?.profile);
const email = getEmailFromProfile(
req.params.provider,
req.session!.grant.response?.profile
);
req.session?.destroy(() => { });
req.session?.destroy(() => {});
const { accessToken, refreshToken, expires } = await authenticationService.authenticate({ email });
const { accessToken, refreshToken, expires } = await authenticationService.authenticate({
email,
});
if (redirect) {
res.cookie('directus_refresh_token', refreshToken, {