説明なし

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