Skip to content

Commit 746e26e

Browse files
committed
feat(settings): persist credentials for services
1 parent 1e0ee52 commit 746e26e

File tree

5 files changed

+50
-32
lines changed

5 files changed

+50
-32
lines changed

src/services/tmdb-client/clients/tmdb-client.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { TmdbClientAuthentication } from '~/models/tmdb/tmdb-client.model';
22

33
import { BaseTmdbClient } from '~/services/tmdb-client/clients/base-tmdb-client';
44
import { Config } from '~/settings/tmdb.api';
5-
import { createTab } from '~/utils/browser/browser.utils';
65

76
/**
87
* TmdbClient is a wrapper around the TmdbApi to provide basic authentication and state management.
@@ -23,13 +22,11 @@ export class TmdbClient extends BaseTmdbClient {
2322
*
2423
* @returns A promise resolving to the request token and redirect information.
2524
*/
26-
async requestUserToken(redirect?: boolean, redirect_to?: string) {
25+
async requestUserToken(redirect_to?: string) {
2726
const response = await this.v4.auth.request({ redirect_to });
2827

2928
const { request_token } = await response.json();
3029

31-
if (redirect) createTab({ url: `${Config.requestTokenUrl}${request_token}` });
32-
3330
return {
3431
request_token,
3532
expires: Date.now() + this.settings.requestTokenTTL,
@@ -81,7 +78,7 @@ export class TmdbClient extends BaseTmdbClient {
8178
*
8279
* @returns A promise resolving to the imported authentication information.
8380
*/
84-
importAuthentication(auth: TmdbClientAuthentication): TmdbClientAuthentication {
81+
importAuthentication(auth: TmdbClientAuthentication = {}): TmdbClientAuthentication {
8582
this.updateAuth(auth);
8683
return this.auth;
8784
}

src/services/trakt-client/clients/trakt-client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ export class TraktClient extends BaseTraktClient {
320320
*
321321
* @returns A promise resolving to the imported Trakt authentication information.
322322
*/
323-
async importAuthentication(auth: TraktClientAuthentication): Promise<TraktClientAuthentication> {
323+
async importAuthentication(auth: TraktClientAuthentication = {}): Promise<TraktClientAuthentication> {
324324
this.updateAuth(auth);
325325

326326
if (auth.expires !== undefined && auth.expires < Date.now()) await this.refreshToken();

src/services/tvdb-client/clients/tvdb-client.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ export class TvdbClient extends BaseTvdbClient {
4242
*
4343
* @returns A promise resolving to the imported authentication information.
4444
*/
45-
async importAuthentication(auth: TvdbClientAuthentication): Promise<TvdbClientAuthentication> {
45+
async importAuthentication(auth: TvdbClientAuthentication = {}): Promise<TvdbClientAuthentication> {
4646
this.updateAuth(auth);
4747

48-
if (auth.expires !== undefined && auth.expires < Date.now()) await this.authenticate(auth.userPin);
48+
if (auth?.expires !== undefined && auth.expires < Date.now()) await this.authenticate(auth.userPin);
4949
return this.auth;
5050
}
5151
}

src/stores/settings.store.ts

+37-17
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,59 @@
11
import { defineStore, storeToRefs } from 'pinia';
2-
import { ref } from 'vue';
2+
import { computed, reactive } from 'vue';
33

4+
import type { TmdbClientAuthentication } from '~/models/tmdb/tmdb-client.model';
45
import type { TraktClientAuthentication } from '~/models/trakt/trakt-authentication.model';
56

7+
import type { TvdbClientAuthentication } from '~/models/tvdb/tvdb-client.model';
8+
69
import { storage } from '~/utils/browser/browser-storage.utils';
710

8-
export const useSettingsStore = defineStore('settings', () => {
9-
const auth = ref<TraktClientAuthentication>();
10-
const isAuthenticated = ref(false);
11+
type SettingsAuth = {
12+
trakt?: TraktClientAuthentication;
13+
tvdb?: TvdbClientAuthentication;
14+
tmdb?: TmdbClientAuthentication;
15+
};
1116

12-
const setAuthenticated = (authenticated: boolean = false) => {
13-
isAuthenticated.value = authenticated;
17+
type SettingsAuthenticated = {
18+
trakt?: boolean;
19+
tvdb?: boolean;
20+
tmdb?: boolean;
21+
};
22+
23+
export const useSettingsStore = defineStore('settings', () => {
24+
const auth = reactive<SettingsAuth>({});
25+
const authenticated = reactive<SettingsAuthenticated>({});
26+
const isAuthenticated = computed(() => Object.values(authenticated).every(Boolean));
27+
28+
const setAuthenticated = ({ trakt, tvdb, tmdb }: SettingsAuth = {}) => {
29+
if (trakt) authenticated.trakt = !!trakt.access_token;
30+
if (tvdb) authenticated.tvdb = !!tvdb.accessToken;
31+
if (tmdb) authenticated.tmdb = !!tmdb.accessToken;
1432
};
1533

16-
const syncSetAuth = (_auth: TraktClientAuthentication) => storage.sync.set('settings.auth', _auth);
34+
const syncSetAuth = (_auth: SettingsAuth) => storage.sync.set('settings.auth', _auth);
1735
const syncClearAuth = () => storage.sync.remove('settings.auth');
1836
const syncRestoreAuth = () =>
19-
storage.sync.get<TraktClientAuthentication>('settings.auth').then(_auth => {
20-
auth.value = _auth;
21-
setAuthenticated(!!_auth?.access_token);
37+
storage.sync.get<SettingsAuth>('settings.auth').then(_auth => {
38+
Object.assign(auth, _auth);
39+
setAuthenticated(_auth);
2240
return _auth;
2341
});
2442

25-
const setAuth = (_auth?: TraktClientAuthentication) => {
26-
auth.value = _auth;
27-
setAuthenticated(!!_auth?.access_token);
28-
console.info('settings-store', 'Auth changed', _auth);
29-
if (_auth?.access_token) {
30-
syncSetAuth(_auth).then(() => console.info('settings-store', 'Auth saved', _auth));
43+
const setAuth = (_auth: SettingsAuth = {}) => {
44+
if (_auth.trakt) auth.trakt = _auth.trakt;
45+
if (_auth.tvdb) auth.tvdb = _auth.tvdb;
46+
if (_auth.tmdb) auth.tmdb = _auth.tmdb;
47+
console.info('settings-store', 'Auth changed', { ...auth });
48+
setAuthenticated(_auth);
49+
if (Object.keys(_auth).length > 0) {
50+
syncSetAuth(auth).then(() => console.info('settings-store', 'Auth saved', { ...auth }));
3151
} else {
3252
syncClearAuth().then(() => console.info('settings-store', 'Auth cleared'));
3353
}
3454
};
3555

36-
return { auth, setAuth, isAuthenticated, setAuthenticated, syncRestoreAuth };
56+
return { auth, setAuth, authenticated, isAuthenticated, syncRestoreAuth };
3757
});
3858

3959
export const useSettingsStoreRefs = () => storeToRefs(useSettingsStore());

src/web/init-services.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ import { tmdbService } from '~/services/tmdb-client/tmdb-client.service';
22
import { traktService } from '~/services/trakt-client/trakt-client.service';
33
import { tvdbService } from '~/services/tvdb-client/tvdb-client.service';
44
import { useSettingsStore } from '~/stores/settings.store';
5-
import { storage } from '~/utils/browser/browser-storage.utils';
65

76
export const initServices = async () => {
87
const { setAuth, syncRestoreAuth } = useSettingsStore();
98

109
const auth = await syncRestoreAuth();
1110

12-
if (auth) traktService.importAuthentication(auth).then(() => console.info('TraktClient.importAuthentication', auth));
11+
const restore = [];
12+
if (auth.trakt) restore.push(traktService.importAuthentication(auth.trakt).then(_auth => console.info('TraktClient.importAuthentication', _auth)));
13+
if (auth.tvdb) restore.push(tvdbService.importAuthentication(auth.tvdb).then(_auth => console.info('TvdbClient.importAuthentication', _auth)));
14+
if (auth.tmdb) console.info('TmdbClient.importAuthentication', tmdbService.importAuthentication(auth.tmdb));
15+
await Promise.all(restore);
1316

1417
traktService.onAuthChange(_auth => {
1518
console.info('TraktClient.onAuthChange', _auth);
16-
setAuth(_auth);
19+
setAuth({ trakt: _auth });
1720
});
1821

1922
traktService.onCall(async call => {
@@ -22,8 +25,7 @@ export const initServices = async () => {
2225

2326
tvdbService.onAuthChange(_auth => {
2427
console.info('TvdbClient.onAuthChange', _auth);
25-
// TODO move this to store
26-
storage.sync.set('todo.tvdb.auth', _auth);
28+
setAuth({ tvdb: _auth });
2729
});
2830

2931
tvdbService.onCall(async call => {
@@ -32,8 +34,7 @@ export const initServices = async () => {
3234

3335
tmdbService.onAuthChange(_auth => {
3436
console.info('TmdbClient.onAuthChange', _auth);
35-
// TODO move this to store
36-
storage.sync.set('todo.tmdb.auth', _auth);
37+
setAuth({ tmdb: _auth });
3738
});
3839

3940
tmdbService.onCall(async call => {

0 commit comments

Comments
 (0)