Açıklama Yok

Login.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. const Drawer = createDrawerNavigator()
  20. const IOS_CLIENT_ID =
  21. "116415331974-tf6sehooctplmmn7j0gt831mdf1oqipl.apps.googleusercontent.com";
  22. export default function Login() {
  23. const [hasToken, setHasToken] = useState(false)
  24. const signInWithGoogle = async ()=>{
  25. try {
  26. const result = await Google.logInAsync({
  27. iosClientId: IOS_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://da0406585426.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://da0406585426.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. <Drawer.Navigator initialRouteName="Home">
  88. <Drawer.Screen name="Home" component={HomeScreen}/>
  89. <Drawer.Screen name="Notifications" component={NotificationsScreen} />
  90. {/* <Drawer.Screen name="Settings" component={SettingScreen} /> */}
  91. <Drawer.Screen name="Add Taken Courses" component={AddTakenCourse} />
  92. <Drawer.Screen name="My Curriculum" component={MyCurriculum} />
  93. <Drawer.Screen name="Enroll Next Semester" component={EnrollNextSemester} />
  94. <Drawer.Screen name="Update Grades" component={EditGrades} />
  95. <Drawer.Screen name="Current Courses" component={CurrentCourses} />
  96. <Drawer.Screen name="Set your semester and year" component={UpdateSemYear} />
  97. <Drawer.Screen name="Logout" component={Logout} />
  98. </Drawer.Navigator>
  99. )
  100. }
  101. const styles = StyleSheet.create({
  102. container: {
  103. flex: 1,
  104. backgroundColor: '#fff',
  105. alignItems: 'center',
  106. justifyContent: 'center',
  107. },
  108. });