feat(api): add custom redirect back
This commit is contained in:
committed by
mrugesh
parent
3823ed19bc
commit
aa62fdbfe9
@ -54,8 +54,19 @@ module.exports = function enableAuthentication(app) {
|
|||||||
} else {
|
} else {
|
||||||
api.get(
|
api.get(
|
||||||
'/signin',
|
'/signin',
|
||||||
|
(req, res, next) => {
|
||||||
|
if (req && req.query && req.query.returnTo) {
|
||||||
|
req.query.returnTo = `${homeLocation}/${req.query.returnTo}`;
|
||||||
|
}
|
||||||
|
return next();
|
||||||
|
},
|
||||||
ifUserRedirect,
|
ifUserRedirect,
|
||||||
passport.authenticate('auth0-login', {})
|
(req, res, next) => {
|
||||||
|
const state = req.query.returnTo
|
||||||
|
? Buffer.from(req.query.returnTo).toString('base64')
|
||||||
|
: null;
|
||||||
|
return passport.authenticate('auth0-login', { state })(req, res, next);
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
api.get(
|
api.get(
|
||||||
|
@ -101,6 +101,10 @@ export const createPassportCallbackAuthenticator = (strategy, config) => (
|
|||||||
res,
|
res,
|
||||||
next
|
next
|
||||||
) => {
|
) => {
|
||||||
|
const returnTo =
|
||||||
|
req && req.query && req.query.state
|
||||||
|
? Buffer.from(req.query.state, 'base64').toString('utf-8')
|
||||||
|
: `${homeLocation}/learn`;
|
||||||
return passport.authenticate(
|
return passport.authenticate(
|
||||||
strategy,
|
strategy,
|
||||||
{ session: false },
|
{ session: false },
|
||||||
@ -112,7 +116,7 @@ export const createPassportCallbackAuthenticator = (strategy, config) => (
|
|||||||
if (!user || !userInfo) {
|
if (!user || !userInfo) {
|
||||||
return res.redirect('/signin');
|
return res.redirect('/signin');
|
||||||
}
|
}
|
||||||
const redirect = `${homeLocation}/learn`;
|
const redirect = `${returnTo}`;
|
||||||
|
|
||||||
const { accessToken } = userInfo;
|
const { accessToken } = userInfo;
|
||||||
const { provider } = config;
|
const { provider } = config;
|
||||||
|
@ -34,6 +34,8 @@ export default {
|
|||||||
authPath: '/auth/auth0',
|
authPath: '/auth/auth0',
|
||||||
callbackPath: '/auth/auth0/callback',
|
callbackPath: '/auth/auth0/callback',
|
||||||
useCustomCallback: true,
|
useCustomCallback: true,
|
||||||
|
passReqToCallback: true,
|
||||||
|
state: false,
|
||||||
successRedirect: successRedirect,
|
successRedirect: successRedirect,
|
||||||
failureRedirect: failureRedirect,
|
failureRedirect: failureRedirect,
|
||||||
scope: ['openid profile email'],
|
scope: ['openid profile email'],
|
||||||
|
@ -56,10 +56,13 @@ export function ifNotVerifiedRedirectToUpdateEmail(req, res, next) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function ifUserRedirectTo(path = `${homeLocation}/`, status) {
|
export function ifUserRedirectTo(path = `${homeLocation}/`, status) {
|
||||||
status = status === 302 ? 302 : 301;
|
status = status === 301 ? 301 : 302;
|
||||||
return (req, res, next) => {
|
return (req, res, next) => {
|
||||||
const { accessToken } = getAccessTokenFromRequest(req);
|
const { accessToken } = getAccessTokenFromRequest(req);
|
||||||
if (req.user && accessToken) {
|
if (req.user && accessToken) {
|
||||||
|
if (req.query && req.query.returnTo) {
|
||||||
|
return res.status(status).redirect(req.query.returnTo);
|
||||||
|
}
|
||||||
return res.status(status).redirect(path);
|
return res.status(status).redirect(path);
|
||||||
}
|
}
|
||||||
if (req.user && !accessToken) {
|
if (req.user && !accessToken) {
|
||||||
|
@ -162,7 +162,7 @@ export function ShowSettings(props) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!showLoading && !isSignedIn) {
|
if (!showLoading && !isSignedIn) {
|
||||||
return navigate(`${apiLocation}/signin`);
|
return navigate(`${apiLocation}/signin?returnTo=settings`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -11,7 +11,9 @@ describe('<ShowSettings />', () => {
|
|||||||
const shallow = new ShallowRenderer();
|
const shallow = new ShallowRenderer();
|
||||||
shallow.render(<ShowSettings {...loggedOutProps} />);
|
shallow.render(<ShowSettings {...loggedOutProps} />);
|
||||||
expect(navigate).toHaveBeenCalledTimes(1);
|
expect(navigate).toHaveBeenCalledTimes(1);
|
||||||
expect(navigate).toHaveBeenCalledWith(`${apiLocation}/signin`);
|
expect(navigate).toHaveBeenCalledWith(
|
||||||
|
`${apiLocation}/signin?returnTo=settings`
|
||||||
|
);
|
||||||
expect(true).toBeTruthy();
|
expect(true).toBeTruthy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -91,7 +91,7 @@ export class DonatePage extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!showLoading && !isSignedIn) {
|
if (!showLoading && !isSignedIn) {
|
||||||
return navigate(`${apiLocation}/signin`);
|
return navigate(`${apiLocation}/signin?returnTo=donate`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -11,7 +11,9 @@ describe('<ShowSettings />', () => {
|
|||||||
const shallow = new ShallowRenderer();
|
const shallow = new ShallowRenderer();
|
||||||
shallow.render(<DonatePage {...loggedOutProps} />);
|
shallow.render(<DonatePage {...loggedOutProps} />);
|
||||||
expect(navigate).toHaveBeenCalledTimes(1);
|
expect(navigate).toHaveBeenCalledTimes(1);
|
||||||
expect(navigate).toHaveBeenCalledWith(`${apiLocation}/signin`);
|
expect(navigate).toHaveBeenCalledWith(
|
||||||
|
`${apiLocation}/signin?returnTo=donate`
|
||||||
|
);
|
||||||
expect(true).toBeTruthy();
|
expect(true).toBeTruthy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -50,7 +50,7 @@ function ProfilePage(props) {
|
|||||||
return <Loader fullScreen={true} />;
|
return <Loader fullScreen={true} />;
|
||||||
}
|
}
|
||||||
if (!showLoading && !isSignedIn) {
|
if (!showLoading && !isSignedIn) {
|
||||||
return navigate(`${apiLocation}/signin`);
|
return navigate(`${apiLocation}/signin?returnTo=portfolio`);
|
||||||
}
|
}
|
||||||
const RedirecUser = createRedirect('/' + username);
|
const RedirecUser = createRedirect('/' + username);
|
||||||
return <RedirecUser />;
|
return <RedirecUser />;
|
||||||
|
Reference in New Issue
Block a user