0

I have a signup composable and a signup template set up. The authentication works but I have not been able to set up firebase email verification despite trying to add await res.user.sendEmailVerification();

How can I set it up so that it sends an email given my signup composable and the email reroutes the user to the login page given by http://localhost:8080/login. I also want my login page to authenticate the user only after they have verified their email so I am also attaching the login composable. Please help I am new to Vue.

Signup Composable

import { ref } from 'vue'
import { projectAuth } from '../firebase/config'

const error = ref(null)
const isPending = ref(false)


const signup = async (email, password, displayName) => {
  error.value = null
  isPending.value = true
  try {
    const res = await projectAuth.createUserWithEmailAndPassword(email, password)
    if (!res) {
      throw new Error('Could not complete signup')
    }
    await res.user.updateProfile({ displayName })
    await res.user.sendEmailVerification();
    error.value = null
    isPending.value = false
    return res
  }
  catch(err) {
    console.log(err.message)
    error.value = err.message
    isPending.value = false
  }
}

const useSignup = () => {
  return { error, signup }
}

export default useSignup

Login Composable:

import { ref } from 'vue'
import { projectAuth } from '../firebase/config'

const error = ref(null)
const isPending = ref(false)

const login = async (email, password) => {
  error.value = null
  isPending.value = true

  try {
    const res = await projectAuth.signInWithEmailAndPassword(email, password)
    error.value = null
    isPending.value = false
    return res
  }
  catch(err) {
    console.log(err.message)
    error.value = 'Incorrect login credentials'
    isPending.value = false
  }
}

const useLogin = () => {
  return { error, login ,isPending}
}

export default useLogin
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
Bilal Hussain
  • 85
  • 2
  • 9
  • 1
    You can manually redirect user to login page using router. You cannot prevent users from logging in even if their email is not verified. Read move in the answer linked above. – Dharmaraj Sep 27 '21 at 11:38
  • But how do I set up email verification in the first place? Can you code it in my signup composable? – Bilal Hussain Sep 27 '21 at 11:53
  • The `sendEmailVerification()` isn't sending the email ? – Dharmaraj Sep 27 '21 at 12:01
  • I was mistaken, the email is being sent. But what is the point of email verification if login is not dependent on it? – Bilal Hussain Sep 27 '21 at 13:21
  • Refer to the answer linked above. You can also check is email is verified or not in security rules and prevent the access unless users verify email. – Dharmaraj Sep 27 '21 at 13:22

0 Answers0