Skip to content

Commit f0dad92

Browse files
committed
added Anonymous login to CosyncJWT
1 parent bef32ce commit f0dad92

File tree

6 files changed

+82
-16
lines changed

6 files changed

+82
-16
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
3+
4+
5+
React Native Expo Project
6+
7+
- Download the source code from Github: https://github.com/Cosync/CosyncSamples.git
8+
- Go to CosyncSamples/CosyncJWT/ReactNative-Expo/ExpoCosyncJWT
9+
- Install npm package - yarn install
10+
- Go to ios folder and run - pod install
11+
- Copy the Realm id from the top left button in the Realm panel in the web UI
12+
- Edit the Realm appId in the config/Config.js file with the copied Realm Id
13+
- Edit the CosyncApp appToken in the config/Config.js file with the APP_TOKEN from the Cosync Portal
14+
15+
Run the app 'npx react-native run-ios'

CosyncJWT/ReactNative-Expo/ExpoCosyncJWT/app.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"adaptiveIcon": {
2525
"foregroundImage": "./assets/adaptive-icon.png",
2626
"backgroundColor": "#FFFFFF"
27-
}
27+
},
28+
"package": "cosyncjwt.expo.app"
2829
},
2930
"web": {
3031
"favicon": "./assets/favicon.png"

CosyncJWT/ReactNative-Expo/ExpoCosyncJWT/package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CosyncJWT/ReactNative-Expo/ExpoCosyncJWT/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
"@react-navigation/drawer": "^6.4.1",
1515
"@react-navigation/native": "^6.0.10",
1616
"@react-navigation/stack": "^6.2.1",
17-
"cosync-jwt-react-native": "^1.0.6",
17+
"cosync-jwt-react-native": "^1.0.7",
1818
"eslint": "^8.12.0",
1919
"expo": "~44.0.0",
20+
"expo-splash-screen": "~0.14.1",
2021
"expo-status-bar": "~1.2.0",
2122
"react": "17.0.1",
2223
"react-dom": "17.0.1",
@@ -25,13 +26,13 @@
2526
"react-native-reanimated": "~2.3.1",
2627
"react-native-safe-area-context": "3.3.2",
2728
"react-native-screens": "~3.10.1",
29+
"react-native-uuid": "^2.0.1",
2830
"react-native-web": "0.17.1",
2931
"react-navigation": "^4.4.4",
3032
"react-navigation-drawer": "^2.7.2",
3133
"react-navigation-stack": "^2.10.4",
3234
"react-redux": "^7.2.8",
33-
"realm": "^10.14.0",
34-
"expo-splash-screen": "~0.14.1"
35+
"realm": "^10.14.0"
3536
},
3637
"devDependencies": {
3738
"@babel/core": "^7.12.9"

CosyncJWT/ReactNative-Expo/ExpoCosyncJWT/screens/Login.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// Copyright © 2022 cosync. All rights reserved.
2424
//
2525

26-
26+
import uuid from 'react-native-uuid';
2727
import React, {useEffect, useState, useCallback, useRef, useContext } from 'react';
2828
import {
2929
TextInput,
@@ -86,6 +86,43 @@ const Login = ({ navigation }) => {
8686
else return true;
8787
}
8888

89+
90+
const loginAnonymous = () => {
91+
92+
setErrortext('');
93+
setLoading(true);
94+
95+
let id = uuid.v4();
96+
global.cosync.login.loginAnonymous(`ANON_${id}`).then(result => {
97+
98+
console.log('CosyncJWT login result ', result);
99+
100+
global.userData = result;
101+
if(result.code){
102+
setErrortext(result.message);
103+
return;
104+
}
105+
106+
global.cosync.profile.getUser().then(data => {
107+
global.userData.data = data;
108+
109+
global.cosync.realmManager.login(global.userData.jwt, Configure.Realm.appId).then(res => {
110+
setLoading(false);
111+
authCtx.authenticate(result);
112+
})
113+
.catch(err => {
114+
setLoading(false);
115+
setErrortext(`MongoDB Realm Error: ${err.message}`);
116+
})
117+
118+
});
119+
120+
}).catch(err => {
121+
setLoading(false);
122+
setErrortext(err.message);
123+
})
124+
125+
}
89126

90127
const handleSubmitPress = () => {
91128
setErrortext('');
@@ -285,6 +322,13 @@ const Login = ({ navigation }) => {
285322
<Text style={globalStyle.buttonTextStyle}>LOGIN</Text>
286323
</TouchableOpacity>
287324

325+
<TouchableOpacity
326+
style={globalStyle.buttonStyle}
327+
activeOpacity={0.5}
328+
onPress={loginAnonymous}>
329+
<Text style={globalStyle.buttonTextStyle}>LOGIN AS ANONYMOUS</Text>
330+
</TouchableOpacity>
331+
288332
<TouchableOpacity
289333
style={globalStyle.buttonStyle}
290334
activeOpacity={0.5}

CosyncJWT/ReactNative-Expo/ExpoCosyncJWT/yarn.lock

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,10 +2633,10 @@ cosmiconfig@^5.0.5, cosmiconfig@^5.1.0:
26332633
js-yaml "^3.13.1"
26342634
parse-json "^4.0.0"
26352635

2636-
cosync-jwt-react-native@^1.0.6:
2637-
version "1.0.6"
2638-
resolved "https://registry.yarnpkg.com/cosync-jwt-react-native/-/cosync-jwt-react-native-1.0.6.tgz#cf1d74ff419946cc13b76d6db395036a1fcaff55"
2639-
integrity sha512-Dt3nfKNAA7BrIOb4pOap4j9Y1OBcD8P00YJlrXbtxB/dUARnepw7Yr44NjD5xBMP7GEMn97B4hSi1+fs4FQQ1A==
2636+
cosync-jwt-react-native@^1.0.7:
2637+
version "1.0.7"
2638+
resolved "https://registry.yarnpkg.com/cosync-jwt-react-native/-/cosync-jwt-react-native-1.0.7.tgz#e34e017d98e8e8f78d513ed15b633b8250821612"
2639+
integrity sha512-ZgUvHNy3RwX+0PPMy+/cNw1d/zg+ofinL1Z7rIPQBWLh1mntf7AUpz3pFCHCHone6jeU2pCoWjHPddFySssZmA==
26402640
dependencies:
26412641
md5 "^2.3.0"
26422642
realm "^10.4.1"
@@ -5646,6 +5646,11 @@ react-native-screens@^3.4.0, react-native-screens@~3.10.1:
56465646
react-freeze "^1.0.0"
56475647
warn-once "^0.1.0"
56485648

5649+
react-native-uuid@^2.0.1:
5650+
version "2.0.1"
5651+
resolved "https://registry.yarnpkg.com/react-native-uuid/-/react-native-uuid-2.0.1.tgz#ed4e2dfb1683eddb66967eb5dca140dfe1abddb9"
5652+
integrity sha512-cptnoIbL53GTCrWlb/+jrDC6tvb7ypIyzbXNJcpR3Vab0mkeaaVd5qnB3f0whXYzS+SMoSQLcUUB0gEWqkPC0g==
5653+
56495654
react-native-web@0.17.1:
56505655
version "0.17.1"
56515656
resolved "https://registry.npmjs.org/react-native-web/-/react-native-web-0.17.1.tgz"

0 commit comments

Comments
 (0)