Skip to content

Commit ca0669c

Browse files
committed
add user
1 parent 9894644 commit ca0669c

File tree

5 files changed

+54
-18
lines changed

5 files changed

+54
-18
lines changed

App.tsx

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { FC, useEffect } from 'react';
1+
import React, { FC } from 'react';
22
import 'react-native-gesture-handler';
33
import { Provider } from 'react-redux';
44
import { store } from './src/redux/store';
@@ -8,6 +8,8 @@ import { tomorrowFonts } from './src/assets';
88
import { firebaseConfig } from './src/utils';
99
import * as firebase from 'firebase';
1010

11+
firebase.initializeApp(firebaseConfig);
12+
1113
const App: FC = () => {
1214
const [fontsLoaded] = useFonts({
1315
'Tomorrow-Regular': tomorrowFonts.regular,
@@ -16,11 +18,6 @@ const App: FC = () => {
1618
'Tomorrow-SemiBold': tomorrowFonts.semiBold,
1719
'Tomorrow-ExtraBold': tomorrowFonts.extraBold,
1820
});
19-
20-
21-
useEffect(() => {
22-
firebase.initializeApp(firebaseConfig);
23-
}, []);
2421

2522
if (!fontsLoaded) {
2623
return <></>;

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"@eva-design/eva": "^2.0.0",
1212
"@expo-google-fonts/inter": "^0.1.0",
1313
"@expo/vector-icons": "^10.2.1",
14+
"@react-native-community/async-storage": "^1.12.0",
1415
"@react-native-community/masked-view": "0.1.10",
1516
"@react-native-firebase/app": "^8.4.3",
1617
"@react-native-firebase/auth": "^9.2.3",

src/redux/epics/auth.epics.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { RootEpic } from '.';
22
import { AuthTypes, setMessage, setError } from '..';
33
import { map, filter, switchMap, catchError } from 'rxjs/operators';
4-
import { of } from 'rxjs';
4+
import { of, from } from 'rxjs';
55
import { isOfType } from 'typesafe-actions';
6-
7-
import { signInWithGoogle } from '../../repos';
6+
import AsyncStorage from '@react-native-community/async-storage';
7+
import { signInWithGoogle, Storage } from '../../repos';
88

99
export const welcomeEpic: RootEpic = ($action) => {
1010
return $action.pipe(
@@ -19,9 +19,11 @@ export const loginWithGoogleEpic: RootEpic = ($action) => {
1919
switchMap((action) => {
2020
const { accessToken } = action.payload;
2121
return signInWithGoogle(accessToken).pipe(
22-
map((res) => {
23-
console.log(res);
24-
return setMessage('logged in');
22+
switchMap((res) => {
23+
console.log('res', res);
24+
return from(AsyncStorage.setItem(Storage.Email, res as string)).pipe(
25+
map(() => setMessage('logged'))
26+
);
2527
}),
2628
catchError((error) => {
2729
console.log(error);

src/repos/auth/auth.ts

+40-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import { FirebaseCollectionReference } from '../../utils';
22
import firebase from 'firebase';
3-
import { Observable, from, combineLatest } from 'rxjs';
3+
import { Observable } from 'rxjs';
4+
5+
export enum Roles {
6+
Client = 'Client',
7+
Admin = 'Admin',
8+
Manager = 'Manager',
9+
}
10+
11+
export enum Storage {
12+
Email = 'Email',
13+
}
414
export interface User {
515
id: string;
616
email: string;
717
username: string;
8-
role?: [string];
18+
role?: [Roles];
919
phoneNumber?: string;
20+
avatar?: string;
1021
}
1122

1223
export const signInWithGoogle = (accessToken: string) => {
@@ -18,7 +29,32 @@ export const signInWithGoogle = (accessToken: string) => {
1829
firebase
1930
.auth()
2031
.signInWithCredential(credential)
21-
.then((res) => observer.next(res))
22-
.catch(() => observer.error('Something went wrong. Try again'));
32+
.then((res) => {
33+
if (res.user) {
34+
const { email, displayName, photoURL } = res.user;
35+
return FirebaseCollectionReference.users()
36+
.where('email', '==', email)
37+
.get()
38+
.then((user) => {
39+
if (user.empty) {
40+
return FirebaseCollectionReference.users()
41+
.add({
42+
...res.user,
43+
email,
44+
username: displayName,
45+
avatar: photoURL,
46+
role: [Roles.Client],
47+
})
48+
.then(() => observer.next(email));
49+
} else {
50+
observer.next(email);
51+
}
52+
});
53+
}
54+
})
55+
.catch((error) => {
56+
console.log(error);
57+
observer.error('Something went wrong. Try again');
58+
});
2359
});
2460
};

src/utils/firebase.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as firebase from 'firebase';
1+
import { firestore } from 'firebase';
2+
import 'firebase/firestore';
23

3-
const { firestore } = firebase;
44
export enum firebaseCollection {
55
users = 'users',
66
books = 'books',

0 commit comments

Comments
 (0)