Next.js Discord

Discord Forum

Login state in server component

Answered
In&Out posted this in #help-forum
Open in Discord
Avatar
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;
View full answer

4 Replies

Avatar
Code I use now
Avatar
"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
Avatar
Fila Brasileiro
Does it have to be pretty? couldn't you just set the input type to email? :D
Avatar
I am big dummy, I forgot I can use formData to get email, and it works as I wanted to, thanks