How do I auth user with username or email? #16619
Replies: 2 comments 9 replies
-
There is no username login. Username is not a standard field in Supabase auth. I commented on your discord post with an idea using an rpc call to a postgres function with username and password to get the email and then login. Not the greatest solution, but not sure if anyone has a better way. |
Beta Was this translation helpful? Give feedback.
-
I did it like so:I use React + Vite + TypeScript + tailwind + zustand + supabase async function login(e: React.FormEvent) {
e.preventDefault()
if (emailOrUsername && password) {
const isEmail = emailOrUsername.includes("@")
if (isEmail) {
try {
const response = await supabase.auth.signInWithPassword({ email: emailOrUsername, password: password })
if (response.error) throw response.error
if (response.data.user) {
userStore.authUser(response.data.user.id)
navigate(`/`, { replace: true })
setSuccess("Success!")
}
} catch (error) {
console.log(58, "error - ", error)
setError("Error!")
}
} else {
try {
const { data, error } = await supabase.from("users").select("email").eq("username", emailOrUsername)
if (error) throw error
if (data) {
const response = await supabase.auth.signInWithPassword({ email: data[0].email, password: password })
if (response.error) throw response.error
if (response.data.user) {
userStore.authUser(response.data.user.id)
setSuccess("You logged in!")
} else {
setError("user not found - made a typo?")
}
} else throw "LoginPage.tsx - no data"
} catch (error) {
console.error(78, "login - ", error)
setError("Error")
}
}
}
} ExplanationsI have Input and state for this input Of course when I register new user I insert data in 3 columns const { error } = await supabase
.from("users")
.insert({ id: response.data.user.id, username: usernameValue, email: email })
if (error) throw error Thank you @GaryAustin1 for idea how do I realize it |
Beta Was this translation helpful? Give feedback.
-
I have this form for login
In my form I have field 'email or username' but now I able to login with email - How do I enable login with email or username?
docs link or step by step guide will be preciate
I use - React + Vite + TypeScript + tailwind + zustand + supabase
Here is a bit of code for my login function
Beta Was this translation helpful? Give feedback.
All reactions