Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve login Redirection #262

Merged
merged 9 commits into from
Jan 18, 2023
5 changes: 5 additions & 0 deletions web/frontend/src/pages/session/HandleLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const handleLogin = async (fctx: FlashState) => {
try {
const res = await fetch(ENDPOINT_GET_TEQ_KEY);

const d = new Date();
d.setTime(d.getTime() + 120000);
let expires = 'expires=' + d.toUTCString();
document.cookie = 'redirect' + '=' + window.location.pathname + ';' + expires + ';path=/';

if (res.status !== 200) {
const txt = await res.text();
throw new Error(`unexpected status: ${res.status} - ${txt}`);
Expand Down
22 changes: 19 additions & 3 deletions web/frontend/src/pages/session/Logged.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AuthContext, FlashContext, FlashLevel } from 'index';
import React, { FC, useContext, useEffect } from 'react';
import { FC, useContext, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';

Expand All @@ -12,14 +12,30 @@ const Logged: FC = () => {
const authCtx = useContext(AuthContext);
const fctx = useContext(FlashContext);

const getCookie = () => {
let name = 'redirect' + '=';
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for (let c of ca) {
let char = c;
while (char.charAt(0) == ' ') {
char = char.substring(1);
}
if (char.indexOf(name) == 0) {
return char.substring(name.length, char.length);
}
}
return '/'; // default value is the home page
};

useEffect(() => {
if (authCtx.isLogged) {
fctx.addMessage(t('loggedIn'), FlashLevel.Info);
} else {
fctx.addMessage(t('notLoggedIn'), FlashLevel.Error);
}

navigate('/');
const redir = getCookie();
navigate(redir);
});

return <></>;
Expand Down
2 changes: 1 addition & 1 deletion web/frontend/src/pages/session/Login.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useEffect, useState } from 'react';
import { FC, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import PropTypes from 'prop-types';

Expand Down