Ingen beskrivning

App.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { StatusBar } from 'expo-status-bar';
  2. import React, {useState} from 'react';
  3. import { StyleSheet, Text, View, Platform } from 'react-native';
  4. import * as Google from "expo-google-app-auth";
  5. import {SocialIcon, socialIcon} from 'react-native-elements'
  6. import {createDrawerNavigator} from '@react-navigation/drawer'
  7. import { NavigationContainer } from '@react-navigation/native';
  8. import * as SecureStore from 'expo-secure-store';
  9. import HomeScreen from './Screens/HomeScreen'
  10. import NotificationsScreen from './Screens/NotificationScreen'
  11. import SettingScreen from './Screens/SettingScreen'
  12. import AddTakenCourse from './Screens/AddTakenCourse'
  13. import MyCurriculum from './Screens/MyCurriculum'
  14. const Drawer = createDrawerNavigator()
  15. console.log(Platform.OS)
  16. const IOS_CLIENT_ID =
  17. "116415331974-tf6sehooctplmmn7j0gt831mdf1oqipl.apps.googleusercontent.com";
  18. const ANDROID_CLIENT_ID =
  19. "116415331974-72n6g689k4me386dod763gi31vpuh71a.apps.googleusercontent.com";
  20. // 116415331974-72n6g689k4me386dod763gi31vpuh71a.apps.googleusercontent.com
  21. export default function App() {
  22. const [hasToken, setHasToken] = useState(false)
  23. const signInWithGoogle = async ()=>{
  24. try {
  25. const result = await Google.logInAsync({
  26. iosClientId: IOS_CLIENT_ID,
  27. androidClientId: ANDROID_CLIENT_ID,
  28. scopes: ["profile", "email"]
  29. })
  30. if (result.type == "success"){
  31. console.log(result.accessToken)
  32. try {
  33. // login user in backend
  34. let response = await fetch('http://8c4029a33a9a.ngrok.io/rest-auth/google/', {
  35. method: 'POST',
  36. headers: {
  37. 'content-type': 'application/json'
  38. },
  39. body: JSON.stringify({
  40. access_token: `${result.accessToken}`
  41. })
  42. })
  43. // storing our token
  44. let responseJson = await response.json()
  45. if (responseJson){
  46. if (responseJson.key){
  47. await SecureStore.setItemAsync('token', responseJson.key)
  48. }
  49. }
  50. const token = await SecureStore.getItemAsync('token')
  51. // storing our id
  52. let id = await fetch('http://8c4029a33a9a.ngrok.io/api/get_user_id', {
  53. method: 'GET',
  54. headers: {
  55. 'content-type': 'application/json',
  56. Authorization: `Token ${token}`
  57. }
  58. })
  59. let idJson = await id.json()
  60. if (idJson){
  61. if(idJson.user_id){
  62. idJson = idJson.user_id
  63. let id = idJson.toString()
  64. await SecureStore.setItemAsync('id', id)
  65. }
  66. }
  67. } catch(error){
  68. console.log(error)
  69. }
  70. setHasToken(true) // update states and redirect
  71. }
  72. else {
  73. console.log("no")
  74. }
  75. } catch(error){
  76. console.log(error)
  77. }
  78. }
  79. if (!hasToken){
  80. return (
  81. <View style={styles.container}>
  82. <SocialIcon title="Login With Google" button={true} type={"google"} onPress={signInWithGoogle}/>
  83. </View>
  84. );
  85. }
  86. return (
  87. <NavigationContainer>
  88. <Drawer.Navigator initialRouteName="Home">
  89. <Drawer.Screen name="Home" component={HomeScreen}/>
  90. <Drawer.Screen name="Notifications" component={NotificationsScreen} />
  91. <Drawer.Screen name="Settings" component={SettingScreen} />
  92. <Drawer.Screen name="Add Taken Courses" component={AddTakenCourse} />
  93. <Drawer.Screen name="My Curriculum" component={MyCurriculum} />
  94. </Drawer.Navigator>
  95. </NavigationContainer>
  96. )
  97. }
  98. const styles = StyleSheet.create({
  99. container: {
  100. flex: 1,
  101. backgroundColor: '#fff',
  102. alignItems: 'center',
  103. justifyContent: 'center',
  104. },
  105. });