Last active
October 29, 2023 09:57
-
-
Save thevahidal/417b33e5b1a736300a42723ef51d3306 to your computer and use it in GitHub Desktop.
LoginForm.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use client"; | |
import React, { useState } from "react"; | |
import { Formik, Field, ErrorMessage, useFormik } from "formik"; | |
import * as Yup from "yup"; | |
import { useRouter } from "next/router"; | |
import { signIn, getCsrfToken } from "next-auth/react"; | |
import Avatar from "@mui/material/Avatar"; | |
import Button from "@mui/material/Button"; | |
import CssBaseline from "@mui/material/CssBaseline"; | |
import TextField from "@mui/material/TextField"; | |
import FormControlLabel from "@mui/material/FormControlLabel"; | |
import Checkbox from "@mui/material/Checkbox"; | |
import Link from "@mui/material/Link"; | |
import Grid from "@mui/material/Grid"; | |
import Box from "@mui/material/Box"; | |
import LockOutlinedIcon from "@mui/icons-material/LockOutlined"; | |
import Typography from "@mui/material/Typography"; | |
import Container from "@mui/material/Container"; | |
import { createTheme, ThemeProvider } from "@mui/material/styles"; | |
export default function LoginForm({csrfToken}) { | |
const csrfToken = await getServerSideCsrfToken(); | |
const [error, setError] = React.useState<string | null>(); | |
const formik = useFormik({ | |
initialValues: { username: "", password: "", tenantKey: "" }, | |
validationSchema: Yup.object({ | |
username: Yup.string() | |
.min(2, "Too Short!") | |
.max(50, "Too Long!") | |
.required("Required"), | |
password: Yup.string().required("Please enter your password"), | |
}), | |
onSubmit: async (values, { setSubmitting }) => { | |
const res = await signIn("credentials", { | |
redirect: false, | |
username: values.username, | |
password: values.password, | |
callbackUrl: `${window.location.origin}`, | |
}); | |
if (res?.error) { | |
setError(res?.error); | |
} else { | |
setError(null); | |
} | |
if (res?.url) router.push(res?.url); | |
setSubmitting(false); | |
}, | |
}); | |
return ( | |
<div> | |
<form onSubmit={formik.handleSubmit}> | |
<input name="csrfToken" type="hidden" defaultValue={csrfToken} /> | |
<TextField | |
fullWidth | |
id="username" | |
name="username" | |
label="Username" | |
value={formik.values.username} | |
onChange={formik.handleChange} | |
onBlur={formik.handleBlur} | |
error={formik.touched.username && Boolean(formik.errors.username)} | |
helperText={formik.touched.username && formik.errors.username} | |
/> | |
<TextField | |
fullWidth | |
id="password" | |
name="password" | |
label="Password" | |
type="password" | |
value={formik.values.password} | |
onChange={formik.handleChange} | |
onBlur={formik.handleBlur} | |
error={formik.touched.password && Boolean(formik.errors.password)} | |
helperText={formik.touched.password && formik.errors.password} | |
/> | |
<Button color="primary" variant="contained" fullWidth type="submit"> | |
Submit | |
</Button> | |
</form> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment