firebase 이메일 인증 여부 확인 방법

Hong·2022년 3월 21일
0

이메일 인증이 필요한 App을 만들다가 이메일 인증이 되지 않았는데 로그인이 되길래 인증 후 로그인이 되게 하는 방법을 찾아보았다.

결국 찾은 방법은 auth().currentUser.emailVerified 이부분을 추가 해주는 것이다. auth().currentUser.emailVerified은 true/false의 Boolean 값으로 되어있는데 이메일 인증이 되지 않은 상태이면 false, 되었다면 true로 상태가 변경된다. 이 값으로 구분하여 이메일이 유효한지 인증을 거치면 되겠다.

<App.js>

import auth from '@react-native-firebase/auth'

...

useEffect(() => {
    auth().onAuthStateChanged((user) => {
      if (user && auth().currentUser.emailVerified) {
        console.log('App 인증:' + auth().currentUser.emailVerified)
        setIsLoggedIn(true)
      } else {
        setIsLoggedIn(false)
      }
    })
  }, [])

<Login.js>

import auth from '@react-native-firebase/auth'

...

const onSubmitPasswordEditing = async () => {
    if (email === '' || password === '') {
      return Alert.alert('빈 항목이 있습니다.')
    }
    if (loading) {
      return
    }
    setLoading(true)
    try {
      await auth()
        .signInWithEmailAndPassword(email, password)
        .then((user) => {
          if (user.user.emailVerified) {
            console.log(
              '유저 로그인 성공, 인증 여부:' + user.user.emailVerified,
            )
            Alert.alert('로그인에 성공했습니다.')
            setLoading(false)
          } else {
            Alert.alert('이메일 인증이 필요합니다.')
            setLoading(false)
          }
        })
    } catch (e) {
      console.log(e)
      Alert.alert('아이디 및 비밀번호가 일치하지 않습니다.')
      setLoading(false)
    }
  }
profile
코딩 배우기

0개의 댓글