pocketbase-react 0.1.18 → 0.1.20

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,3 +1,4 @@
1
1
  import * as recordsAction from './records';
2
2
  import * as subscriptionsAction from './subscriptions';
3
- export { recordsAction, subscriptionsAction };
3
+ import * as authAction from './auth';
4
+ export { recordsAction, subscriptionsAction, authAction };
@@ -1,3 +1,4 @@
1
1
  import * as recordsAction from './records';
2
2
  import * as subscriptionsAction from './subscriptions';
3
- export { recordsAction, subscriptionsAction };
3
+ import * as authAction from './auth';
4
+ export { recordsAction, subscriptionsAction, authAction };
@@ -0,0 +1,12 @@
1
+ import { Admin, User } from '@tobicrain/pocketbase';
2
+ import * as ReduxType from '../types';
3
+ export interface ReduxAuth {
4
+ token: string;
5
+ model: User | Admin | null;
6
+ cookie: string | null;
7
+ }
8
+ export declare type AuthAction = {
9
+ type: ReduxType.AuthTypes;
10
+ payload: User | Admin | null | string;
11
+ };
12
+ export declare const auth: (state: ReduxAuth | undefined, action: AuthAction) => ReduxAuth;
@@ -0,0 +1,36 @@
1
+ import _extends from "@babel/runtime/helpers/esm/extends";
2
+ import * as ReduxType from '../types';
3
+ export const auth = (state = {
4
+ model: null,
5
+ token: '',
6
+ cookie: null
7
+ }, action) => {
8
+ switch (action.type) {
9
+ case ReduxType.SET_TOKEN:
10
+ return _extends({}, state, {
11
+ token: action.payload
12
+ });
13
+ case ReduxType.SET_MODEL:
14
+ return _extends({}, state, {
15
+ model: action.payload
16
+ });
17
+ case ReduxType.SET_COOKIE:
18
+ return _extends({}, state, {
19
+ cookie: action.payload
20
+ });
21
+ case ReduxType.DELETE_TOKEN:
22
+ return _extends({}, state, {
23
+ token: ''
24
+ });
25
+ case ReduxType.DELETE_MODEL:
26
+ return _extends({}, state, {
27
+ model: null
28
+ });
29
+ case ReduxType.DELETE_COOKIE:
30
+ return _extends({}, state, {
31
+ cookie: null
32
+ });
33
+ default:
34
+ return state;
35
+ }
36
+ };
@@ -1,7 +1,8 @@
1
1
  export declare const appReducer: import("redux").Reducer<import("redux").CombinedState<{
2
2
  records: import("./records").ReduxRecord;
3
3
  subscriptions: import("./subscriptions").ReduxSubscriptions;
4
- }>, import("./records").RecordAction | import("./subscriptions").SubscriptionAction>;
4
+ auth: import("./auth").ReduxAuth;
5
+ }>, import("./records").RecordAction | import("./subscriptions").SubscriptionAction | import("./auth").AuthAction>;
5
6
  interface AppReducer {
6
7
  records: ReturnType<typeof appReducer>;
7
8
  }
@@ -1,7 +1,9 @@
1
1
  import { combineReducers } from 'redux';
2
2
  import { records } from './records';
3
3
  import { subscriptions } from './subscriptions';
4
+ import { auth } from './auth';
4
5
  export const appReducer = combineReducers({
5
6
  records: records,
6
- subscriptions: subscriptions
7
+ subscriptions: subscriptions,
8
+ auth: auth
7
9
  });
@@ -16,12 +16,13 @@ declare const store: import("@reduxjs/toolkit").EnhancedStore<import("redux").Co
16
16
  reducer: import("redux").EmptyObject & {
17
17
  records: import("./reducers/records").ReduxRecord;
18
18
  subscriptions: import("./reducers/subscriptions").ReduxSubscriptions;
19
+ auth: import("./reducers/auth").ReduxAuth;
19
20
  } & import("redux-persist/es/persistReducer").PersistPartial;
20
- }>, RecordAction | import("./reducers/subscriptions").SubscriptionAction, (import("redux-thunk").ThunkMiddleware<any, import("redux").AnyAction, undefined> & {
21
+ }>, RecordAction | import("./reducers/subscriptions").SubscriptionAction | import("./reducers/auth").AuthAction, (import("redux-thunk").ThunkMiddleware<any, import("redux").AnyAction, undefined> & {
21
22
  withExtraArgument<ExtraThunkArg, State = any, BasicAction extends import("redux").Action<any> = import("redux").AnyAction>(extraArgument: ExtraThunkArg): import("redux-thunk").ThunkMiddleware<State, BasicAction, ExtraThunkArg>;
22
23
  })[]>;
23
24
  declare type AppDispatch = typeof store.dispatch<RecordAction>;
24
- declare const useAppDispatch: import("redux").Dispatch<RecordAction | import("./reducers/subscriptions").SubscriptionAction>;
25
+ declare const useAppDispatch: import("redux").Dispatch<RecordAction | import("./reducers/subscriptions").SubscriptionAction | import("./reducers/auth").AuthAction>;
25
26
  declare type RootState = ReturnType<typeof store.getState>;
26
27
  declare const useAppSelector: TypedUseSelectorHook<RootState>;
27
28
  declare const persistor: import("redux-persist").Persistor;
@@ -9,3 +9,10 @@ export declare const SET_SUBSCRIPTIONS = "SET_SUBSCRIPTIONS";
9
9
  export declare const ADD_SUBSCRIPTION = "ADD_SUBSCRIPTION";
10
10
  export declare const DELETE_SUBSCRIPTION = "DELETE_SUBSCRIPTION";
11
11
  export declare type SubscriptionsTypes = typeof SET_SUBSCRIPTIONS | typeof ADD_SUBSCRIPTION | typeof DELETE_SUBSCRIPTION;
12
+ export declare const SET_MODEL = "SET_MODEL";
13
+ export declare const SET_TOKEN = "SET_TOKEN";
14
+ export declare const SET_COOKIE = "SET_COOKIE";
15
+ export declare const DELETE_MODEL = "DELETE_MODEL";
16
+ export declare const DELETE_TOKEN = "DELETE_TOKEN";
17
+ export declare const DELETE_COOKIE = "DELETE_COOKIE";
18
+ export declare type AuthTypes = typeof SET_MODEL | typeof SET_TOKEN | typeof DELETE_MODEL | typeof DELETE_TOKEN | typeof SET_COOKIE | typeof DELETE_COOKIE;
@@ -6,4 +6,10 @@ export const DELETE_RECORD = 'DELETE_RECORD';
6
6
  export const DELETE_RECORDS = 'DELETE_RECORDS';
7
7
  export const SET_SUBSCRIPTIONS = 'SET_SUBSCRIPTIONS';
8
8
  export const ADD_SUBSCRIPTION = 'ADD_SUBSCRIPTION';
9
- export const DELETE_SUBSCRIPTION = 'DELETE_SUBSCRIPTION';
9
+ export const DELETE_SUBSCRIPTION = 'DELETE_SUBSCRIPTION';
10
+ export const SET_MODEL = 'SET_MODEL';
11
+ export const SET_TOKEN = 'SET_TOKEN';
12
+ export const SET_COOKIE = 'SET_COOKIE';
13
+ export const DELETE_MODEL = 'DELETE_MODEL';
14
+ export const DELETE_TOKEN = 'DELETE_TOKEN';
15
+ export const DELETE_COOKIE = 'DELETE_COOKIE';
@@ -7,7 +7,7 @@ var React = _interopRequireWildcard(require("react"));
7
7
  var _pocketbase = _interopRequireDefault(require("@tobicrain/pocketbase"));
8
8
  var _reactRedux = require("react-redux");
9
9
  var _react2 = require("redux-persist/integration/react");
10
- var store = _interopRequireWildcard(require("../store/store"));
10
+ var _store = require("../store/store");
11
11
  var _client = require("./client");
12
12
  var _content = require("./content");
13
13
  var _auth = require("./auth");
@@ -20,9 +20,9 @@ const Pocketbase = props => {
20
20
  return client ? /*#__PURE__*/React.createElement(_client.ClientProvider, {
21
21
  client: client
22
22
  }, /*#__PURE__*/React.createElement(_reactRedux.Provider, {
23
- store: store.store
23
+ store: _store.store
24
24
  }, /*#__PURE__*/React.createElement(_react2.PersistGate, {
25
- persistor: store.persistor
25
+ persistor: _store.persistor
26
26
  }, /*#__PURE__*/React.createElement(_auth.AuthProvider, {
27
27
  webRedirectUrl: props.webRedirectUrl,
28
28
  mobileRedirectUrl: props.mobileRedirectUrl,
@@ -5,6 +5,7 @@ exports.AuthProvider = exports.AuthContext = void 0;
5
5
  var React = _interopRequireWildcard(require("react"));
6
6
  var _useClientContext = require("../hooks/useClientContext");
7
7
  var _Storage = require("../service/Storage");
8
+ var _store = require("../store");
8
9
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
9
10
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
10
11
  const AuthContext = (0, React.createContext)({});
@@ -61,12 +62,28 @@ const AuthProvider = props => {
61
62
  await (client == null ? void 0 : client.users.delete(id));
62
63
  }
63
64
  };
65
+ const dispatch = _store.useAppDispatch;
66
+ const selector = (0, _store.useAppSelector)(state => state.reducer.auth);
64
67
  React.useEffect(() => {
68
+ const listener = client == null ? void 0 : client.authStore.onChange(() => {
69
+ const cookie = client == null ? void 0 : client.authStore.exportToCookie();
70
+ if (cookie) dispatch(_store.authAction.setCookie(cookie));
71
+ });
65
72
  (async () => {
66
73
  const methods = await (client == null ? void 0 : client.users.listAuthMethods());
67
74
  setAuthProviders(methods == null ? void 0 : methods.authProviders);
68
75
  })();
76
+ return () => {
77
+ if (listener) listener();
78
+ };
69
79
  }, [props.webRedirectUrl, props.mobileRedirectUrl]);
80
+ React.useEffect(() => {
81
+ const cookie = selector.cookie;
82
+ if (cookie) {
83
+ console.log('cookie', cookie);
84
+ client == null ? void 0 : client.authStore.loadFromCookie(cookie);
85
+ }
86
+ }, [selector.cookie]);
70
87
  return /*#__PURE__*/React.createElement(AuthContext.Provider, {
71
88
  value: actions
72
89
  }, props.children);
@@ -4,16 +4,26 @@ exports.__esModule = true;
4
4
  exports.useAuth = useAuth;
5
5
  var _react = require("react");
6
6
  var _auth = require("../context/auth");
7
+ var _Storage = require("../service/Storage");
7
8
  var _useClientContext = require("./useClientContext");
8
9
  function useAuth() {
9
10
  const client = (0, _useClientContext.useClientContext)();
10
11
  const actions = (0, _react.useContext)(_auth.AuthContext);
11
12
  const [isSignedIn, setIsSignedIn] = (0, _react.useState)(false);
12
13
  const [user, setUser] = (0, _react.useState)(null);
14
+ function updateAuth() {
15
+ var _client$authStore$mod;
16
+ setIsSignedIn((client == null ? void 0 : client.authStore.token) !== '');
17
+ setUser((_client$authStore$mod = client == null ? void 0 : client.authStore.model) != null ? _client$authStore$mod : null);
18
+ const cookie = client == null ? void 0 : client.authStore.exportToCookie();
19
+ if (cookie) {
20
+ _Storage.StorageService.set(_Storage.StorageService.Constants.COOKIE, cookie);
21
+ }
22
+ }
13
23
  (0, _react.useEffect)(() => {
24
+ updateAuth();
14
25
  client == null ? void 0 : client.authStore.onChange(() => {
15
- setIsSignedIn((client == null ? void 0 : client.authStore.token) !== '');
16
- setUser(client == null ? void 0 : client.authStore.model);
26
+ updateAuth();
17
27
  });
18
28
  }, []);
19
29
  return {
@@ -25,5 +25,6 @@ class StorageService {
25
25
  }
26
26
  exports.StorageService = StorageService;
27
27
  StorageService.Constants = {
28
- SUBSCRIBED: 'subscribed'
28
+ SUBSCRIBED: 'subscribed',
29
+ COOKIE: 'cookie'
29
30
  };
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.setToken = exports.setModel = exports.setCookie = exports.deleteToken = exports.deleteModel = exports.deleteCookie = void 0;
5
+ var ReduxType = _interopRequireWildcard(require("../types"));
6
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
7
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
8
+ const setToken = payload => ({
9
+ type: ReduxType.SET_TOKEN,
10
+ payload
11
+ });
12
+ exports.setToken = setToken;
13
+ const setModel = payload => ({
14
+ type: ReduxType.SET_MODEL,
15
+ payload
16
+ });
17
+ exports.setModel = setModel;
18
+ const setCookie = payload => ({
19
+ type: ReduxType.SET_COOKIE,
20
+ payload
21
+ });
22
+ exports.setCookie = setCookie;
23
+ const deleteToken = () => ({
24
+ type: ReduxType.DELETE_TOKEN,
25
+ payload: ''
26
+ });
27
+ exports.deleteToken = deleteToken;
28
+ const deleteModel = () => ({
29
+ type: ReduxType.DELETE_MODEL,
30
+ payload: ''
31
+ });
32
+ exports.deleteModel = deleteModel;
33
+ const deleteCookie = () => ({
34
+ type: ReduxType.DELETE_COOKIE,
35
+ payload: ''
36
+ });
37
+ exports.deleteCookie = deleteCookie;
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
- exports.subscriptionsAction = exports.recordsAction = void 0;
4
+ exports.subscriptionsAction = exports.recordsAction = exports.authAction = void 0;
5
5
  var recordsAction = _interopRequireWildcard(require("./records"));
6
6
  exports.recordsAction = recordsAction;
7
7
  var subscriptionsAction = _interopRequireWildcard(require("./subscriptions"));
8
8
  exports.subscriptionsAction = subscriptionsAction;
9
+ var authAction = _interopRequireWildcard(require("./auth"));
10
+ exports.authAction = authAction;
9
11
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
10
12
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ exports.__esModule = true;
5
+ exports.auth = void 0;
6
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
7
+ var ReduxType = _interopRequireWildcard(require("../types"));
8
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
9
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
10
+ const auth = (state = {
11
+ model: null,
12
+ token: '',
13
+ cookie: null
14
+ }, action) => {
15
+ switch (action.type) {
16
+ case ReduxType.SET_TOKEN:
17
+ return (0, _extends2.default)({}, state, {
18
+ token: action.payload
19
+ });
20
+ case ReduxType.SET_MODEL:
21
+ return (0, _extends2.default)({}, state, {
22
+ model: action.payload
23
+ });
24
+ case ReduxType.SET_COOKIE:
25
+ return (0, _extends2.default)({}, state, {
26
+ cookie: action.payload
27
+ });
28
+ case ReduxType.DELETE_TOKEN:
29
+ return (0, _extends2.default)({}, state, {
30
+ token: ''
31
+ });
32
+ case ReduxType.DELETE_MODEL:
33
+ return (0, _extends2.default)({}, state, {
34
+ model: null
35
+ });
36
+ case ReduxType.DELETE_COOKIE:
37
+ return (0, _extends2.default)({}, state, {
38
+ cookie: null
39
+ });
40
+ default:
41
+ return state;
42
+ }
43
+ };
44
+ exports.auth = auth;
@@ -5,8 +5,10 @@ exports.appReducer = void 0;
5
5
  var _redux = require("redux");
6
6
  var _records = require("./records");
7
7
  var _subscriptions = require("./subscriptions");
8
+ var _auth = require("./auth");
8
9
  const appReducer = (0, _redux.combineReducers)({
9
10
  records: _records.records,
10
- subscriptions: _subscriptions.subscriptions
11
+ subscriptions: _subscriptions.subscriptions,
12
+ auth: _auth.auth
11
13
  });
12
14
  exports.appReducer = appReducer;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
- exports.UPDATE_RECORD = exports.SET_SUBSCRIPTIONS = exports.SET_RECORDS = exports.DELETE_SUBSCRIPTION = exports.DELETE_RECORDS = exports.DELETE_RECORD = exports.ADD_SUBSCRIPTION = exports.ADD_RECORDS = exports.ADD_RECORD = void 0;
4
+ exports.UPDATE_RECORD = exports.SET_TOKEN = exports.SET_SUBSCRIPTIONS = exports.SET_RECORDS = exports.SET_MODEL = exports.SET_COOKIE = exports.DELETE_TOKEN = exports.DELETE_SUBSCRIPTION = exports.DELETE_RECORDS = exports.DELETE_RECORD = exports.DELETE_MODEL = exports.DELETE_COOKIE = exports.ADD_SUBSCRIPTION = exports.ADD_RECORDS = exports.ADD_RECORD = void 0;
5
5
  const SET_RECORDS = 'SET_RECORDS';
6
6
  exports.SET_RECORDS = SET_RECORDS;
7
7
  const ADD_RECORD = 'ADD_RECORD';
@@ -19,4 +19,16 @@ exports.SET_SUBSCRIPTIONS = SET_SUBSCRIPTIONS;
19
19
  const ADD_SUBSCRIPTION = 'ADD_SUBSCRIPTION';
20
20
  exports.ADD_SUBSCRIPTION = ADD_SUBSCRIPTION;
21
21
  const DELETE_SUBSCRIPTION = 'DELETE_SUBSCRIPTION';
22
- exports.DELETE_SUBSCRIPTION = DELETE_SUBSCRIPTION;
22
+ exports.DELETE_SUBSCRIPTION = DELETE_SUBSCRIPTION;
23
+ const SET_MODEL = 'SET_MODEL';
24
+ exports.SET_MODEL = SET_MODEL;
25
+ const SET_TOKEN = 'SET_TOKEN';
26
+ exports.SET_TOKEN = SET_TOKEN;
27
+ const SET_COOKIE = 'SET_COOKIE';
28
+ exports.SET_COOKIE = SET_COOKIE;
29
+ const DELETE_MODEL = 'DELETE_MODEL';
30
+ exports.DELETE_MODEL = DELETE_MODEL;
31
+ const DELETE_TOKEN = 'DELETE_TOKEN';
32
+ exports.DELETE_TOKEN = DELETE_TOKEN;
33
+ const DELETE_COOKIE = 'DELETE_COOKIE';
34
+ exports.DELETE_COOKIE = DELETE_COOKIE;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pocketbase-react",
3
- "version": "0.1.18",
3
+ "version": "0.1.20",
4
4
  "description": "Unofficial React SDK (React, React Native, Expo) for interacting with the PocketBase JS SDK",
5
5
  "keywords": [
6
6
  "pocketbase",
@@ -1,12 +1,14 @@
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';
11
+ import { authAction } from '../store';
10
12
 
11
13
  export const PocketbaseContext = createContext<PocketBase | null>(null);
12
14
 
@@ -24,8 +26,8 @@ export const Pocketbase = (props: PocketbaseProviderProps) => {
24
26
 
25
27
  return client ? (
26
28
  <ClientProvider client={client}>
27
- <Provider store={store.store}>
28
- <PersistGate persistor={store.persistor}>
29
+ <Provider store={store}>
30
+ <PersistGate persistor={persistor}>
29
31
  <AuthProvider
30
32
  webRedirectUrl={props.webRedirectUrl}
31
33
  mobileRedirectUrl={props.mobileRedirectUrl}
@@ -4,6 +4,7 @@ import * as React from 'react';
4
4
  import { createContext, useEffect } from 'react';
5
5
  import { useClientContext } from '../hooks/useClientContext';
6
6
  import { StorageService } from '../service/Storage';
7
+ import { authAction, store, useAppDispatch, useAppSelector } from '../store';
7
8
 
8
9
  export type AuthProviderInfo = {
9
10
  name: string;
@@ -108,13 +109,32 @@ export const AuthProvider = (props: AuthProviderProps) => {
108
109
  await client?.users.delete(id);
109
110
  },
110
111
  };
112
+ const dispatch = useAppDispatch;
113
+ const selector = useAppSelector((state) => state.reducer.auth);
111
114
 
112
115
  React.useEffect(() => {
116
+ const listener = client?.authStore.onChange(() => {
117
+ const cookie = client?.authStore.exportToCookie();
118
+ if (cookie) dispatch(authAction.setCookie(cookie));
119
+ });
120
+
113
121
  (async () => {
114
122
  const methods = await client?.users.listAuthMethods();
115
123
  setAuthProviders(methods?.authProviders);
116
124
  })();
125
+
126
+ return () => {
127
+ if (listener) listener();
128
+ };
117
129
  }, [props.webRedirectUrl, props.mobileRedirectUrl]);
118
130
 
131
+ React.useEffect(() => {
132
+ const cookie = selector.cookie;
133
+ if (cookie) {
134
+ console.log('cookie', cookie);
135
+ client?.authStore.loadFromCookie(cookie);
136
+ }
137
+ }, [selector.cookie]);
138
+
119
139
  return <AuthContext.Provider value={actions}>{props.children}</AuthContext.Provider>;
120
140
  };
@@ -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 {
@@ -15,10 +16,19 @@ export function useAuth(): AuthContextInterface {
15
16
  const [isSignedIn, setIsSignedIn] = useState(false);
16
17
  const [user, setUser] = useState<User | Admin | null>(null);
17
18
 
19
+ function updateAuth() {
20
+ setIsSignedIn(client?.authStore.token !== '');
21
+ setUser(client?.authStore.model ?? null);
22
+ const cookie = client?.authStore.exportToCookie();
23
+ if (cookie) {
24
+ StorageService.set(StorageService.Constants.COOKIE, cookie);
25
+ }
26
+ }
27
+
18
28
  useEffect(() => {
29
+ updateAuth();
19
30
  client?.authStore.onChange(() => {
20
- setIsSignedIn(client?.authStore.token !== '');
21
- setUser(client?.authStore.model);
31
+ updateAuth();
22
32
  });
23
33
  }, []);
24
34
 
@@ -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> {
@@ -0,0 +1,41 @@
1
+ import { Admin, User } from '@tobicrain/pocketbase';
2
+ import { AuthAction } from '../reducers/auth';
3
+ import * as ReduxType from '../types';
4
+
5
+ const setToken = (payload: string) =>
6
+ ({
7
+ type: ReduxType.SET_TOKEN,
8
+ payload,
9
+ } as AuthAction);
10
+
11
+ const setModel = (payload: User | Admin | null | string) =>
12
+ ({
13
+ type: ReduxType.SET_MODEL,
14
+ payload,
15
+ } as AuthAction);
16
+
17
+ const setCookie = (payload: string) =>
18
+ ({
19
+ type: ReduxType.SET_COOKIE,
20
+ payload,
21
+ } as AuthAction);
22
+
23
+ const deleteToken = () =>
24
+ ({
25
+ type: ReduxType.DELETE_TOKEN,
26
+ payload: '',
27
+ } as AuthAction);
28
+
29
+ const deleteModel = () =>
30
+ ({
31
+ type: ReduxType.DELETE_MODEL,
32
+ payload: '',
33
+ } as AuthAction);
34
+
35
+ const deleteCookie = () =>
36
+ ({
37
+ type: ReduxType.DELETE_COOKIE,
38
+ payload: '',
39
+ } as AuthAction);
40
+
41
+ export { setToken, setModel, setCookie, deleteToken, deleteModel, deleteCookie };
@@ -1,4 +1,5 @@
1
1
  import * as recordsAction from './records';
2
2
  import * as subscriptionsAction from './subscriptions';
3
+ import * as authAction from './auth';
3
4
 
4
- export { recordsAction, subscriptionsAction };
5
+ export { recordsAction, subscriptionsAction, authAction };
@@ -0,0 +1,57 @@
1
+ import { Admin, User } from '@tobicrain/pocketbase';
2
+ import * as ReduxType from '../types';
3
+
4
+ export interface ReduxAuth {
5
+ token: string;
6
+ model: User | Admin | null;
7
+ cookie: string | null;
8
+ }
9
+
10
+ export type AuthAction = {
11
+ type: ReduxType.AuthTypes;
12
+ payload: User | Admin | null | string;
13
+ };
14
+
15
+ export const auth = (
16
+ state: ReduxAuth = {
17
+ model: null,
18
+ token: '',
19
+ cookie: null,
20
+ },
21
+ action: AuthAction
22
+ ) => {
23
+ switch (action.type) {
24
+ case ReduxType.SET_TOKEN:
25
+ return {
26
+ ...state,
27
+ token: action.payload as string,
28
+ };
29
+ case ReduxType.SET_MODEL:
30
+ return {
31
+ ...state,
32
+ model: action.payload as User | Admin | null,
33
+ };
34
+ case ReduxType.SET_COOKIE:
35
+ return {
36
+ ...state,
37
+ cookie: action.payload as string,
38
+ };
39
+ case ReduxType.DELETE_TOKEN:
40
+ return {
41
+ ...state,
42
+ token: '',
43
+ };
44
+ case ReduxType.DELETE_MODEL:
45
+ return {
46
+ ...state,
47
+ model: null,
48
+ };
49
+ case ReduxType.DELETE_COOKIE:
50
+ return {
51
+ ...state,
52
+ cookie: null,
53
+ };
54
+ default:
55
+ return state;
56
+ }
57
+ };
@@ -1,10 +1,12 @@
1
1
  import { combineReducers } from 'redux';
2
2
  import { records } from './records';
3
3
  import { subscriptions } from './subscriptions';
4
+ import { auth } from './auth';
4
5
 
5
6
  export const appReducer = combineReducers({
6
7
  records: records,
7
8
  subscriptions: subscriptions,
9
+ auth: auth,
8
10
  });
9
11
 
10
12
  interface AppReducer {
@@ -21,3 +21,18 @@ export type SubscriptionsTypes =
21
21
  | typeof SET_SUBSCRIPTIONS
22
22
  | typeof ADD_SUBSCRIPTION
23
23
  | typeof DELETE_SUBSCRIPTION;
24
+
25
+ export const SET_MODEL = 'SET_MODEL';
26
+ export const SET_TOKEN = 'SET_TOKEN';
27
+ export const SET_COOKIE = 'SET_COOKIE';
28
+ export const DELETE_MODEL = 'DELETE_MODEL';
29
+ export const DELETE_TOKEN = 'DELETE_TOKEN';
30
+ export const DELETE_COOKIE = 'DELETE_COOKIE';
31
+
32
+ export type AuthTypes =
33
+ | typeof SET_MODEL
34
+ | typeof SET_TOKEN
35
+ | typeof DELETE_MODEL
36
+ | typeof DELETE_TOKEN
37
+ | typeof SET_COOKIE
38
+ | typeof DELETE_COOKIE;