Login state in server component
Answered
In&Out posted this in #help-forum
In&OutOP
So I have a login file, i want to make some error checking, if for example there isn't @ symbol in input box, it throws error and if there is no error, a message pops up but can't figure out how to do that with a server file
Answered by In&Out
"use client";
import styles from "./css/inputs.module.css";
import React, { useState } from "react";
import { signIn, useOTP } from "@/app/login/actions";
function Inputs() {
const [data, setData] = useState<{ email: string }>({
email: "",
});
const [error, setError] = useState(false);
const [clicked, setClicked] = useState(false);
const [leingt, setLeingt] = useState<boolean>();
function handleSubmit() {
if (!data.email.includes("@")) {
setError(true);
} else {
setError(false);
}
setClicked(true);
}
return (
<div className={styles.container} style={{ marginTop: "-20px" }}>
<form>
<div className={styles.container_input}>
<label
className={`${styles.label} ${leingt ? styles.focus : ""}`}
htmlFor="email"
>
Email
</label>
<input
id="email"
name="email"
onChange={(e) => {
e.target.value.length <= 0 ? setLeingt(false) : setLeingt(true);
setData({ email: e.target.value });
}}
className={styles.input}
type="email"
required
/>
</div>
<div
className={styles.Verify}
style={{ color: !error ? "#8d93a0" : "red" }}
>
{clicked && !error
? `Code sent! Copy the code and paste here`
: clicked && error
? "Email must include @"
: ""}
</div>
<h3>
By continuing, you accept
<span> Terms & Conditions </span> and
<span> Privacy Policy</span>
</h3>
<button
type="submit"
onClick={() => handleSubmit()}
formAction={signIn}
>
Get Sign-in Link
</button>
</form>
</div>
);
}
export default Inputs;
4 Replies
In&OutOP
Code I use now
In&OutOP
"use client";
import styles from "./css/inputs.module.css";
import React, { useState } from "react";
import { signIn, useOTP } from "@/app/login/actions";
function Inputs() {
const [data, setData] = useState<{ email: string }>({
email: "",
});
const [error, setError] = useState(false);
const [clicked, setClicked] = useState(false);
const [leingt, setLeingt] = useState<boolean>();
function handleSubmit() {
if (!data.email.includes("@")) {
setError(true);
} else {
setError(false);
}
setClicked(true);
}
return (
<div className={styles.container} style={{ marginTop: "-20px" }}>
<form>
<div className={styles.container_input}>
<label
className={`${styles.label} ${leingt ? styles.focus : ""}`}
htmlFor="email"
>
Email
</label>
<input
id="email"
name="email"
onChange={(e) => {
e.target.value.length <= 0 ? setLeingt(false) : setLeingt(true);
setData({ email: e.target.value });
}}
className={styles.input}
type="email"
required
/>
</div>
<div
className={styles.Verify}
style={{ color: !error ? "#8d93a0" : "red" }}
>
{clicked && !error
? `Code sent! Copy the code and paste here`
: clicked && error
? "Email must include @"
: ""}
</div>
<h3>
By continuing, you accept
<span> Terms & Conditions </span> and
<span> Privacy Policy</span>
</h3>
<button
type="submit"
onClick={() => handleSubmit()}
formAction={signIn}
>
Get Sign-in Link
</button>
</form>
</div>
);
}
export default Inputs;
Answer
Fila Brasileiro
Does it have to be pretty? couldn't you just set the input type to email? :D
In&OutOP
I am big dummy, I forgot I can use formData to get email, and it works as I wanted to, thanks