Skip to content

Commit

Permalink
redirect to correct page after login / signup
Browse files Browse the repository at this point in the history
  • Loading branch information
paglias committed Sep 27, 2017
1 parent 26ad65c commit ff9aa97
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 20 deletions.
35 changes: 25 additions & 10 deletions website/client/components/auth/registerLogin.vue
Expand Up @@ -226,7 +226,7 @@
}
.toggle-link {
color: #fff !important;
color: $white !important;
}
.forgot-password {
Expand Down Expand Up @@ -307,16 +307,19 @@ export default {
passwordConfirm: this.passwordConfirm,
});
if (this.$store.state.afterLoginRedirect) {
window.location.href = this.$store.state.afterLoginRedirect;
return;
let redirectTo;
if (this.$route.query.redirectTo) {
redirectTo = this.$route.query.redirectTo;
} else {
redirectTo = '/';
}
// @TODO do not reload entire page
// problem is that app.vue created hook should be called again
// after user is logged in / just signed up
// ALSO it's the only way to make sure language data is reloaded and correct for the logged in user
window.location.href = '/';
window.location.href = redirectTo;
},
async login () {
if (!this.username) {
Expand All @@ -330,16 +333,19 @@ export default {
password: this.password,
});
if (this.$store.state.afterLoginRedirect) {
window.location.href = this.$store.state.afterLoginRedirect;
return;
let redirectTo;
if (this.$route.query.redirectTo) {
redirectTo = this.$route.query.redirectTo;
} else {
redirectTo = '/';
}
// @TODO do not reload entire page
// problem is that app.vue created hook should be called again
// after user is logged in / just signed up
// ALSO it's the only way to make sure language data is reloaded and correct for the logged in user
window.location.href = '/';
window.location.href = redirectTo;
},
async socialAuth (network) {
const url = window.location.href;
Expand All @@ -354,10 +360,19 @@ export default {
auth,
});
let redirectTo;
if (this.$route.query.redirectTo) {
redirectTo = this.$route.query.redirectTo;
} else {
redirectTo = '/';
}
// @TODO do not reload entire page
// problem is that app.vue created hook should be called again
// after user is logged in / just signed up
window.location.href = '/';
// ALSO it's the only way to make sure language data is reloaded and correct for the logged in user
window.location.href = redirectTo;
},
handleSubmit () {
if (this.registering) {
Expand Down
8 changes: 6 additions & 2 deletions website/client/components/static/groupPlans.vue
Expand Up @@ -44,8 +44,12 @@
methods: {
goToNewGroupPage () {
if (!this.$store.state.isUserLoggedIn) {
this.$store.state.afterLoginRedirect = '/group-plans';
this.$router.push('/register');
this.$router.push({
name: 'register',
query: {
redirectTo: '/group-plans',
},
});
return;
}
Expand Down
12 changes: 8 additions & 4 deletions website/client/components/static/home.vue
Expand Up @@ -620,6 +620,7 @@
let re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
},
// @TODO this is totally duplicate from the registerLogin component
async register () {
if (this.password !== this.passwordConfirm) {
alert('Passwords must match');
Expand All @@ -633,12 +634,15 @@
passwordConfirm: this.passwordConfirm,
});
if (this.$store.state.afterLoginRedirect) {
window.location.href = this.$store.state.afterLoginRedirect;
return;
let redirectTo;
if (this.$route.query.redirectTo) {
redirectTo = this.$route.query.redirectTo;
} else {
redirectTo = '/';
}
window.location.href = '/';
window.location.href = redirectTo;
},
playButtonClick () {
Analytics.track({
Expand Down
23 changes: 20 additions & 3 deletions website/client/router.js
Expand Up @@ -274,9 +274,26 @@ router.beforeEach(function routerGuard (to, from, next) {
if (!isUserLoggedIn && routeRequiresLogin) {
// Redirect to the login page unless the user is trying to reach the
// root of the website, in which case show the home page.
// TODO when redirecting to login if user login then redirect back to initial page
// so if you tried to go to /party you'll be redirected to /party after login/signup
return next({name: to.path === '/' ? 'home' : 'login'});
// Pass the requested page as a query parameter to redirect later.

const redirectTo = to.path === '/' ? 'home' : 'login';
return next({
name: redirectTo,
query: redirectTo === 'login' ? {
redirectTo: to.path,
} : null,
});
}

// Keep the redirectTo query param when going from login to register
// !to.query.redirectTo is to avoid entering a loop of infinite redirects
if (to.name === 'register' && !to.query.redirectTo && from.name === 'login' && from.query.redirectTo) {
return next({
name: 'register',
query: {
redirectTo: from.query.redirectTo,
},
});
}

if (isUserLoggedIn && (to.name === 'login' || to.name === 'register')) {
Expand Down
1 change: 0 additions & 1 deletion website/client/store/index.js
Expand Up @@ -129,7 +129,6 @@ export default function () {
upgradingGroup: {},
notificationStore: [],
modalStack: [],
afterLoginRedirect: '',
userIdToMessage: '',
},
});
Expand Down

0 comments on commit ff9aa97

Please sign in to comment.