Untitled

mail@pastecode.io avatar
unknown
javascript
3 years ago
1.8 kB
2
Indexable
Never

import React, { useState } from 'react'
import { useRouter } from 'next/router'
import Firebase_Votimsg from '../../src/services/firebase/firebase_votimsg'
const votimsgFirebase = new Firebase_Votimsg

async function handleVerifyEmail(actionCode) {

  async function run(){
    console.log('dd')
    await votimsgFirebase.currentUser().reload();
    console.log( votimsgFirebase.currentUser() )
    const { uid, displayName, email } = votimsgFirebase.currentUser()
    await votimsgFirebase.storeUserInfos({
      auth_uid: uid,
      display_name: displayName,
      email: email,
      photo_URL: '...defaultpicture.png...'
    })
  }
  await votimsgFirebase.auth().applyActionCode(actionCode)
  await run()
}

async function onEmailVerification({action, mode, oobCode, apiKey}) {
  if (action !== 'action') return
  alert('wait the server verified your email')
  if (process.env.NEXT_PUBLIC_API_KEY_FIREBASE !== apiKey) return
  if (mode !== 'verifyEmail') return
  await handleVerifyEmail(oobCode)
}

function SingIn() {
  const router = useRouter()
  const { action, mode, oobCode, apiKey, continueUrl, lang } = router.query
  console.log(router.query)
  const [email, setEmail] = useState('')
  const [password, setPassword] = useState('')
  async function onSignInHandle(querys) {
    await votimsgFirebase.signInUser('email', { email, password })
    await onEmailVerification(querys)
    router.push('/')
  }

  return (
    <div>
      <input
        placeholder="email"
        onChange={e => setEmail(e.target.value)}
      ></input>
      <input
        placeholder="password"
        onChange={e => setPassword(e.target.value)}
      ></input>
      <button
        onClick={ () => { onSignInHandle(router.query) }}
      >SingIn</button>
    </div>
  )
}
export default SingIn