pocketbase-react 0.1.8 → 0.1.9
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.
- package/dist/pocketbase-react.js +103 -24
- package/dist/pocketbase-react.min.js +3 -3
- package/es/context/Pocketbase.d.ts +2 -4
- package/es/context/Pocketbase.js +7 -9
- package/es/context/auth.d.ts +38 -0
- package/es/context/auth.js +67 -0
- package/es/context/index.d.ts +1 -0
- package/es/context/index.js +1 -0
- package/es/hooks/index.d.ts +1 -0
- package/es/hooks/index.js +2 -1
- package/es/hooks/useAuth.d.ts +4 -0
- package/es/hooks/useAuth.js +8 -0
- package/es/service/Authentication.d.ts +23 -0
- package/es/service/Authentication.js +32 -0
- package/es/service/Storage.d.ts +5 -0
- package/es/service/Storage.js +20 -0
- package/es/store/store.js +7 -19
- package/lib/context/Pocketbase.js +6 -8
- package/lib/context/auth.js +74 -0
- package/lib/context/index.js +11 -0
- package/lib/hooks/index.js +11 -0
- package/lib/hooks/useAuth.js +12 -0
- package/lib/service/Authentication.js +37 -0
- package/lib/service/Storage.js +26 -0
- package/lib/store/store.js +7 -19
- package/package.json +1 -1
- package/src/context/Pocketbase.tsx +9 -14
- package/src/context/auth.tsx +113 -0
- package/src/context/index.ts +2 -1
- package/src/hooks/index.ts +2 -1
- package/src/hooks/useAuth.ts +7 -0
- package/src/service/Authentication.ts +50 -0
- package/src/service/Storage.ts +23 -0
- package/src/store/store.tsx +7 -21
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
2
|
+
export class StorageService {
|
|
3
|
+
static async get(key) {
|
|
4
|
+
return typeof document !== 'undefined' ? localStorage.getItem(key) : await AsyncStorage.getItem(key);
|
|
5
|
+
}
|
|
6
|
+
static async set(key, value) {
|
|
7
|
+
if (typeof document !== 'undefined') {
|
|
8
|
+
return localStorage.setItem(key, value);
|
|
9
|
+
} else {
|
|
10
|
+
return await AsyncStorage.setItem(key, value);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
static async remove(key) {
|
|
14
|
+
if (typeof document !== 'undefined') {
|
|
15
|
+
return localStorage.removeItem(key);
|
|
16
|
+
} else {
|
|
17
|
+
return await AsyncStorage.removeItem(key);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
package/es/store/store.js
CHANGED
|
@@ -3,28 +3,16 @@ import { persistStore, persistReducer } from 'redux-persist';
|
|
|
3
3
|
import { useSelector } from 'react-redux';
|
|
4
4
|
import { appReducer } from './reducers';
|
|
5
5
|
import thunk from 'redux-thunk';
|
|
6
|
-
import
|
|
6
|
+
import { StorageService } from '../service/Storage';
|
|
7
7
|
const CustomStorage = {
|
|
8
|
-
getItem: async (
|
|
9
|
-
|
|
10
|
-
return localStorage.getItem(_key);
|
|
11
|
-
} else if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
|
|
12
|
-
return await AsyncStorage.getItem(_key);
|
|
13
|
-
}
|
|
8
|
+
getItem: async (key, ..._args) => {
|
|
9
|
+
return await StorageService.get(key);
|
|
14
10
|
},
|
|
15
|
-
setItem: async (
|
|
16
|
-
|
|
17
|
-
return localStorage.setItem(_key, _value);
|
|
18
|
-
} else if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
|
|
19
|
-
return await AsyncStorage.setItem(_key, _value);
|
|
20
|
-
}
|
|
11
|
+
setItem: async (key, value, ..._args) => {
|
|
12
|
+
return StorageService.set(key, value);
|
|
21
13
|
},
|
|
22
|
-
removeItem: async (
|
|
23
|
-
|
|
24
|
-
return localStorage.removeItem(_key);
|
|
25
|
-
} else if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
|
|
26
|
-
return await AsyncStorage.removeItem(_key);
|
|
27
|
-
}
|
|
14
|
+
removeItem: async (key, ..._args) => {
|
|
15
|
+
return StorageService.remove(key);
|
|
28
16
|
}
|
|
29
17
|
};
|
|
30
18
|
export const persistConfig = {
|
|
@@ -10,26 +10,24 @@ var _react2 = require("redux-persist/integration/react");
|
|
|
10
10
|
var store = _interopRequireWildcard(require("../store/store"));
|
|
11
11
|
var _client = require("./client");
|
|
12
12
|
var _content = require("./content");
|
|
13
|
+
var _auth = require("./auth");
|
|
13
14
|
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); }
|
|
14
15
|
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; }
|
|
15
16
|
const PocketbaseContext = (0, React.createContext)(null);
|
|
16
17
|
exports.PocketbaseContext = PocketbaseContext;
|
|
17
18
|
const Pocketbase = props => {
|
|
18
|
-
const
|
|
19
|
-
(0, React.useEffect)(() => {
|
|
20
|
-
const client = new _pocketbase.default(props.serverURL);
|
|
21
|
-
client.admins.authViaEmail(props.credentials.username, props.credentials.password).then(() => {
|
|
22
|
-
setClient(client);
|
|
23
|
-
});
|
|
24
|
-
}, [props.serverURL]);
|
|
19
|
+
const client = new _pocketbase.default(props.serverURL);
|
|
25
20
|
return client ? /*#__PURE__*/React.createElement(_client.ClientProvider, {
|
|
26
21
|
client: client
|
|
27
22
|
}, /*#__PURE__*/React.createElement(_reactRedux.Provider, {
|
|
28
23
|
store: store.store
|
|
29
24
|
}, /*#__PURE__*/React.createElement(_react2.PersistGate, {
|
|
30
25
|
persistor: store.persistor
|
|
26
|
+
}, /*#__PURE__*/React.createElement(_auth.AuthProvider, {
|
|
27
|
+
redirectUrl: props.redirectURL,
|
|
28
|
+
openURL: props.openURL
|
|
31
29
|
}, /*#__PURE__*/React.createElement(_content.ContentProvider, {
|
|
32
30
|
collections: props.initialCollections
|
|
33
|
-
}, props.children)))) : null;
|
|
31
|
+
}, props.children))))) : null;
|
|
34
32
|
};
|
|
35
33
|
exports.Pocketbase = Pocketbase;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.AuthProvider = exports.AuthContext = void 0;
|
|
5
|
+
var React = _interopRequireWildcard(require("react"));
|
|
6
|
+
var _useClientContext = require("../hooks/useClientContext");
|
|
7
|
+
var _Storage = require("../service/Storage");
|
|
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 AuthContext = (0, React.createContext)(null);
|
|
11
|
+
exports.AuthContext = AuthContext;
|
|
12
|
+
const AuthProvider = props => {
|
|
13
|
+
const client = (0, _useClientContext.useClientContext)();
|
|
14
|
+
const [authProviders, setAuthProviders] = React.useState();
|
|
15
|
+
const actions = {
|
|
16
|
+
registerWithEmail: async (email, password) => {
|
|
17
|
+
await (client == null ? void 0 : client.users.create({
|
|
18
|
+
email: email,
|
|
19
|
+
password: password,
|
|
20
|
+
passwordConfirm: password
|
|
21
|
+
}));
|
|
22
|
+
},
|
|
23
|
+
signInWithEmail: async (email, password) => {
|
|
24
|
+
await (client == null ? void 0 : client.users.authViaEmail(email, password));
|
|
25
|
+
},
|
|
26
|
+
signInWithProvider: async provider => {
|
|
27
|
+
const authProvider = authProviders == null ? void 0 : authProviders.find(p => p.name === provider);
|
|
28
|
+
const url = (authProvider == null ? void 0 : authProvider.authUrl) + props.redirectUrl;
|
|
29
|
+
await props.openURL(url);
|
|
30
|
+
await _Storage.StorageService.set('provider', JSON.stringify(authProviders));
|
|
31
|
+
},
|
|
32
|
+
submitProviderResult: async url => {
|
|
33
|
+
const params = new URLSearchParams(url.split('?')[1]);
|
|
34
|
+
const code = params.get('code');
|
|
35
|
+
const state = params.get('state');
|
|
36
|
+
const providersString = await _Storage.StorageService.get('provider');
|
|
37
|
+
if (providersString) {
|
|
38
|
+
const providers = JSON.parse(providersString);
|
|
39
|
+
const authProvider = providers == null ? void 0 : providers.find(p => p.state === state);
|
|
40
|
+
if (authProvider && code) {
|
|
41
|
+
await (client == null ? void 0 : client.users.authViaOAuth2(authProvider.name, code, authProvider.codeVerifier, props.redirectUrl));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
signOut: () => {
|
|
46
|
+
client == null ? void 0 : client.authStore.clear();
|
|
47
|
+
},
|
|
48
|
+
sendPasswordResetEmail: async email => {
|
|
49
|
+
await (client == null ? void 0 : client.users.requestPasswordReset(email));
|
|
50
|
+
},
|
|
51
|
+
sendEmailVerification: async email => {
|
|
52
|
+
await (client == null ? void 0 : client.users.requestVerification(email));
|
|
53
|
+
},
|
|
54
|
+
updateProfile: async (id, record) => {
|
|
55
|
+
await (client == null ? void 0 : client.records.update('profiles', id, record));
|
|
56
|
+
},
|
|
57
|
+
updateEmail: async email => {
|
|
58
|
+
await (client == null ? void 0 : client.users.requestEmailChange(email));
|
|
59
|
+
},
|
|
60
|
+
deleteUser: async id => {
|
|
61
|
+
await (client == null ? void 0 : client.users.delete(id));
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
React.useEffect(() => {
|
|
65
|
+
(async () => {
|
|
66
|
+
const methods = await (client == null ? void 0 : client.users.listAuthMethods());
|
|
67
|
+
setAuthProviders(methods == null ? void 0 : methods.authProviders);
|
|
68
|
+
})();
|
|
69
|
+
}, [props.redirectUrl]);
|
|
70
|
+
return /*#__PURE__*/React.createElement(AuthContext.Provider, {
|
|
71
|
+
value: actions
|
|
72
|
+
}, props.children);
|
|
73
|
+
};
|
|
74
|
+
exports.AuthProvider = AuthProvider;
|
package/lib/context/index.js
CHANGED
|
@@ -23,6 +23,17 @@ Object.keys(_client).forEach(function (key) {
|
|
|
23
23
|
}
|
|
24
24
|
});
|
|
25
25
|
});
|
|
26
|
+
var _auth = require("./auth");
|
|
27
|
+
Object.keys(_auth).forEach(function (key) {
|
|
28
|
+
if (key === "default" || key === "__esModule") return;
|
|
29
|
+
if (key in exports && exports[key] === _auth[key]) return;
|
|
30
|
+
Object.defineProperty(exports, key, {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function () {
|
|
33
|
+
return _auth[key];
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
});
|
|
26
37
|
var _Pocketbase = require("./Pocketbase");
|
|
27
38
|
Object.keys(_Pocketbase).forEach(function (key) {
|
|
28
39
|
if (key === "default" || key === "__esModule") return;
|
package/lib/hooks/index.js
CHANGED
|
@@ -22,4 +22,15 @@ Object.keys(_useClientContext).forEach(function (key) {
|
|
|
22
22
|
return _useClientContext[key];
|
|
23
23
|
}
|
|
24
24
|
});
|
|
25
|
+
});
|
|
26
|
+
var _useAuth = require("./useAuth");
|
|
27
|
+
Object.keys(_useAuth).forEach(function (key) {
|
|
28
|
+
if (key === "default" || key === "__esModule") return;
|
|
29
|
+
if (key in exports && exports[key] === _useAuth[key]) return;
|
|
30
|
+
Object.defineProperty(exports, key, {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function () {
|
|
33
|
+
return _useAuth[key];
|
|
34
|
+
}
|
|
35
|
+
});
|
|
25
36
|
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.useAuth = useAuth;
|
|
5
|
+
var _react = require("react");
|
|
6
|
+
var _auth = require("../context/auth");
|
|
7
|
+
function useAuth() {
|
|
8
|
+
const actions = (0, _react.useContext)(_auth.AuthContext);
|
|
9
|
+
return {
|
|
10
|
+
actions
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
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;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
exports.__esModule = true;
|
|
5
|
+
exports.StorageService = void 0;
|
|
6
|
+
var _asyncStorage = _interopRequireDefault(require("@react-native-async-storage/async-storage"));
|
|
7
|
+
class StorageService {
|
|
8
|
+
static async get(key) {
|
|
9
|
+
return typeof document !== 'undefined' ? localStorage.getItem(key) : await _asyncStorage.default.getItem(key);
|
|
10
|
+
}
|
|
11
|
+
static async set(key, value) {
|
|
12
|
+
if (typeof document !== 'undefined') {
|
|
13
|
+
return localStorage.setItem(key, value);
|
|
14
|
+
} else {
|
|
15
|
+
return await _asyncStorage.default.setItem(key, value);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
static async remove(key) {
|
|
19
|
+
if (typeof document !== 'undefined') {
|
|
20
|
+
return localStorage.removeItem(key);
|
|
21
|
+
} else {
|
|
22
|
+
return await _asyncStorage.default.removeItem(key);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.StorageService = StorageService;
|
package/lib/store/store.js
CHANGED
|
@@ -8,28 +8,16 @@ var _reduxPersist = require("redux-persist");
|
|
|
8
8
|
var _reactRedux = require("react-redux");
|
|
9
9
|
var _reducers = require("./reducers");
|
|
10
10
|
var _reduxThunk = _interopRequireDefault(require("redux-thunk"));
|
|
11
|
-
var
|
|
11
|
+
var _Storage = require("../service/Storage");
|
|
12
12
|
const CustomStorage = {
|
|
13
|
-
getItem: async (
|
|
14
|
-
|
|
15
|
-
return localStorage.getItem(_key);
|
|
16
|
-
} else if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
|
|
17
|
-
return await _asyncStorage.default.getItem(_key);
|
|
18
|
-
}
|
|
13
|
+
getItem: async (key, ..._args) => {
|
|
14
|
+
return await _Storage.StorageService.get(key);
|
|
19
15
|
},
|
|
20
|
-
setItem: async (
|
|
21
|
-
|
|
22
|
-
return localStorage.setItem(_key, _value);
|
|
23
|
-
} else if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
|
|
24
|
-
return await _asyncStorage.default.setItem(_key, _value);
|
|
25
|
-
}
|
|
16
|
+
setItem: async (key, value, ..._args) => {
|
|
17
|
+
return _Storage.StorageService.set(key, value);
|
|
26
18
|
},
|
|
27
|
-
removeItem: async (
|
|
28
|
-
|
|
29
|
-
return localStorage.removeItem(_key);
|
|
30
|
-
} else if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
|
|
31
|
-
return await _asyncStorage.default.removeItem(_key);
|
|
32
|
-
}
|
|
19
|
+
removeItem: async (key, ..._args) => {
|
|
20
|
+
return _Storage.StorageService.remove(key);
|
|
33
21
|
}
|
|
34
22
|
};
|
|
35
23
|
const persistConfig = {
|
package/package.json
CHANGED
|
@@ -6,35 +6,30 @@ import { PersistGate } from 'redux-persist/integration/react';
|
|
|
6
6
|
import * as store from '../store/store';
|
|
7
7
|
import { ClientProvider } from './client';
|
|
8
8
|
import { ContentProvider } from './content';
|
|
9
|
+
import { AuthProvider } from './auth';
|
|
9
10
|
|
|
10
11
|
export const PocketbaseContext = createContext<PocketBase | null>(null);
|
|
11
12
|
|
|
12
13
|
export type PocketbaseProviderProps = {
|
|
13
14
|
children: React.ReactNode;
|
|
14
15
|
serverURL: string;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
password: string;
|
|
18
|
-
};
|
|
16
|
+
redirectURL: string;
|
|
17
|
+
openURL: (url: string) => Promise<void>;
|
|
19
18
|
initialCollections?: string[];
|
|
20
19
|
};
|
|
21
20
|
|
|
22
21
|
export const Pocketbase = (props: PocketbaseProviderProps) => {
|
|
23
|
-
const
|
|
24
|
-
useEffect(() => {
|
|
25
|
-
const client = new PocketBase(props.serverURL);
|
|
26
|
-
client.admins.authViaEmail(props.credentials.username, props.credentials.password).then(() => {
|
|
27
|
-
setClient(client);
|
|
28
|
-
});
|
|
29
|
-
}, [props.serverURL]);
|
|
22
|
+
const client = new PocketBase(props.serverURL);
|
|
30
23
|
|
|
31
24
|
return client ? (
|
|
32
25
|
<ClientProvider client={client}>
|
|
33
26
|
<Provider store={store.store}>
|
|
34
27
|
<PersistGate persistor={store.persistor}>
|
|
35
|
-
<
|
|
36
|
-
{props.
|
|
37
|
-
|
|
28
|
+
<AuthProvider redirectUrl={props.redirectURL} openURL={props.openURL} >
|
|
29
|
+
<ContentProvider collections={props.initialCollections}>
|
|
30
|
+
{props.children}
|
|
31
|
+
</ContentProvider>
|
|
32
|
+
</AuthProvider>
|
|
38
33
|
</PersistGate>
|
|
39
34
|
</Provider>
|
|
40
35
|
</ClientProvider>
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { createContext, useEffect } from 'react';
|
|
4
|
+
import { useClientContext } from '../hooks/useClientContext';
|
|
5
|
+
import { StorageService } from '../service/Storage';
|
|
6
|
+
|
|
7
|
+
export type AuthProviderInfo = {
|
|
8
|
+
name: string;
|
|
9
|
+
state: string;
|
|
10
|
+
codeVerifier: string;
|
|
11
|
+
codeChallenge: string;
|
|
12
|
+
codeChallengeMethod: string;
|
|
13
|
+
authUrl: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type RegisterWithEmailType = (email: string, password: string) => Promise<void>;
|
|
17
|
+
export type SignInWithEmailType = (email: string, password: string) => Promise<void>;
|
|
18
|
+
export type SignInWithProviderType = (provider: string) => Promise<void>;
|
|
19
|
+
export type SubmitProviderResultType = (url: string) => Promise<void>;
|
|
20
|
+
export type SignOutType = () => void;
|
|
21
|
+
export type SendPasswordResetEmailType = (email: string) => Promise<void>;
|
|
22
|
+
export type SendEmailVerificationType = (email: string) => Promise<void>;
|
|
23
|
+
export type UpdateProfileType = (id: string, record: {}) => Promise<void>;
|
|
24
|
+
export type UpdateEmailType = (email: string) => Promise<void>;
|
|
25
|
+
export type DeleteUserType = (id: string) => Promise<void>;
|
|
26
|
+
|
|
27
|
+
export interface AuthActions {
|
|
28
|
+
registerWithEmail: RegisterWithEmailType
|
|
29
|
+
signInWithEmail: SignInWithEmailType;
|
|
30
|
+
signInWithProvider: SignInWithProviderType;
|
|
31
|
+
submitProviderResult: SubmitProviderResultType;
|
|
32
|
+
signOut: SignOutType;
|
|
33
|
+
sendPasswordResetEmail: SendPasswordResetEmailType;
|
|
34
|
+
sendEmailVerification: SendEmailVerificationType;
|
|
35
|
+
updateProfile: UpdateProfileType;
|
|
36
|
+
updateEmail: UpdateEmailType;
|
|
37
|
+
deleteUser: DeleteUserType;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const AuthContext = createContext<AuthActions | null>(null);
|
|
41
|
+
|
|
42
|
+
export type AuthProviderProps = {
|
|
43
|
+
children: React.ReactNode;
|
|
44
|
+
redirectUrl: string;
|
|
45
|
+
openURL: (url: string) => Promise<void>;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
export const AuthProvider = (props: AuthProviderProps) => {
|
|
50
|
+
const client = useClientContext();
|
|
51
|
+
const [authProviders, setAuthProviders] = React.useState<AuthProviderInfo[]>();
|
|
52
|
+
|
|
53
|
+
const actions: AuthActions = {
|
|
54
|
+
registerWithEmail: async (email, password) => {
|
|
55
|
+
await client?.users.create({
|
|
56
|
+
email: email,
|
|
57
|
+
password: password,
|
|
58
|
+
passwordConfirm: password,
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
signInWithEmail: async (email: string, password: string) => {
|
|
62
|
+
await client?.users.authViaEmail(email, password);
|
|
63
|
+
},
|
|
64
|
+
signInWithProvider: async (provider: string) => {
|
|
65
|
+
const authProvider = authProviders?.find((p) => p.name === provider);
|
|
66
|
+
const url = authProvider?.authUrl + props.redirectUrl;
|
|
67
|
+
await props.openURL(url);
|
|
68
|
+
await StorageService.set('provider', JSON.stringify(authProviders));
|
|
69
|
+
},
|
|
70
|
+
submitProviderResult: async (url: string) => {
|
|
71
|
+
const params = new URLSearchParams(url.split('?')[1]);
|
|
72
|
+
const code = params.get('code');
|
|
73
|
+
const state = params.get('state');
|
|
74
|
+
const providersString = await StorageService.get('provider');
|
|
75
|
+
if (providersString) {
|
|
76
|
+
const providers = JSON.parse(providersString) as AuthProviderInfo[];
|
|
77
|
+
const authProvider = providers?.find((p) => p.state === state);
|
|
78
|
+
if (authProvider && code) {
|
|
79
|
+
await client?.users.authViaOAuth2(authProvider.name, code, authProvider.codeVerifier, props.redirectUrl);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
signOut: () => {
|
|
84
|
+
client?.authStore.clear();
|
|
85
|
+
},
|
|
86
|
+
sendPasswordResetEmail: async (email: string) => {
|
|
87
|
+
await client?.users.requestPasswordReset(email);
|
|
88
|
+
},
|
|
89
|
+
sendEmailVerification: async (email: string) => {
|
|
90
|
+
await client?.users.requestVerification(email);
|
|
91
|
+
},
|
|
92
|
+
updateProfile: async (id: string, record: {}) => {
|
|
93
|
+
await client?.records.update('profiles', id, record);
|
|
94
|
+
},
|
|
95
|
+
updateEmail: async (email: string) => {
|
|
96
|
+
await client?.users.requestEmailChange(email);
|
|
97
|
+
},
|
|
98
|
+
deleteUser: async (id: string) => {
|
|
99
|
+
await client?.users.delete(id)
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
React.useEffect(() => {
|
|
104
|
+
(async () => {
|
|
105
|
+
const methods = await client?.users.listAuthMethods();
|
|
106
|
+
setAuthProviders(methods?.authProviders);
|
|
107
|
+
})();
|
|
108
|
+
}, [props.redirectUrl]);
|
|
109
|
+
|
|
110
|
+
return (
|
|
111
|
+
<AuthContext.Provider value={actions}>{props.children}</AuthContext.Provider>
|
|
112
|
+
);
|
|
113
|
+
};
|
package/src/context/index.ts
CHANGED
package/src/hooks/index.ts
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
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(
|
|
18
|
+
'google',
|
|
19
|
+
'CODE',
|
|
20
|
+
'VERIFIER',
|
|
21
|
+
'REDIRECT_URL'
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
async AuthWithOauth(provider: string, code: string, verifier: string) {
|
|
25
|
+
await this.client.users.authViaOAuth2(
|
|
26
|
+
provider,
|
|
27
|
+
code,
|
|
28
|
+
verifier,
|
|
29
|
+
this.redirect_url
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async AuthWithEmail(email: string, password: string){
|
|
34
|
+
await this.client.users.authViaEmail(email, password);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async RegisterWithEmail(email: string, password: string){
|
|
38
|
+
await this.client.users.create({
|
|
39
|
+
email: email,
|
|
40
|
+
password: password,
|
|
41
|
+
passwordConfirm: password,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async getUserData(id:string, token:string) {
|
|
46
|
+
const headers = new Headers()
|
|
47
|
+
headers.append("Authorization","user "+token)
|
|
48
|
+
return await this.client.users.getOne(id)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
2
|
+
|
|
3
|
+
export class StorageService {
|
|
4
|
+
static async get(key: string): Promise<string | null> {
|
|
5
|
+
return typeof document !== 'undefined' ? localStorage.getItem(key) : await AsyncStorage.getItem(key);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
static async set(key: string, value: string): Promise<void> {
|
|
9
|
+
if (typeof document !== 'undefined') {
|
|
10
|
+
return localStorage.setItem(key, value);
|
|
11
|
+
} else {
|
|
12
|
+
return await AsyncStorage.setItem(key, value);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
static async remove(key: string): Promise<void> {
|
|
17
|
+
if (typeof document !== 'undefined') {
|
|
18
|
+
return localStorage.removeItem(key);
|
|
19
|
+
} else {
|
|
20
|
+
return await AsyncStorage.removeItem(key);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|