diff --git a/frontend/src/index.ts b/frontend/src/index.ts index d7d3257f4..546578c70 100644 --- a/frontend/src/index.ts +++ b/frontend/src/index.ts @@ -4,6 +4,10 @@ import {getElemsByClass, getElemById} from './ts/dom-utils'; import {widgetFactory} from './ts/widget'; import {scrollTop} from './ts/scrolltop'; +// #if SANDBOX +import {sandboxRedirect} from './ts/sandbox-redirect'; +// #endif + /** * Entrypoint * The main entrypoint for the application @@ -17,6 +21,12 @@ function entrypoint(): void { // register scroll to top btn functionality const btn = getElemById('scrollToTopBtn'); scrollTop(btn as HTMLButtonElement); + + // #if SANDBOX + // This is used to redirect non AdaCore staff to the main site if + // the staging site is accidentally reached + sandboxRedirect(); + // #endif } (function(): void { diff --git a/frontend/src/ts/sandbox-redirect.ts b/frontend/src/ts/sandbox-redirect.ts new file mode 100644 index 000000000..a36f19013 --- /dev/null +++ b/frontend/src/ts/sandbox-redirect.ts @@ -0,0 +1,33 @@ +import {Cookies} from 'typescript-cookies' + +const cookies = new Cookies({ + path: '/', + secure: true, + samesite: 'none', +}) + +/** + * Redirects the user to main learn site if not authenticated + */ +export function sandboxRedirect(): void { + /* istanbul ignore next */ + const cookieName = "Learn_Sandbox_Authenticated"; + const cookieValue = cookies.get(cookieName) as string; + const cookieReferenceValue = "true"; + + if (cookieValue != cookieReferenceValue) { + const passw = prompt("Enter site password:") + + if (passw != "Ada") { + const msg = 'You have reached learn-sandbox, the learn testing site. ' + + 'This is reserved for testers only. You will be directed to the main ' + + 'learn.adacore.com site after pressing OK.'; + alert(msg); + window.location.href = 'http://learn.adacore.com'; + } + else + { + cookies.set(cookieName, cookieReferenceValue, {expires: 3650}); + } + } +}