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