first commit
This commit is contained in:
47
auth/components/signin/action.ts
Normal file
47
auth/components/signin/action.ts
Normal file
@ -0,0 +1,47 @@
|
||||
"use server";
|
||||
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import { Argon2id } from "oslo/password";
|
||||
import { lucia } from "@/lib/lucia";
|
||||
import { lachesis } from "@/lib/prisma";
|
||||
|
||||
const signIn = async (formData: FormData) => {
|
||||
const formDataRaw = {
|
||||
username: formData.get("username") as string,
|
||||
password: formData.get("password") as string,
|
||||
};
|
||||
|
||||
try {
|
||||
const user = await lachesis.user.findUnique({
|
||||
where: { username: formDataRaw.username },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return { error: "Incorrect username" };
|
||||
}
|
||||
|
||||
const validPassword = await new Argon2id().verify(
|
||||
user.hashedPassword,
|
||||
formDataRaw.password
|
||||
);
|
||||
|
||||
if (!validPassword) {
|
||||
return { error: "Incorrect password" };
|
||||
}
|
||||
|
||||
const session = await lucia.createSession(user.id, {});
|
||||
const sessionCookie = lucia.createSessionCookie(session.id);
|
||||
|
||||
cookies().set(
|
||||
sessionCookie.name,
|
||||
sessionCookie.value,
|
||||
sessionCookie.attributes
|
||||
);
|
||||
} catch (error: any) {
|
||||
return { error: "Sign-in failed: " + error.message };
|
||||
}
|
||||
redirect("/home");
|
||||
};
|
||||
|
||||
export { signIn };
|
79
auth/components/signin/signin.tsx
Normal file
79
auth/components/signin/signin.tsx
Normal file
@ -0,0 +1,79 @@
|
||||
"use client";
|
||||
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import Link from "next/link";
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from "@/components/ui/card";
|
||||
import { Label } from "@/components/ui/label";
|
||||
|
||||
import { signIn } from "./action";
|
||||
import { toast } from "@/components/ui/use-toast";
|
||||
|
||||
export default function SignInForm() {
|
||||
const submit = async (data: FormData) => {
|
||||
const { error } = await signIn(data);
|
||||
if (error) {
|
||||
toast({
|
||||
title: "Error",
|
||||
description: error,
|
||||
});
|
||||
} else {
|
||||
toast({
|
||||
title: "Success",
|
||||
description: "Account created successfully",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Card className="mx-auto max-w-sm">
|
||||
<CardHeader>
|
||||
<CardTitle className="text-2xl">Sign in</CardTitle>
|
||||
<CardDescription>
|
||||
Enter your info below to login your account
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<form action={submit} className="grid gap-4">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="username">Username</Label>
|
||||
<Input name="username" type="text" placeholder="clotho" required />
|
||||
</div>
|
||||
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="password">Password</Label>
|
||||
<Input
|
||||
name="password"
|
||||
type="password"
|
||||
placeholder="********"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<Link
|
||||
href="/forgot-password"
|
||||
className="ml-auto inline-block text-sm underline"
|
||||
>
|
||||
Forgot your password?
|
||||
</Link>
|
||||
|
||||
<Button type="submit" className="w-full">
|
||||
Sign in
|
||||
</Button>
|
||||
</form>
|
||||
<div className="mt-4 text-center text-sm">
|
||||
Need an account?{" "}
|
||||
<Link href="/signup" className="underline">
|
||||
Sign up
|
||||
</Link>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
export { SignInForm };
|
26
auth/components/signout.ts
Normal file
26
auth/components/signout.ts
Normal file
@ -0,0 +1,26 @@
|
||||
'use server';
|
||||
|
||||
import { cookies } from 'next/headers';
|
||||
import { redirect } from 'next/navigation';
|
||||
import { lucia } from '@/lib/lucia';
|
||||
import { getAuth } from '../queries/getauth';
|
||||
|
||||
export const signOut = async (_formData: FormData) => {
|
||||
const { session } = await getAuth();
|
||||
|
||||
if (!session) {
|
||||
redirect('/');
|
||||
}
|
||||
|
||||
await lucia.invalidateSession(session.id);
|
||||
|
||||
const sessionCookie = lucia.createBlankSessionCookie();
|
||||
|
||||
cookies().set(
|
||||
sessionCookie.name,
|
||||
sessionCookie.value,
|
||||
sessionCookie.attributes
|
||||
);
|
||||
|
||||
redirect('/');
|
||||
};
|
99
auth/components/signup/action.ts
Normal file
99
auth/components/signup/action.ts
Normal file
@ -0,0 +1,99 @@
|
||||
"use server";
|
||||
|
||||
import { generateId } from "lucia";
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import { Argon2id } from "oslo/password";
|
||||
import { lucia } from "@/lib/lucia";
|
||||
import { lachesis, artemis } from "@/lib/prisma";
|
||||
|
||||
const signUp = async (formData: FormData) => {
|
||||
const formDataRaw = {
|
||||
username: formData.get("username") as string,
|
||||
email: formData.get("email") as string,
|
||||
accessCode: formData.get("accessCode") as string,
|
||||
password: formData.get("password") as string,
|
||||
confirmPassword: formData.get("confirmPassword") as string,
|
||||
};
|
||||
|
||||
if (formDataRaw.password !== formDataRaw.confirmPassword) {
|
||||
return { error: "Passwords do not match" };
|
||||
}
|
||||
|
||||
try {
|
||||
// Check if access code is already used in lachesis database
|
||||
const existingUser = await lachesis.user.findFirst({
|
||||
where: {
|
||||
accessCode: formDataRaw.accessCode,
|
||||
},
|
||||
});
|
||||
|
||||
if (existingUser) {
|
||||
return { error: "Access Code already in use" };
|
||||
}
|
||||
|
||||
// Check if username is already used in lachesis database
|
||||
const existingUsername = await lachesis.user.findFirst({
|
||||
where: {
|
||||
username: formDataRaw.username,
|
||||
},
|
||||
});
|
||||
|
||||
if (existingUsername) {
|
||||
return { error: "Username is currently taken" };
|
||||
}
|
||||
|
||||
const existingEmail = await lachesis.user.findFirst({
|
||||
where: {
|
||||
email: formDataRaw.email,
|
||||
},
|
||||
});
|
||||
|
||||
if (existingEmail) {
|
||||
return { error: "Email is already in use" };
|
||||
}
|
||||
|
||||
// Check if access code exists in artemis database
|
||||
const existingAccessCode = await artemis.aime_card.findFirst({
|
||||
where: {
|
||||
access_code: formDataRaw.accessCode,
|
||||
},
|
||||
});
|
||||
|
||||
if (!existingAccessCode) {
|
||||
return { error: "Not in artemis's database, Nice try ^_^" };
|
||||
}
|
||||
|
||||
const hashedPassword = await new Argon2id().hash(formDataRaw.password);
|
||||
const userId = generateId(15);
|
||||
|
||||
// Create user in the lachesis database
|
||||
await lachesis.user.create({
|
||||
data: {
|
||||
id: userId,
|
||||
username: formDataRaw.username,
|
||||
email: formDataRaw.email,
|
||||
accessCode: formDataRaw.accessCode,
|
||||
hashedPassword,
|
||||
},
|
||||
});
|
||||
|
||||
// Create session and set cookie
|
||||
const session = await lucia.createSession(userId, {});
|
||||
const sessionCookie = lucia.createSessionCookie(session.id);
|
||||
|
||||
cookies().set(
|
||||
sessionCookie.name,
|
||||
sessionCookie.value,
|
||||
sessionCookie.attributes
|
||||
);
|
||||
console.log("Account created");
|
||||
|
||||
// Redirect to home page
|
||||
redirect("/home");
|
||||
} catch (error: any) {
|
||||
return { error: "Account creation failed: " + error.message };
|
||||
}
|
||||
};
|
||||
|
||||
export { signUp };
|
107
auth/components/signup/signup.tsx
Normal file
107
auth/components/signup/signup.tsx
Normal file
@ -0,0 +1,107 @@
|
||||
"use client";
|
||||
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import Link from "next/link";
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from "@/components/ui/card";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { toast, useToast } from "@/components/ui/use-toast";
|
||||
|
||||
import { signUp } from "./action";
|
||||
import { useState } from "react";
|
||||
|
||||
export default function SignUpForm() {
|
||||
const [error, setError] = useState<string>("");
|
||||
|
||||
const submit = async (data: FormData) => {
|
||||
const { error } = await signUp(data);
|
||||
if (error) {
|
||||
toast({
|
||||
title: "Error",
|
||||
description: error,
|
||||
});
|
||||
} else {
|
||||
toast({
|
||||
title: "Success",
|
||||
description: "Account created successfully",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Card className="mx-auto max-w-sm">
|
||||
<CardHeader>
|
||||
<CardTitle className="text-2xl">Sign up</CardTitle>
|
||||
<CardDescription>
|
||||
Enter your info below to create your account
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<form action={submit} className="grid gap-4">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="username">Username</Label>
|
||||
<Input name="username" type="text" placeholder="clotho" required />
|
||||
</div>
|
||||
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="email">Email</Label>
|
||||
<Input
|
||||
name="email"
|
||||
type="email"
|
||||
placeholder="clotho@fates.com"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="accessCode">Access Code</Label>
|
||||
<Input
|
||||
name="accessCode"
|
||||
type="text"
|
||||
placeholder="*******************"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="password">Password</Label>
|
||||
<Input
|
||||
name="password"
|
||||
type="password"
|
||||
placeholder="********"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="confirmPassword">Confirm Password</Label>
|
||||
<Input
|
||||
name="confirmPassword"
|
||||
type="password"
|
||||
placeholder="********"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Button type="submit" className="w-full">
|
||||
Sign up
|
||||
</Button>
|
||||
</form>
|
||||
<div className="mt-4 text-center text-sm">
|
||||
Already have an account?{" "}
|
||||
<Link href="/" className="underline">
|
||||
Sign in
|
||||
</Link>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
export { SignUpForm };
|
Reference in New Issue
Block a user