Skip to content

Commit

Permalink
fix: try login again the user if refresh token goes wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
phoebus-84 committed Feb 18, 2025
1 parent 06e80da commit d54235f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
2 changes: 0 additions & 2 deletions src/lib/slangroom/refreshAuthToken.slang
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
Rule unknown ignore

Given I connect to 'pb_address' and start capacitor pb client
Given I send my_credentials 'my_credentials' and login and output into 'login_output'
Given I refresh token and output into 'refresh_output'
Given I have a 'string dictionary' named 'login_output'
Given I have a 'string dictionary' named 'refresh_output'
Then print data
14 changes: 2 additions & 12 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@
import { m } from '$lib/i18n';
import { Network } from '@capacitor/network';
import { debugPopup, debugPopupContent } from '$lib/components/organisms/debug/debug';
import { getUser } from '$lib/preferences/user';
import { getUserPassword } from '$lib/preferences/userPassword';
import { refreshAuth } from './[[lang]]/(auth)/login/_lib';
$: refreshUser();
$: refreshAuth();
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 Down Expand Up @@ -73,14 +71,6 @@
return originalXhrSend.apply(this, [body]);
};
const refreshUser = async () => {
const user = await getUser();
const password = await getUserPassword();
if (user && password) {
await refreshAuth(user.email, password);
}
};
onMount(async () => {
isConnected = (await Network.getStatus()).connected;
Network.addListener('networkStatusChange', (status) => {
Expand Down
17 changes: 11 additions & 6 deletions src/routes/[[lang]]/(auth)/login/_lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import getPublicKeysScript from '$lib/slangroom/getPublicKeys.slang?raw';
import askResetPasswordScript from '$lib/slangroom/askResetPassword.slang?raw';
import checkUserExistScript from '$lib/slangroom/checkUserExist.slang?raw';
import refreshAuthToken from '$lib/slangroom/refreshAuthToken.slang?raw';
import { setUserPassword } from '$lib/preferences/userPassword';
import { getUserPassword, setUserPassword } from '$lib/preferences/userPassword';

//

Expand Down Expand Up @@ -192,15 +192,20 @@ export const askResetPassword = async (email: string) => {
return res.result.output;
};

export const refreshAuth = async (email: string, password: string) => {
export const refreshAuth = async () => {
const user = await getUser();
const password = await getUserPassword();
if (!(user || password)) return;
const data = {
pb_address: backendUri,
my_credentials: {
email,
email: user!.email,
password
}
};
const res = await slangroom.execute(refreshAuthToken, { data });
console.log(res);
return res.result.output;
try {
await slangroom.execute(refreshAuthToken, { data });
} catch {
await slangroom.execute(loginScript, { data });
}
};

0 comments on commit d54235f

Please sign in to comment.