Skip to content

Commit

Permalink
Finish internationalisation of password management
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-intuitem committed Dec 12, 2024
1 parent c6ffe35 commit 7436cd7
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 15 deletions.
5 changes: 4 additions & 1 deletion frontend/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -979,5 +979,8 @@
"attackPath": "Attack path",
"attackPaths": "Attack paths",
"currentCriticality": "Current criticality",
"residualCriticality": "Residual criticality"
"residualCriticality": "Residual criticality",
"errorAssetGraphMustNotContainCycles": "The asset graph must not contain cycles.",
"resetPasswordHere": "You can reset your password here.",
"resetPassword": "Reset password"
}
4 changes: 3 additions & 1 deletion frontend/messages/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -903,5 +903,7 @@
"bringTheEvidencesHelpText": "Si désactivé, l'objet sera dupliqué sans ses preuves",
"existingControlsHelper": "Que disposez-vous actuellement pour gérer ce risque",
"extraControlsHelper": "Que ferez-vous pour atténuer ce risque",
"existingContextHelper": "Description des mesures existantes (ce champ sera bientôt obsolète)"
"existingContextHelper": "Description des mesures existantes (ce champ sera bientôt obsolète)",
"resetPasswordHere": "Vous pouvez réinitialiser votre mot de passe ici.",
"resetPassword": "Réinitialiser le mot de passe"
}
3 changes: 2 additions & 1 deletion frontend/src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { User } from '$lib/utils/types';
import { redirect, type Handle, type RequestEvent, type HandleFetch } from '@sveltejs/kit';
import { setFlash } from 'sveltekit-flash-message/server';
import { languageTag, setLanguageTag } from '$paraglide/runtime';
import { DEFAULT_LANGUAGE } from '$lib/utils/constants';

import { loadFeatureFlags } from '$lib/feature-flags';

Expand Down Expand Up @@ -66,7 +67,7 @@ export const handle: Handle = async ({ event, resolve }) => {

const errorId = new URL(event.request.url).searchParams.get('error');
if (errorId) {
setLanguageTag(event.cookies.get('ciso_lang') || 'en');
setLanguageTag(event.cookies.get('ciso_lang') || DEFAULT_LANGUAGE);
setFlash({ type: 'error', message: safeTranslate(errorId) }, event);
redirect(302, '/login');
}
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/lib/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ export const BASE_API_URL = `${
: 'http://localhost:8000/api'
}`;

export const DEFAULT_LANGUAGE = `${
Object.hasOwn(env, 'PUBLIC_DEFAULT_LANGUAGE')
? env.PUBLIC_DEFAULT_LANGUAGE
: 'en'
}`;

export const ALLAUTH_API_URL = `${BASE_API_URL}/_allauth/app/v1`;

export const BACKEND_API_EXPOSED_URL = `${
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/routes/(app)/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { LayoutServerLoad } from './$types';
import { redirect } from '@sveltejs/kit';
import { loadFlash } from 'sveltekit-flash-message/server';
import { setLanguageTag, sourceLanguageTag } from '$paraglide/runtime';
import { setLanguageTag } from '$paraglide/runtime';
import { DEFAULT_LANGUAGE } from '$lib/utils/constants';

const loginPageRegex = /^[a-zA-Z0-9]+:\/\/[^\/]+\/login\/?.*$/;

Expand All @@ -21,6 +22,6 @@ export const load = loadFlash(async ({ locals, url, cookies, request }) => {
});
}
}
setLanguageTag(cookies.get('ciso_lang') || sourceLanguageTag);
setLanguageTag(cookies.get('ciso_lang') || DEFAULT_LANGUAGE);
return { user: locals.user, settings: locals.settings };
}) satisfies LayoutServerLoad;
5 changes: 3 additions & 2 deletions frontend/src/routes/(authentication)/login/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { setError, superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import type { PageServerLoad } from './$types';
import { mfaAuthenticateSchema } from './mfa/utils/schemas';
import { DEFAULT_LANGUAGE } from '$lib/utils/constants';

interface AuthenticationFlow {
id:
Expand Down Expand Up @@ -125,8 +126,8 @@ export const actions: Actions = {
const preferencesRes = await fetch(`${BASE_API_URL}/user-preferences/`);
const preferences = await preferencesRes.json();

const currentLang = cookies.get('ciso_lang') || 'en';
const preferedLang = preferences.lang || 'en';
const currentLang = cookies.get('ciso_lang') || DEFAULT_LANGUAGE;
const preferedLang = preferences.lang || DEFAULT_LANGUAGE;

if (currentLang !== preferedLang) {
cookies.set('ciso_lang', preferedLang, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import { setFlash } from 'sveltekit-flash-message/server';
import { setError, superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import type { PageServerLoad } from './$types';
import { setLanguageTag } from '$paraglide/runtime';
import { DEFAULT_LANGUAGE } from '$lib/utils/constants';

export const load: PageServerLoad = async (event) => {
setLanguageTag(event.cookies.get('ciso_lang') || DEFAULT_LANGUAGE);
const form = await superValidate(event.request, zod(ResetPasswordSchema));

return { form };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import TextField from '$lib/components/Forms/TextField.svelte';
import { ResetPasswordSchema } from '$lib/utils/schemas';
import { zod } from 'sveltekit-superforms/adapters';
import * as m from '$paraglide/messages.js';
export let data: PageData;
</script>
Expand All @@ -21,7 +22,7 @@
<i class="fa-solid fa-key" />
</div>
<p class="text-gray-600 text-sm text-center">
You can reset your password here.<br />
{m.resetPasswordHere()}<br />
</p>
<!-- SuperForm with dataType 'form' -->
<div class="flex w-full">
Expand All @@ -32,18 +33,18 @@
let:form
validators={zod(ResetPasswordSchema)}
>
<TextField type="password" {form} field="new_password" label="New password" />
<TextField type="password" {form} field="new_password" label={m.newPassword()} />
<TextField
type="password"
{form}
field="confirm_new_password"
label="Confirm new password"
label={m.confirmNewPassword()}
/>
<p class="pt-3">
<button
class="btn variant-filled-primary font-semibold w-full"
type="submit"
data-testid="set-password-btn">Reset Password</button
data-testid="set-password-btn">{m.resetPassword()}</button
>
</p>
</SuperForm>
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { LayoutServerLoad } from './$types';
import { setLanguageTag } from '$paraglide/runtime';
import { DEFAULT_LANGUAGE } from '$lib/utils/constants';

export const load: LayoutServerLoad = async ({ locals }) => {
export const load: LayoutServerLoad = async ({ locals, cookies }) => {
setLanguageTag(cookies.get('ciso_lang') || DEFAULT_LANGUAGE);
return { featureFlags: locals.featureFlags };
};

6 changes: 3 additions & 3 deletions frontend/src/routes/ParaglideJsProvider.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
languageTag,
onSetLanguageTag,
setLanguageTag,
sourceLanguageTag
} from '$paraglide/runtime';
import { onDestroy, onMount } from 'svelte';
import { browser } from '$app/environment';
import { getCookie, deleteCookie, setCookie } from '$lib/utils/cookies';
import { DEFAULT_LANGUAGE } from '$lib/utils/constants';
onMount(() => {
// const valueFromSession = sessionStorage.getItem('lang') || sourceLanguageTag;
const valueFromCookies = getCookie('ciso_lang') || sourceLanguageTag;
// const valueFromSession = sessionStorage.getItem('lang') || DEFAULT_LANGUAGE;
const valueFromCookies = getCookie('ciso_lang') || DEFAULT_LANGUAGE;
// @ts-ignore
setCookie('ciso_lang', valueFromCookies);
setLanguageTag(valueFromCookies);
Expand Down

0 comments on commit 7436cd7

Please sign in to comment.