You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AuthContext.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import AsyncStorage from "@react-native-async-storage/async-storage";
  2. import * as Google from "expo-auth-session/providers/google";
  3. import React, { createContext, useEffect, useState } from "react";
  4. import { googleApi, loginApi, registerApi } from "../service/user";
  5. import {revokeAsync} from 'expo-auth-session'
  6. export const AuthContext = createContext();
  7. export const AuthProvider = ({ children }) => {
  8. const [userInfo, setUserInfo] = useState({});
  9. const [isLoading, setIsLoading] = useState(false);
  10. // Google Auth
  11. const [request, response, promptAsync] = Google.useAuthRequest({
  12. androidClientId:
  13. "1032296921439-dpgvgkss3ggds0egvo6puf0r7un9em6c.apps.googleusercontent.com",
  14. iosClientId:
  15. "1032296921439-j7jfahhm7q3l07aj04qvdblmcns77oul.apps.googleusercontent.com",
  16. expoClientId:
  17. "1032296921439-vfs9k28sn7kei4nft998ck3067m8ksiq.apps.googleusercontent.com",
  18. });
  19. useEffect(() => {
  20. if (response?.type === "success") {
  21. googleApi(response.authentication.accessToken)
  22. .then((res) => {
  23. if (res.user) {
  24. setUserInfo(res);
  25. AsyncStorage.setItem("userInfo", JSON.stringify(res));
  26. setIsLoading(false);
  27. } else {
  28. callback(res);
  29. setIsLoading(false);
  30. }
  31. })
  32. .catch((e) => {
  33. console.log("error", e);
  34. setIsLoading(false);
  35. });
  36. }
  37. }, [response]);
  38. const login = (email, password, callback) => {
  39. setIsLoading(true);
  40. loginApi({ identifier: email, password })
  41. .then((res) => {
  42. if (res.user) {
  43. setUserInfo(res);
  44. AsyncStorage.setItem("userInfo", JSON.stringify(res));
  45. setIsLoading(false);
  46. } else {
  47. callback(res);
  48. setIsLoading(false);
  49. }
  50. })
  51. .catch((e) => {
  52. console.log("error", e);
  53. setIsLoading(false);
  54. });
  55. };
  56. const googleAuth = () => {
  57. promptAsync({ useProxy: true, showInRecents: true });
  58. };
  59. const register = (username, email, password, callback) => {
  60. setIsLoading(true);
  61. registerApi({ username, email, password })
  62. .then((res) => {
  63. if (res.user) {
  64. setUserInfo(res);
  65. AsyncStorage.setItem("userInfo", JSON.stringify(res));
  66. setIsLoading(false);
  67. } else {
  68. callback(res);
  69. setIsLoading(false);
  70. }
  71. })
  72. .catch((e) => {
  73. console.log("error", e);
  74. setIsLoading(false);
  75. });
  76. };
  77. const logout = async () => {
  78. setIsLoading(true);
  79. AsyncStorage.removeItem("userInfo");
  80. setUserInfo({});
  81. setIsLoading(false);
  82. if (userInfo.user.provider === 'google') {
  83. await revokeAsync({token: response.authentication.accessToken}, {revocationEndpoint: 'https://oauth2.googleapis.com/revoke'})
  84. }
  85. };
  86. const isLoggedIn = async () => {
  87. try {
  88. let userInfo = await AsyncStorage.getItem("userInfo");
  89. userInfo = JSON.parse(userInfo);
  90. if (userInfo) {
  91. setUserInfo(userInfo);
  92. }
  93. } catch (e) {
  94. console.log(e);
  95. }
  96. };
  97. useEffect(() => {
  98. isLoggedIn();
  99. }, []);
  100. return (
  101. <AuthContext.Provider
  102. value={{ isLoading, login, logout, userInfo, register, googleAuth }}
  103. >
  104. {children}
  105. </AuthContext.Provider>
  106. );
  107. };