pocketbase-react 0.1.19 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,13 @@
1
1
  import * as React from 'react';
2
2
  import { createContext, useEffect } from 'react';
3
- import PocketBase from '@tobicrain/pocketbase';
3
+ import PocketBase, { Admin, BaseAuthStore, LocalAuthStore, User } from '@tobicrain/pocketbase';
4
4
  import { Provider } from 'react-redux';
5
5
  import { PersistGate } from 'redux-persist/integration/react';
6
- import * as store from '../store/store';
6
+ import { store, persistor } from '../store/store';
7
7
  import { ClientProvider } from './client';
8
8
  import { ContentProvider } from './content';
9
9
  import { AuthProvider } from './auth';
10
+ import { StorageService } from '../service/Storage';
10
11
 
11
12
  export const PocketbaseContext = createContext<PocketBase | null>(null);
12
13
 
@@ -20,19 +21,31 @@ export type PocketbaseProviderProps = {
20
21
  };
21
22
 
22
23
  export const Pocketbase = (props: PocketbaseProviderProps) => {
23
- const client = new PocketBase(props.serverURL);
24
+ const [client, setClient] = React.useState<PocketBase | null>(null);
25
+ const [initialCollections, setInitialCollections] = React.useState<string[]>();
26
+ useEffect(() => {
27
+ const client = new PocketBase(props.serverURL);
28
+ client.authStore.onChange(async () => {
29
+ await StorageService.set(StorageService.Constants.COOKIE, client.authStore.exportToCookie());
30
+ setInitialCollections(props.initialCollections);
31
+ });
32
+ StorageService.get(StorageService.Constants.COOKIE).then((cookie) => {
33
+ if (cookie) {
34
+ client.authStore.loadFromCookie(cookie);
35
+ }
36
+ setClient(client);
37
+ });
38
+ }, [props.serverURL]);
24
39
 
25
40
  return client ? (
26
41
  <ClientProvider client={client}>
27
- <Provider store={store.store}>
28
- <PersistGate persistor={store.persistor}>
42
+ <Provider store={store}>
43
+ <PersistGate persistor={persistor}>
29
44
  <AuthProvider
30
45
  webRedirectUrl={props.webRedirectUrl}
31
46
  mobileRedirectUrl={props.mobileRedirectUrl}
32
47
  openURL={props.openURL}>
33
- <ContentProvider collections={props.initialCollections}>
34
- {props.children}
35
- </ContentProvider>
48
+ <ContentProvider collections={initialCollections}>{props.children}</ContentProvider>
36
49
  </AuthProvider>
37
50
  </PersistGate>
38
51
  </Provider>
@@ -1,7 +1,5 @@
1
- import AsyncStorage from '@react-native-async-storage/async-storage';
2
- import { Admin, User } from '@tobicrain/pocketbase';
3
1
  import * as React from 'react';
4
- import { createContext, useEffect } from 'react';
2
+ import { createContext } from 'react';
5
3
  import { useClientContext } from '../hooks/useClientContext';
6
4
  import { StorageService } from '../service/Storage';
7
5
 
@@ -3,6 +3,7 @@ import * as store from '../store';
3
3
  import { ContentContext } from '../context';
4
4
  import { Record } from '../interfaces/Record';
5
5
  import { StorageService } from '../service/Storage';
6
+ import { useAppSelector } from '../store';
6
7
 
7
8
  export type SubscribeType = () => Promise<void | undefined>;
8
9
  export type UnsubscribeType = () => Promise<void | undefined>;
@@ -24,9 +25,8 @@ export function useAppContent<T extends Record>(
24
25
  collectionName: string,
25
26
  initialFetch: boolean = false
26
27
  ): { records: T[]; actions: Actions; isSubscribed: boolean } {
27
- const records = (store.useAppSelector((state) => state.reducer.records[collectionName]) ??
28
- []) as T[];
29
- const subscriptions = store.useAppSelector((state) => state.reducer.subscriptions).subscriptions;
28
+ const records = useAppSelector((state) => state.reducer.records[collectionName]) as T[];
29
+ const subscriptions = useAppSelector((state) => state.reducer.subscriptions);
30
30
  const context = useContext(ContentContext);
31
31
 
32
32
  useEffect(() => {
@@ -1,6 +1,7 @@
1
1
  import { Admin, User } from '@tobicrain/pocketbase';
2
2
  import { useContext, useEffect, useState } from 'react';
3
3
  import { AuthActions, AuthContext } from '../context/auth';
4
+ import { StorageService } from '../service/Storage';
4
5
  import { useClientContext } from './useClientContext';
5
6
 
6
7
  export interface AuthContextInterface {
@@ -18,6 +19,10 @@ export function useAuth(): AuthContextInterface {
18
19
  function updateAuth() {
19
20
  setIsSignedIn(client?.authStore.token !== '');
20
21
  setUser(client?.authStore.model ?? null);
22
+ const cookie = client?.authStore.exportToCookie();
23
+ if (cookie) {
24
+ StorageService.set(StorageService.Constants.COOKIE, cookie);
25
+ }
21
26
  }
22
27
 
23
28
  useEffect(() => {
@@ -3,6 +3,7 @@ import AsyncStorage from '@react-native-async-storage/async-storage';
3
3
  export class StorageService {
4
4
  static Constants = {
5
5
  SUBSCRIBED: 'subscribed',
6
+ COOKIE: 'cookie',
6
7
  };
7
8
 
8
9
  static async get(key: string): Promise<string | null> {
@@ -3,12 +3,8 @@ import { records } from './records';
3
3
  import { subscriptions } from './subscriptions';
4
4
 
5
5
  export const appReducer = combineReducers({
6
- records: records,
7
- subscriptions: subscriptions,
6
+ records,
7
+ subscriptions,
8
8
  });
9
9
 
10
- interface AppReducer {
11
- records: ReturnType<typeof appReducer>;
12
- }
13
-
14
- export type State = AppReducer;
10
+ export type State = ReturnType<typeof appReducer>;
@@ -1,46 +1,29 @@
1
1
  import * as ReduxType from '../types';
2
2
 
3
- export interface ReduxSubscriptions {
4
- subscriptions: string[];
5
- }
6
-
7
3
  export type SubscriptionAction = {
8
4
  type: ReduxType.SubscriptionsTypes;
9
5
  payload: string | string[];
10
6
  };
11
7
 
12
8
  function appendSubscription(subscription: string, subscriptions: string[]): string[] {
13
- return [...subscriptions, subscription];
9
+ return subscriptions.includes(subscription) ? subscriptions : [...subscriptions, subscription];
14
10
  }
15
11
 
16
12
  function deleteSubscription(subscription: string, subscriptions: string[]): string[] {
17
13
  return subscriptions.filter((sub) => sub !== subscription);
18
14
  }
19
15
 
20
- export const subscriptions = (
21
- state: ReduxSubscriptions = {
22
- subscriptions: [],
23
- },
24
- action: SubscriptionAction
25
- ) => {
26
- const list = state.subscriptions;
27
-
16
+ export const subscriptions = (state: string[] = [], action: SubscriptionAction) => {
28
17
  switch (action.type) {
29
18
  case ReduxType.SET_SUBSCRIPTIONS:
30
19
  if (Array.isArray(action.payload)) {
31
- return {
32
- subscriptions: action.payload,
33
- };
20
+ return action.payload as string[];
34
21
  }
35
22
  case ReduxType.ADD_SUBSCRIPTION:
36
- return {
37
- subscriptions: appendSubscription(action.payload as string, list),
38
- };
23
+ return appendSubscription(action.payload as string, state) as string[];
39
24
  case ReduxType.DELETE_SUBSCRIPTION:
40
- return {
41
- subscriptions: deleteSubscription(action.payload as string, list),
42
- };
25
+ return deleteSubscription(action.payload as string, state) as string[];
43
26
  default:
44
- return state;
27
+ return state as string[];
45
28
  }
46
29
  };
@@ -1,23 +0,0 @@
1
- import PocketBase from '@tobicrain/pocketbase';
2
- export declare class AuthenticationService {
3
- private client;
4
- redirect_url: string;
5
- constructor(client: PocketBase);
6
- getListAuthMethods(): Promise<{
7
- [key: string]: any;
8
- emailPassword: boolean;
9
- authProviders: {
10
- name: string;
11
- state: string;
12
- codeVerifier: string;
13
- codeChallenge: string;
14
- codeChallengeMethod: string;
15
- authUrl: string;
16
- }[];
17
- }>;
18
- getDataAuth(): Promise<void>;
19
- AuthWithOauth(provider: string, code: string, verifier: string): Promise<void>;
20
- AuthWithEmail(email: string, password: string): Promise<void>;
21
- RegisterWithEmail(email: string, password: string): Promise<void>;
22
- getUserData(id: string, token: string): Promise<import("@tobicrain/pocketbase").User>;
23
- }
@@ -1,32 +0,0 @@
1
- export class AuthenticationService {
2
- constructor(client) {
3
- this.client = void 0;
4
- this.redirect_url = void 0;
5
- this.client = client;
6
- this.redirect_url = '';
7
- }
8
- async getListAuthMethods() {
9
- return await this.client.users.listAuthMethods();
10
- }
11
- async getDataAuth() {
12
- await this.client.users.authViaOAuth2('google', 'CODE', 'VERIFIER', 'REDIRECT_URL');
13
- }
14
- async AuthWithOauth(provider, code, verifier) {
15
- await this.client.users.authViaOAuth2(provider, code, verifier, this.redirect_url);
16
- }
17
- async AuthWithEmail(email, password) {
18
- await this.client.users.authViaEmail(email, password);
19
- }
20
- async RegisterWithEmail(email, password) {
21
- await this.client.users.create({
22
- email: email,
23
- password: password,
24
- passwordConfirm: password
25
- });
26
- }
27
- async getUserData(id, token) {
28
- const headers = new Headers();
29
- headers.append('Authorization', 'user ' + token);
30
- return await this.client.users.getOne(id);
31
- }
32
- }
@@ -1,37 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.AuthenticationService = void 0;
5
- class AuthenticationService {
6
- constructor(client) {
7
- this.client = void 0;
8
- this.redirect_url = void 0;
9
- this.client = client;
10
- this.redirect_url = '';
11
- }
12
- async getListAuthMethods() {
13
- return await this.client.users.listAuthMethods();
14
- }
15
- async getDataAuth() {
16
- await this.client.users.authViaOAuth2('google', 'CODE', 'VERIFIER', 'REDIRECT_URL');
17
- }
18
- async AuthWithOauth(provider, code, verifier) {
19
- await this.client.users.authViaOAuth2(provider, code, verifier, this.redirect_url);
20
- }
21
- async AuthWithEmail(email, password) {
22
- await this.client.users.authViaEmail(email, password);
23
- }
24
- async RegisterWithEmail(email, password) {
25
- await this.client.users.create({
26
- email: email,
27
- password: password,
28
- passwordConfirm: password
29
- });
30
- }
31
- async getUserData(id, token) {
32
- const headers = new Headers();
33
- headers.append('Authorization', 'user ' + token);
34
- return await this.client.users.getOne(id);
35
- }
36
- }
37
- exports.AuthenticationService = AuthenticationService;
@@ -1,40 +0,0 @@
1
- import PocketBase from '@tobicrain/pocketbase';
2
-
3
- export class AuthenticationService {
4
- private client: PocketBase;
5
- public redirect_url: string;
6
-
7
- constructor(client: PocketBase) {
8
- this.client = client;
9
- this.redirect_url = '';
10
- }
11
-
12
- async getListAuthMethods() {
13
- return await this.client.users.listAuthMethods();
14
- }
15
-
16
- async getDataAuth() {
17
- await this.client.users.authViaOAuth2('google', 'CODE', 'VERIFIER', 'REDIRECT_URL');
18
- }
19
- async AuthWithOauth(provider: string, code: string, verifier: string) {
20
- await this.client.users.authViaOAuth2(provider, code, verifier, this.redirect_url);
21
- }
22
-
23
- async AuthWithEmail(email: string, password: string) {
24
- await this.client.users.authViaEmail(email, password);
25
- }
26
-
27
- async RegisterWithEmail(email: string, password: string) {
28
- await this.client.users.create({
29
- email: email,
30
- password: password,
31
- passwordConfirm: password,
32
- });
33
- }
34
-
35
- async getUserData(id: string, token: string) {
36
- const headers = new Headers();
37
- headers.append('Authorization', 'user ' + token);
38
- return await this.client.users.getOne(id);
39
- }
40
- }