Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clerk user auth #58

Merged
merged 5 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
615 changes: 588 additions & 27 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dependencies": {
"@chakra-ui/icons": "^2.1.1",
"@chakra-ui/react": "^2.8.2",
"@clerk/nextjs": "^4.29.7",
"@emotion/react": "^11.11.3",
"@emotion/styled": "^11.11.0",
"@fullcalendar/bootstrap5": "^6.1.10",
Expand All @@ -27,6 +28,7 @@
"@heroicons/react": "^2.1.1",
"bootstrap": "^5.3.2",
"bootstrap-icons": "^1.11.3",
"eslint-plugin-prettier": "^5.1.3",
"framer-motion": "^11.0.3",
"mongoose": "^7.6.8",
"next": "14.0.0",
Expand Down
3 changes: 3 additions & 0 deletions src/app/Providers.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
"use client";
import { ClerkProvider } from "@clerk/nextjs";
import StyledComponentsRegistry from "./lib/registry";
import { ChakraProvider } from "@chakra-ui/react";
export default function Providers({ children }: { children: React.ReactNode }) {
return (

<ClerkProvider>
<StyledComponentsRegistry>
<ChakraProvider>
{children}
</ChakraProvider>
</StyledComponentsRegistry>
</ClerkProvider>

)
}
15 changes: 8 additions & 7 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Metadata } from "next";
import { ClerkProvider } from '@clerk/nextjs'
import "./globals.css";
import Providers from "./Providers";

Expand All @@ -14,12 +15,12 @@ export default function RootLayout({
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>
<Providers>
{children}
</Providers>
</body>
</html>
<html lang="en">
<body>
<Providers>
{children}
</Providers>
</body>
</html>
);
}
69 changes: 64 additions & 5 deletions src/app/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,59 @@ import {
} from "@chakra-ui/react";
import NextLink from "next/link";
import { FaEye, FaEyeSlash } from "react-icons/fa";
import { useSignIn } from '@clerk/nextjs';
import { useRouter, useSearchParams} from 'next/navigation';

export default function Login() {
const [showPassword, setShowPassword] = useState(false);
const { isLoaded, signIn, setActive } = useSignIn();
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const router = useRouter();
const searchParams = useSearchParams()
const redirect_url = searchParams.get('redirect_url')

const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();

if (!isLoaded) {
return;
}

if (!email || !password) {
alert('Please fill out all fields.');
return;
}

console.log(email, password)

try {
const completeSignIn = await signIn.create({identifier: email, strategy: "password", password });
if (completeSignIn.status !== 'complete') {
// The status can also be `needs_factor_on', 'needs_factor_two', or 'needs_identifier'
// Please see https://clerk.com/docs/references/react/use-sign-in#result-status for more information
console.log(JSON.stringify(completeSignIn, null, 2));
}

if (completeSignIn.status === 'complete') {
// If complete, user exists and provided password match -- set session active
await setActive({ session: completeSignIn.createdSessionId });

// Redirect the user to a post sign-in route
if (redirect_url){
router.push(redirect_url);
}
else{
// Redirect the user to a post sign-in route
router.push('/');
}
}
} catch (err) {
console.error(JSON.stringify(err, null, 2));
}

//setSubmitted(true);
};

const handleTogglePassword = () => {
setShowPassword((prevShowPassword) => !prevShowPassword);
Expand All @@ -24,21 +74,30 @@ export default function Login() {
<>
<Box p={4} maxWidth="400px" mx="auto">
<Box mb={6}>
<Heading as="h1" fontSize="xl" textAlign="center">
<Heading
as="h1"
fontSize="xl"
textAlign="center">
Log In
</Heading>
</Box>
<FormControl mb={4}>
<FormControl mb={4} isRequired>
<FormLabel>Email</FormLabel>
<Input type="text" placeholder="Email" variant="filled" />
<Input
type="text"
placeholder="Email"
variant="filled"
onChange={(e) => setEmail(e.target.value)}
/>
</FormControl>
<FormControl mb={4}>
<FormControl mb={4} isRequired>
<FormLabel>Password</FormLabel>
<Input
type={showPassword ? "text" : "password"}
placeholder="Password"
variant="filled"
pr="4.5rem"
onChange={(e) => setPassword(e.target.value)}
/>
<Button
position="absolute"
Expand All @@ -56,7 +115,7 @@ export default function Login() {
</ChakraLink>
</FormControl>
<FormControl mb={4}>
<Button bg="#a3caf0" width="full">
<Button bg="#a3caf0" width="full" onClick={handleSubmit}>
Log In
</Button>
</FormControl>
Expand Down
2 changes: 2 additions & 0 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Button } from "@styles/Button";
import CreateEditEvent from "@components/CreateEditEvent";
import NextLink from "next/link";
import YourParentComponent from "@components/YourParentComponent";
import { SignOutButton } from "@clerk/nextjs";

export default function Home() {
return (
Expand All @@ -14,6 +15,7 @@ export default function Home() {
<NextLink href="/login">
<Button>Login</Button>
</NextLink>
< SignOutButton />
<YourParentComponent />
</main>
);
Expand Down
Loading