Skip to content

Commit d58e77e

Browse files
authored
Merge pull request #295 from acmauth/offline_access
Implement long term token support
2 parents 36393e6 + 28255b6 commit d58e77e

File tree

4 files changed

+12
-17
lines changed

4 files changed

+12
-17
lines changed

src/app.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const appConfig = {
1818
realm: 'universis',
1919
clientId: 'aristomate',
2020
redirectUri: 'https://applink.aristomate.gr/authsso/callback',
21-
scope: 'students:read',
21+
scope: 'students:read offline_access',
2222
isMobile: isMobile,
2323
// Use proxy for web
2424
tokenUrl: isMobile ? undefined : 'https://applink.aristomate.gr/api/auth/token',

src/lib/authentication/OIDCClient.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -374,23 +374,14 @@ class OIDCClient {
374374
}
375375

376376
isRefreshable() {
377+
// With offline access scope, refresh token can't expire
377378
const refreshToken = this.getItemFromStore('refresh_token');
378-
if(refreshToken){
379-
const decodedToken = jwtDecode(refreshToken);
380-
const now = Date.now() / 1000;
381-
return (decodedToken.exp as number) > now;
382-
}
383-
return false;
379+
return !!refreshToken;
384380
}
385381

386382
// Check if user is authenticated
387383
isAuthenticated() {
388-
const refreshToken = this.getItemFromStore('refresh_token');
389-
if (!refreshToken) {
390-
return false;
391-
}
392-
return this.isRefreshable();
393-
384+
return this.isRefreshable();
394385
}
395386

396387
// Make authenticated API request
@@ -440,4 +431,4 @@ const response = await client.fetch('https://api.example.com/data');
440431
441432
// Logout
442433
await client.logout();
443-
*/
434+
*/

src/lib/storage/capacitorPersistedStore.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Preferences } from '@capacitor/preferences';
2-
import type { Writable } from 'svelte/store';
2+
import { get, type Writable } from 'svelte/store';
33
// import { writable, type Writable } from 'svelte/store';
44
import { persisted } from 'svelte-persisted-store';
55
import type { GetResult } from '@capacitor/preferences';
@@ -69,6 +69,7 @@ class CapacitorPersistedStore<T> {
6969
* @param value - The new value of the store.
7070
*/
7171
public set(value: T) {
72+
Preferences.set({ key: this.key, value: JSON.stringify(value) });
7273
this.store.set(value);
7374
}
7475

@@ -78,6 +79,8 @@ class CapacitorPersistedStore<T> {
7879
*/
7980
public update(updater: (value: T) => T) {
8081
this.store.update(updater);
82+
const newValue = get(this.store);
83+
Preferences.set({ key: this.key, value: JSON.stringify(newValue) });
8184
}
8285
}
8386

src/routes/persistedStoreDeclarations.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { userCreds, userTokens, useAlternativeLogin } from "$stores/credentials.
77
import { scheduledNotifications } from "$lib/calendarNotifications/notificationsStore";
88
import { loginStore } from "$src/lib/authentication/loginStore";
99
import { userCredsFlag } from "$components/webmailLogin/userCredsFlagStore";
10+
import { get } from "svelte/store";
1011

1112
const persistedStores: CapacitorPersistedStore<any>[] = [
1213
toggles,
@@ -24,5 +25,5 @@ const persistedStores: CapacitorPersistedStore<any>[] = [
2425

2526
export async function loadPersistedStores() {
2627
console.log('Loading persisted stores');
27-
persistedStores.forEach(async (store) => await store.loadFromStorage());
28-
}
28+
persistedStores.forEach(async (store) => { await store.loadFromStorage();});
29+
}

0 commit comments

Comments
 (0)