link-stack/apps/link/app/(login)/login/_components/Login.tsx

259 lines
8 KiB
TypeScript
Raw Normal View History

2023-06-26 10:07:12 +00:00
"use client";
import { FC, useState, useEffect } from "react";
2023-08-25 07:11:33 +00:00
import {
Box,
Grid,
Container,
IconButton,
Typography,
TextField,
} from "@mui/material";
import {
Apple as AppleIcon,
Google as GoogleIcon,
2025-01-20 11:25:13 +01:00
Microsoft as MicrosoftIcon,
2023-08-25 07:11:33 +00:00
Key as KeyIcon,
} from "@mui/icons-material";
import { signIn, getProviders } from "next-auth/react";
2023-08-25 07:11:33 +00:00
import Image from "next/image";
import LinkLogo from "public/link-logo-small.png";
2024-06-05 08:52:41 +02:00
import { colors, fonts } from "@link-stack/ui";
2023-08-25 07:11:33 +00:00
import { useSearchParams } from "next/navigation";
2022-12-02 10:55:56 +00:00
2023-05-24 20:27:57 +00:00
type LoginProps = {
session: any;
baseURL: string;
2023-05-24 20:27:57 +00:00
};
export const Login: FC<LoginProps> = ({ session, baseURL }) => {
let origin = null;
if (typeof window !== "undefined") {
origin = window.location.origin;
}
const callbackUrl = `${origin}/link`;
const [provider, setProvider] = useState(undefined);
2023-08-25 07:11:33 +00:00
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const params = useSearchParams();
const error = params.get("error");
const { darkGray, cdrLinkOrange, white } = colors;
2024-05-09 07:42:44 +02:00
const { poppins } = fonts;
2022-12-02 10:55:56 +00:00
const buttonStyles = {
borderRadius: 500,
width: "100%",
fontSize: "16px",
fontWeight: "bold",
2023-08-25 07:11:33 +00:00
backgroundColor: white,
"&:hover": {
color: white,
backgroundColor: cdrLinkOrange,
},
};
const fieldStyles = {
"& label.Mui-focused": {
color: cdrLinkOrange,
},
"& .MuiInput-underline:after": {
borderBottomColor: cdrLinkOrange,
},
"& .MuiFilledInput-underline:after": {
borderBottomColor: cdrLinkOrange,
},
"& .MuiOutlinedInput-root": {
"&.Mui-focused fieldset": {
borderColor: cdrLinkOrange,
},
},
2022-12-02 10:55:56 +00:00
};
useEffect(() => {
const fetchProviders = async () => {
const providers = await getProviders();
setProvider(Object.keys(providers)?.pop());
};
fetchProviders();
}, []);
2022-12-02 10:55:56 +00:00
return (
2023-08-25 07:11:33 +00:00
<Box sx={{ backgroundColor: darkGray, height: "100vh" }}>
<Container maxWidth="md" sx={{ p: 10 }}>
2022-12-02 10:55:56 +00:00
<Grid container spacing={2} direction="column" alignItems="center">
2023-08-25 07:11:33 +00:00
<Grid
item
container
direction="row"
justifyContent="center"
alignItems="center"
>
<Grid item>
<Box
sx={{
width: "70px",
height: "70px",
margin: "0 auto",
}}
>
<Image
src={LinkLogo}
alt="Link logo"
width={70}
height={70}
style={{
objectFit: "cover",
filter: "grayscale(100) brightness(100)",
}}
/>
</Box>
</Grid>
<Grid item>
<Typography
variant="h2"
sx={{
fontSize: 36,
color: "white",
fontWeight: 700,
mt: 1,
ml: 0.5,
2024-05-09 07:42:44 +02:00
fontFamily: poppins.style.fontFamily,
2023-08-25 07:11:33 +00:00
}}
>
CDR Link
</Typography>
</Grid>
2022-12-02 10:55:56 +00:00
</Grid>
2023-08-25 07:11:33 +00:00
<Grid item sx={{ width: "100%" }}>
2022-12-02 10:55:56 +00:00
{!session ? (
2023-08-25 07:11:33 +00:00
<Container
maxWidth="xs"
sx={{
p: 3,
mt: 3,
}}
2022-12-02 10:55:56 +00:00
>
2023-08-25 07:11:33 +00:00
<Grid
container
spacing={3}
direction="column"
alignItems="center"
>
{error ? (
<Grid item sx={{ width: "100%" }}>
<Box sx={{ backgroundColor: "red", p: 3 }}>
<Typography
variant="body1"
sx={{
fontSize: 18,
color: "white",
textAlign: "center",
}}
>
{`${error} error`}
</Typography>
</Box>
</Grid>
) : null}
{provider === "google" && (
<Grid item sx={{ width: "100%" }}>
<IconButton
sx={buttonStyles}
onClick={() =>
signIn("google", {
callbackUrl,
})
}
>
<GoogleIcon sx={{ mr: 1 }} />
Sign in with Google
</IconButton>
</Grid>
)}
{provider === "apple" && (
<Grid item sx={{ width: "100%" }}>
<IconButton
aria-label="Sign in with Apple"
sx={buttonStyles}
onClick={() =>
signIn("apple", {
callbackUrl,
})
}
>
<AppleIcon sx={{ mr: 1 }} />
Sign in with Apple
</IconButton>
</Grid>
)}
2025-01-20 11:25:13 +01:00
{provider === "azure-ad" && (
<Grid item sx={{ width: "100%" }}>
<IconButton
sx={buttonStyles}
onClick={() =>
signIn("azure-ad", {
callbackUrl,
2025-01-20 11:25:13 +01:00
})
}
>
<MicrosoftIcon sx={{ mr: 1 }} />
Sign in with Azure
</IconButton>
</Grid>
)}
{provider === "credentials" && (
<Grid item container spacing={3}>
<Grid item sx={{ width: "100%" }}>
<TextField
value={email}
onChange={(e) => setEmail(e.target.value)}
label="Email"
variant="filled"
size="small"
fullWidth
sx={{ ...fieldStyles, backgroundColor: white }}
/>
</Grid>
<Grid item sx={{ ...fieldStyles, width: "100%" }}>
<TextField
value={password}
onChange={(e) => setPassword(e.target.value)}
label="Password"
variant="filled"
size="small"
fullWidth
sx={{ backgroundColor: white }}
type="password"
/>
</Grid>
<Grid item sx={{ width: "100%" }}>
<IconButton
sx={buttonStyles}
onClick={() => {
signIn("credentials", {
email,
password,
callbackUrl,
});
}}
>
<KeyIcon sx={{ mr: 1 }} />
Sign in with Zammad credentials
</IconButton>
</Grid>
</Grid>
)}
2022-12-02 10:55:56 +00:00
</Grid>
2023-08-25 07:11:33 +00:00
</Container>
2022-12-02 10:55:56 +00:00
) : null}
{session ? (
<Box component="h4">
{` ${session.user.name ?? session.user.email}.`}
</Box>
) : null}
</Grid>
</Grid>
</Container>
2023-08-25 07:11:33 +00:00
</Box>
2022-12-02 10:55:56 +00:00
);
};