Skip to content

Commit

Permalink
feat: refresh auth (#589)
Browse files Browse the repository at this point in the history
- **feat: save user password in preferences**
- **feat: call refresh auth on entry point**

---------

Signed-off-by: phoebus-84 <83974413+phoebus-84@users.noreply.github.com>
  • Loading branch information
phoebus-84 authored Feb 24, 2025
1 parent ea35530 commit 4735c25
Show file tree
Hide file tree
Showing 18 changed files with 209 additions and 87 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@
"@inlang/paraglide-js-adapter-sveltekit": "^0.6.7",
"@ionic/cli": "^7.2.0",
"@ionic/core": "^7.8.6",
"@slangroom/core": "^1.43.2",
"@slangroom/helpers": "^1.43.2",
"@slangroom/http": "^1.43.2",
"@slangroom/pocketbase": "^1.43.2",
"@slangroom/zencode": "^1.43.2",
"@slangroom/core": "^1.44.0",
"@slangroom/helpers": "^1.44.0",
"@slangroom/http": "^1.44.0",
"@slangroom/pocketbase": "^1.44.0",
"@slangroom/zencode": "^1.44.0",
"ajv": "^8.17.1",
"ajv-formats": "^2.1.1",
"capacitor-native-settings": "^6.0.1",
Expand Down
111 changes: 58 additions & 53 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/lib/preferences/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { USER_PREFERENCES_KEY } from './user';
import { CREDENTIALS_PREFERENCES_KEY } from './credentials';
import { DID_PREFERENCES_KEY } from './did';
import { removePreference } from '.';
import { USER_PASSWORD_KEY } from './userPassword';

export const logout = async () => {
await removePreference(KEYPAIR_PREFERENCES_KEY);
await removePreference(USER_PREFERENCES_KEY);
await removePreference(CREDENTIALS_PREFERENCES_KEY);
await removePreference(DID_PREFERENCES_KEY);
await removePreference(USER_PASSWORD_KEY);
};
4 changes: 2 additions & 2 deletions src/lib/preferences/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type UserPreference = {

export async function getUser(): Promise<UserPreference | undefined> {
const auth = (await getStructuredPreferences(USER_PREFERENCES_KEY)) as {
model: UserPreference;
record: UserPreference;
} | undefined;
return auth?.model;
return auth?.record;
}
12 changes: 12 additions & 0 deletions src/lib/preferences/userPassword.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getPreference } from ".";
import { setPreference } from ".";

export const USER_PASSWORD_KEY = "USER_PASSWORD";

export const getUserPassword = async (): Promise<string | null> => {
return await getPreference(USER_PASSWORD_KEY);
};

export const setUserPassword = async (password: string) => {
await setPreference(USER_PASSWORD_KEY, password);
};
6 changes: 6 additions & 0 deletions src/lib/slangroom/refreshAuthToken.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Rule unknown ignore

Given I connect to 'pb_address' and start capacitor pb client
Given I refresh token and output into 'refresh_output'
Given I have a 'string dictionary' named 'refresh_output'
Then print data
15 changes: 9 additions & 6 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import { m } from '$lib/i18n';
import { Network } from '@capacitor/network';
import { debugPopup, debugPopupContent } from '$lib/components/organisms/debug/debug';
import { refreshAuth } from './[[lang]]/(auth)/login/_lib';
const controller = new AbortController();
const signal = controller.signal;
let isConnected: boolean;
let isConnected: boolean = true;
const originalXhrOpen = XMLHttpRequest.prototype.open;
const originalXhrSend = XMLHttpRequest.prototype.send;
Expand All @@ -43,7 +43,7 @@
return originalXhrOpen.apply(this, [method, url, ...rest]);
};
const prettifyJsonString = function (jsonString:any) {
const prettifyJsonString = function (jsonString: any) {
try {
const parsed = JSON.parse(jsonString);
return JSON.stringify(parsed, null, 2);
Expand All @@ -53,10 +53,10 @@
};
XMLHttpRequest.prototype.send = function (body) {
const prettifiedBody = prettifyJsonString(body)
const prettifiedBody = prettifyJsonString(body);
debugPopupContent.push(
// @ts-ignore
`XMLHttpRequest Sent: ${JSON.stringify({ method: this._method, url: this._url, prettifiedBody}, null, 2)}`
`XMLHttpRequest Sent: ${JSON.stringify({ method: this._method, url: this._url, prettifiedBody }, null, 2)}`
);
debugPopup.set(true);
this.addEventListener('load', async function () {
Expand All @@ -71,8 +71,11 @@
onMount(async () => {
isConnected = (await Network.getStatus()).connected;
Network.addListener('networkStatusChange', (status) => {
Network.addListener('networkStatusChange', async (status) => {
isConnected = status.connected;
if (isConnected) {
await refreshAuth();
}
});
document.addEventListener(
'ionBackButton',
Expand Down
4 changes: 2 additions & 2 deletions src/routes/[[lang]]/(auth)/login/(methods)/+layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { r } from '$lib/i18n';

export const load = async () => {
const store = get(userEmailStore);
const { email: userEmail, registration } = store;
const { email: userEmail, registration, password } = store;
if (!userEmail) redirect(303, r('/login'));
return { userEmail, registration };
return { userEmail, registration, password };
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
if (confirmPassword !== password) throw new Error(`The passwords do not match`);
await createUser($userEmailStore.email!, password, confirmPassword);
await login($userEmailStore.email!, password);
userEmailStore.set({
email: $userEmailStore.email!,
registration: $userEmailStore.registration,
password
});
await goto('/login/questions');
} catch (e) {
feedback = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
import background from '$lib/assets/bg-5.svg';
import { Input } from '$lib/forms';
import HeaderWithBackButton from '$lib/components/molecules/HeaderWithBackButton.svelte';
import { setUserPassword } from '$lib/preferences/userPassword.js';
//
export let data;
let { userEmail } = data;
let { userEmail, password } = data;
let feedback: Feedback = {};
Expand All @@ -34,7 +35,8 @@
await setKeypairPreference(keypair);
await generateDid();
await checkKeypairs();
await goto('/wallet', undefined, false);
await setUserPassword(password!);
await goto('/wallet', undefined);
} catch (e) {
feedback = {
type: 'error',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
import { log } from '$lib/log';
import FingerPrint from '$lib/assets/lottieFingerPrint/FingerPrint.svelte';
import HeaderWithBackButton from '$lib/components/molecules/HeaderWithBackButton.svelte';
import { setUserPassword } from '$lib/preferences/userPassword';
//
let { email: userEmail, registration } = $userEmailStore;
let { email: userEmail, registration, password } = $userEmailStore;
//
Expand Down Expand Up @@ -71,8 +72,8 @@
if (registration) await saveUserPublicKeys();
if (!registration) await checkKeypairs();
await generateDid();
seed = keypair.seed;
await setUserPassword(password!)
loading = false;
} catch (e) {
loading = false;
Expand Down
Loading

0 comments on commit 4735c25

Please sign in to comment.