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.

140 lines
4.6 KiB

5 months ago
  1. import React, { useState } from "react";
  2. import { Button, Text, View } from "react-native";
  3. import ELogo from "@components/ELogo";
  4. import ETextInput from "@components/ETextInput";
  5. import EFullButton, { EIconButton } from "@components/EButton";
  6. import { ColorScheme } from "@components/Design";
  7. import { AppleIcon, GoogleIcon } from "@components/EIcons";
  8. import { ECaptionText, ELabelText } from "@components/EText";
  9. import auth from "@react-native-firebase/auth";
  10. import { useRouter } from "expo-router";
  11. import { GoogleSignin } from "@react-native-google-signin/google-signin";
  12. import { useAuthState } from "@components/AuthProvider";
  13. GoogleSignin.configure({
  14. webClientId:
  15. "406674810515-l8a40f83kl5hnvflvcjrabmjmbvda7am.apps.googleusercontent.com",
  16. });
  17. export default function LoginScreen(props: any) {
  18. const [email, setEmail] = useState<string>("");
  19. const [password, setPassword] = useState<string>("");
  20. const [error, setError] = useState(false);
  21. const router = useRouter();
  22. const [loginAttempting, setLoginAttempting] = useState(false);
  23. const signInWithEmail = async () => {
  24. setLoginAttempting(true);
  25. try {
  26. await auth().signInWithEmailAndPassword(email, password);
  27. router.replace("/");
  28. } catch (e) {
  29. setError(true);
  30. }
  31. setLoginAttempting(false);
  32. };
  33. const signInWithGoogle = async () => {
  34. setLoginAttempting(true);
  35. await GoogleSignin.hasPlayServices({ showPlayServicesUpdateDialog: true });
  36. const { idToken } = await GoogleSignin.signIn();
  37. const googleCredential = auth.GoogleAuthProvider.credential(idToken);
  38. return auth().signInWithCredential(googleCredential);
  39. };
  40. const [user, loginstate, setLoginState] = useAuthState();
  41. return (
  42. <>
  43. <View
  44. style={{
  45. flex: 1,
  46. rowGap: 24,
  47. flexDirection: "column",
  48. paddingTop: 80,
  49. }}
  50. >
  51. <ELogo />
  52. <View style={{ rowGap: 16, flexDirection: "column" }}>
  53. <ETextInput
  54. placeholder={"이메일"}
  55. value={email}
  56. setValue={setEmail}
  57. onChange={() => setError(false)}
  58. />
  59. <ETextInput
  60. placeholder={"비밀번호"}
  61. value={password}
  62. setValue={setPassword}
  63. passwordHidden={true}
  64. onChange={() => setError(false)}
  65. />
  66. {error && (
  67. <ECaptionText error={true}>
  68. </ECaptionText>
  69. )}
  70. </View>
  71. <View style={{ rowGap: 16, flexDirection: "column" }}>
  72. <EFullButton
  73. text={"로그인"}
  74. active={
  75. email.length !== 0 && password.length !== 0 && !loginAttempting
  76. }
  77. onPress={() => signInWithEmail()}
  78. />
  79. <EFullButton
  80. style={{ backgroundColor: ColorScheme.passthrough }}
  81. onPress={() => {
  82. setLoginState &&
  83. setLoginState({ ...loginstate, passwordReset: true });
  84. router.push("auth/passwordresetemail");
  85. }}
  86. active={true}
  87. text={"비밀번호를 모르겠어요"}
  88. />
  89. <View
  90. style={{
  91. flex: 1,
  92. justifyContent: "center",
  93. columnGap: 16,
  94. flexDirection: "row",
  95. }}
  96. >
  97. <EIconButton active={false}>
  98. <AppleIcon fill={ColorScheme.fill[10]} />
  99. </EIconButton>
  100. <EIconButton
  101. active={!loginAttempting}
  102. onPress={() => {
  103. signInWithGoogle()
  104. .then((d) => {
  105. console.log("worked!");
  106. setLoginAttempting(false);
  107. router.replace("");
  108. })
  109. .catch((e) => {
  110. console.error(e);
  111. setLoginAttempting(false);
  112. });
  113. }}
  114. >
  115. <GoogleIcon fill={ColorScheme.fill[10]} />
  116. </EIconButton>
  117. </View>
  118. </View>
  119. </View>
  120. <View style={{ flexDirection: "column", rowGap: 16, marginTop: 200 }}>
  121. <ELabelText style={{ textAlign: "center" }}>
  122. ?
  123. </ELabelText>
  124. <EFullButton
  125. onPress={() => {
  126. setLoginState &&
  127. setLoginState({ ...loginstate, passwordReset: false });
  128. router.push("auth/registeremail");
  129. }}
  130. style={{ backgroundColor: ColorScheme.frostedfill.white[10] }}
  131. active={true}
  132. text={"회원가입"}
  133. />
  134. </View>
  135. </>
  136. );
  137. }