暫無描述

App.js 3.4KB

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