pocketbase-react 0.1.8 → 0.1.10

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
  export * from './content';
2
2
  export * from './client';
3
- export * from './Pocketbase';
3
+ export * from './auth';
4
+ export * from './Pocketbase';
@@ -1,2 +1,3 @@
1
1
  export * from './useAppContent';
2
- export * from './useClientContext';
2
+ export * from './useClientContext';
3
+ export * from './useAuth';
@@ -0,0 +1,7 @@
1
+ import { useContext } from 'react';
2
+ import { AuthActions, AuthContext } from '../context/auth';
3
+
4
+ export function useAuth(): { actions: AuthActions | null } {
5
+ const actions = useContext(AuthContext);
6
+ return { actions };
7
+ }
@@ -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
+ }
@@ -5,6 +5,7 @@ import { appReducer } from './reducers';
5
5
  import thunk from 'redux-thunk';
6
6
  import { RecordAction } from './reducers/records';
7
7
  import AsyncStorage from '@react-native-async-storage/async-storage';
8
+ import { StorageService } from '../service/Storage';
8
9
 
9
10
  interface Storage {
10
11
  getItem(key: string, ...args: Array<any>): any;
@@ -14,29 +15,14 @@ interface Storage {
14
15
 
15
16
 
16
17
  const CustomStorage: Storage = {
17
- getItem: async (_key: string, ..._args: Array<any>) => {
18
- if (typeof document !== 'undefined') {
19
- return localStorage.getItem(_key);
20
- }
21
- else if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
22
- return await AsyncStorage.getItem(_key);
23
- }
18
+ getItem: async (key: string, ..._args: Array<any>) => {
19
+ return await StorageService.get(key);
24
20
  },
25
- setItem: async (_key: string, _value: any, ..._args: Array<any>) => {
26
- if (typeof document !== 'undefined') {
27
- return localStorage.setItem(_key, _value);
28
- }
29
- else if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
30
- return await AsyncStorage.setItem(_key, _value);
31
- }
21
+ setItem: async (key: string, value: any, ..._args: Array<any>) => {
22
+ return StorageService.set(key, value);
32
23
  },
33
- removeItem: async (_key: string, ..._args: Array<any>) => {
34
- if (typeof document !== 'undefined') {
35
- return localStorage.removeItem(_key);
36
- }
37
- else if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
38
- return await AsyncStorage.removeItem(_key);
39
- }
24
+ removeItem: async (key: string, ..._args: Array<any>) => {
25
+ return StorageService.remove(key);
40
26
  },
41
27
  };
42
28