Skip to content

Commit

Permalink
Redirect to requested page
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed Jul 2, 2024
1 parent 27127d0 commit f775e1c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
14 changes: 11 additions & 3 deletions backend/core/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,18 @@ def get_portal_link(reservation):
return f"{protocol}{hostname}{front_path}?email={reservation.email}"


def get_login_link(token):
def get_login_link(token, redirect_to):
from flask import current_app

protocol = "http://" if current_app.config["DEBUG"] else "https://"
hostname = current_app.config["PUBLIC_SERVER_NAME"]
front_path = current_app.config["FRONTEND_LOGIN_PATHNAME"]
return f"{protocol}{hostname}{front_path}?token={token}"

route = f"{protocol}{hostname}{front_path}?token={token}"

if redirect_to:
route = f"{route}&route={redirect_to}"
return route


class QueryParamValidationError(Exception):
Expand Down Expand Up @@ -538,6 +543,7 @@ def cancel_reservation(reservation_id):
def send_login_email():
try:
email = request.get_json()["email"]
redirect_to = request.get_json()["route"]
except KeyError:
return jsonify({"error": "'email' property is mandatory"}), 400

Expand All @@ -555,7 +561,9 @@ def send_login_email():
send_email(
get_mail_subject("Lien de connexion au site de réservation des animations"),
recipients=[email],
html=render_template("login_mail.html", login_link=get_login_link(token.token)),
html=render_template(
"login_mail.html", login_link=get_login_link(token.token, redirect_to)
),
)

return "lien de login envoyé", 204
Expand Down
2 changes: 2 additions & 0 deletions front-vite/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ router.beforeEach((to, from, next) => {
// we are going to a route that may require auth
if (to.meta.requiresAuth) {
// if user is not auth, go to login
// Store requested route
if (!authStore.isAuth) {
authStore.requestedRoute(to.path as string)
if (to.name===ROUTES_NAMES.RESA_FORM) {
next({
path: ROUTES_PATHS.HOME,
Expand Down
13 changes: 11 additions & 2 deletions front-vite/src/stores/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { postApiData, getApiData } from '@/utils/api'
import type { RouteRecordName } from 'vue-router'

interface User {
email: string
Expand All @@ -11,6 +12,12 @@ export const useAuthStore = defineStore('auth', () => {

const user = ref<null | User>(null)

const redirectTo = ref<null | string>(null)

function requestedRoute(route: string) {
redirectTo.value = route as string;
}

const isAuth = computed(() => {
return !!user.value
})
Expand Down Expand Up @@ -61,22 +68,24 @@ export const useAuthStore = defineStore('auth', () => {
/**
* Ask for sending a password-less email login to the user
*/
async function sendLoginEmail (email: string) {
async function sendLoginEmail(email: string) {
await postApiData(
CONFIGURATION.URL_APPLICATION,
'send-login-email',
{ email },
{ "email": email, "route": redirectTo.value }
)
}

return {
user,
isAuth,
isAdmin,
redirectTo,

checkAuth,
login,
logout,
sendLoginEmail,
requestedRoute
}
})
17 changes: 12 additions & 5 deletions front-vite/src/views/LoginCallbackView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { ref, onMounted } from 'vue'
import { ROUTES_PATHS } from '@/router'
const token = useRoute().query.token as string
const redirectTo = useRoute().query.route as string
const authStore = useAuthStore()
const router = useRouter()
Expand All @@ -46,14 +47,20 @@ onMounted(async () => {
await authStore.login(token)
success.value = true
/**
* Si l'utilisateur est admin, on le renvoie sur la page des événements
* Si le paramètre route est spécifié on le renvoie vers la page demandé sauf si login ou home
*/
if (authStore.isAdmin) {
if (redirectTo && ![ROUTES_PATHS.LOGIN, ROUTES_PATHS.HOME].includes(redirectTo)) {
router.push(redirectTo)
}
else if (authStore.isAdmin) {
/**
* Si l'utilisateur est admin, on le renvoie sur la page des événements
*/
router.push(ROUTES_PATHS.EVENT_LISTING)
} else {
/**
* Sinon, sur les réservations
*/
/**
* Sinon, sur les réservations
*/
router.push(ROUTES_PATHS.RESA_LISTING)
}
} catch {
Expand Down

0 comments on commit f775e1c

Please sign in to comment.