WIP changes
This commit is contained in:
@@ -16,7 +16,7 @@ export default async function Index(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!session?.user?.username)
|
||||
return res.status(401).json({ response: "You must be logged in." });
|
||||
else if (session?.user?.isSubscriber === false)
|
||||
res.status(401).json({
|
||||
return res.status(401).json({
|
||||
response:
|
||||
"You are not a subscriber, feel free to reach out to us at support@linkwarden.app in case of any issues.",
|
||||
});
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { prisma } from "@/lib/api/db";
|
||||
import NextAuth from "next-auth/next";
|
||||
import CredentialsProvider from "next-auth/providers/credentials";
|
||||
import { AuthOptions, Session } from "next-auth";
|
||||
import { AuthOptions, Session, User } from "next-auth";
|
||||
import bcrypt from "bcrypt";
|
||||
import EmailProvider from "next-auth/providers/email";
|
||||
import { JWT } from "next-auth/jwt";
|
||||
import { PrismaAdapter } from "@auth/prisma-adapter";
|
||||
import { Adapter } from "next-auth/adapters";
|
||||
import sendVerificationRequest from "@/lib/api/sendVerificationRequest";
|
||||
@@ -19,6 +18,7 @@ const providers: Provider[] = [
|
||||
type: "credentials",
|
||||
credentials: {},
|
||||
async authorize(credentials, req) {
|
||||
console.log("User logged in...");
|
||||
if (!credentials) return null;
|
||||
|
||||
const { username, password } = credentials as {
|
||||
@@ -81,13 +81,6 @@ export const authOptions: AuthOptions = {
|
||||
verifyRequest: "/confirmation",
|
||||
},
|
||||
callbacks: {
|
||||
session: async ({ session, token }: { session: Session; token: JWT }) => {
|
||||
session.user.id = parseInt(token.id as string);
|
||||
session.user.username = token.username as string;
|
||||
session.user.isSubscriber = token.isSubscriber as boolean;
|
||||
|
||||
return session;
|
||||
},
|
||||
async jwt({ token, trigger, user }) {
|
||||
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
|
||||
|
||||
@@ -121,7 +114,7 @@ export const authOptions: AuthOptions = {
|
||||
}
|
||||
|
||||
if (trigger === "signIn") {
|
||||
token.id = user.id;
|
||||
token.id = user.id as number;
|
||||
token.username = (user as any).username;
|
||||
} else if (trigger === "update" && token.id) {
|
||||
console.log(token);
|
||||
@@ -132,7 +125,7 @@ export const authOptions: AuthOptions = {
|
||||
},
|
||||
});
|
||||
|
||||
if (user) {
|
||||
if (user?.name && user.username && user.email) {
|
||||
token.name = user.name;
|
||||
token.username = user.username?.toLowerCase();
|
||||
token.email = user.email?.toLowerCase();
|
||||
@@ -140,6 +133,13 @@ export const authOptions: AuthOptions = {
|
||||
}
|
||||
return token;
|
||||
},
|
||||
async session({ session, token }) {
|
||||
session.user.id = token.id;
|
||||
session.user.username = token.username;
|
||||
session.user.isSubscriber = token.isSubscriber;
|
||||
|
||||
return session;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ export default async function Index(req: NextApiRequest, res: NextApiResponse) {
|
||||
.status(401)
|
||||
.send("You must be logged in.");
|
||||
else if (session?.user?.isSubscriber === false)
|
||||
res.status(401).json({
|
||||
return res.status(401).json({
|
||||
response:
|
||||
"You are not a subscriber, feel free to reach out to us at support@linkwarden.app in case of any issues.",
|
||||
});
|
||||
|
||||
@@ -13,7 +13,7 @@ export default async function collections(
|
||||
if (!session?.user?.id) {
|
||||
return res.status(401).json({ response: "You must be logged in." });
|
||||
} else if (session?.user?.isSubscriber === false)
|
||||
res.status(401).json({
|
||||
return res.status(401).json({
|
||||
response:
|
||||
"You are not a subscriber, feel free to reach out to us at support@linkwarden.app in case of any issues.",
|
||||
});
|
||||
|
||||
@@ -13,7 +13,7 @@ export default async function collections(
|
||||
if (!session?.user?.id) {
|
||||
return res.status(401).json({ response: "You must be logged in." });
|
||||
} else if (session?.user?.isSubscriber === false)
|
||||
res.status(401).json({
|
||||
return res.status(401).json({
|
||||
response:
|
||||
"You are not a subscriber, feel free to reach out to us at support@linkwarden.app in case of any issues.",
|
||||
});
|
||||
|
||||
@@ -10,7 +10,7 @@ export default async function links(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!session?.user?.id) {
|
||||
return res.status(401).json({ response: "You must be logged in." });
|
||||
} else if (session?.user?.isSubscriber === false)
|
||||
res.status(401).json({
|
||||
return res.status(401).json({
|
||||
response:
|
||||
"You are not a subscriber, feel free to reach out to us at support@linkwarden.app in case of any issues.",
|
||||
});
|
||||
|
||||
@@ -1,18 +1,10 @@
|
||||
// For future...
|
||||
// import { getToken } from "next-auth/jwt";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { getToken } from "next-auth/jwt";
|
||||
|
||||
// export default async (req, res) => {
|
||||
// // If you don't have NEXTAUTH_SECRET set, you will have to pass your secret as `secret` to `getToken`
|
||||
// console.log({ req });
|
||||
// const token = await getToken({ req, raw: true });
|
||||
// if (token) {
|
||||
// // Signed in
|
||||
// console.log("JSON Web Token", JSON.stringify(token, null, 2));
|
||||
// } else {
|
||||
// // Not Signed in
|
||||
// res.status(401);
|
||||
// }
|
||||
// res.end();
|
||||
// };
|
||||
|
||||
export {};
|
||||
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
// if using `NEXTAUTH_SECRET` env variable, we detect it, and you won't actually need to `secret`
|
||||
// const token = await getToken({ req })
|
||||
const token = await getToken({ req });
|
||||
console.log("JSON Web Token", token);
|
||||
res.end();
|
||||
};
|
||||
|
||||
@@ -12,7 +12,7 @@ export default async function links(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!session?.user?.id) {
|
||||
return res.status(401).json({ response: "You must be logged in." });
|
||||
} else if (session?.user?.isSubscriber === false)
|
||||
res.status(401).json({
|
||||
return res.status(401).json({
|
||||
response:
|
||||
"You are not a subscriber, feel free to reach out to us at support@linkwarden.app in case of any issues.",
|
||||
});
|
||||
|
||||
@@ -11,7 +11,7 @@ export default async function links(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!session?.user?.id) {
|
||||
return res.status(401).json({ response: "You must be logged in." });
|
||||
} else if (session?.user?.isSubscriber === false)
|
||||
res.status(401).json({
|
||||
return res.status(401).json({
|
||||
response:
|
||||
"You are not a subscriber, feel free to reach out to us at support@linkwarden.app in case of any issues.",
|
||||
});
|
||||
|
||||
@@ -4,14 +4,19 @@ import { authOptions } from "@/pages/api/v1/auth/[...nextauth]";
|
||||
import getLinks from "@/lib/api/controllers/links/getLinks";
|
||||
import postLink from "@/lib/api/controllers/links/postLink";
|
||||
import { LinkRequestQuery } from "@/types/global";
|
||||
import { getToken } from "next-auth/jwt";
|
||||
|
||||
export default async function links(req: NextApiRequest, res: NextApiResponse) {
|
||||
const session = await getServerSession(req, res, authOptions);
|
||||
const token = await getToken({ req });
|
||||
|
||||
// const session = await getServerSession(req, res, authOptions);
|
||||
|
||||
return res.status(200).json(token);
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return res.status(401).json({ response: "You must be logged in." });
|
||||
} else if (session?.user?.isSubscriber === false)
|
||||
res.status(401).json({
|
||||
return res.status(401).json({
|
||||
response:
|
||||
"You are not a subscriber, feel free to reach out to us at support@linkwarden.app in case of any issues.",
|
||||
});
|
||||
|
||||
@@ -20,7 +20,7 @@ export default async function users(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!session?.user.id) {
|
||||
return res.status(401).json({ response: "You must be logged in." });
|
||||
} else if (session?.user?.isSubscriber === false)
|
||||
res.status(401).json({
|
||||
return res.status(401).json({
|
||||
response:
|
||||
"You are not a subscriber, feel free to reach out to us at support@linkwarden.app in case of any issues.",
|
||||
});
|
||||
|
||||
@@ -9,7 +9,7 @@ export default async function tags(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!session?.user?.username) {
|
||||
return res.status(401).json({ response: "You must be logged in." });
|
||||
} else if (session?.user?.isSubscriber === false)
|
||||
res.status(401).json({
|
||||
return res.status(401).json({
|
||||
response:
|
||||
"You are not a subscriber, feel free to reach out to us at support@linkwarden.app in case of any issues.",
|
||||
});
|
||||
|
||||
@@ -9,7 +9,7 @@ export default async function tags(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!session?.user?.username) {
|
||||
return res.status(401).json({ response: "You must be logged in." });
|
||||
} else if (session?.user?.isSubscriber === false)
|
||||
res.status(401).json({
|
||||
return res.status(401).json({
|
||||
response:
|
||||
"You are not a subscriber, feel free to reach out to us at support@linkwarden.app in case of any issues.",
|
||||
});
|
||||
|
||||
@@ -26,7 +26,7 @@ export default async function users(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!userId) {
|
||||
return res.status(401).json({ response: "You must be logged in." });
|
||||
} else if (session?.user?.isSubscriber === false)
|
||||
res.status(401).json({
|
||||
return res.status(401).json({
|
||||
response:
|
||||
"You are not a subscriber, feel free to reach out to us at support@linkwarden.app in case of any issues.",
|
||||
});
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
// TODO - Stripe webhooks for user cancellation...
|
||||
|
||||
// import { NextApiRequest, NextApiResponse } from "next";
|
||||
// import Stripe from "stripe";
|
||||
// import { buffer } from "micro";
|
||||
// import { prisma } from "@/lib/api/db";
|
||||
|
||||
// const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string, {
|
||||
// apiVersion: "2022-11-15",
|
||||
// });
|
||||
|
||||
// const endpointSecret =
|
||||
// "whsec_7c144bcd924041257e3d83eac1e2fba9c8a938b240fd8adb1c902f079e0cdee0";
|
||||
|
||||
// export const config = {
|
||||
// api: {
|
||||
// bodyParser: false,
|
||||
// },
|
||||
// };
|
||||
|
||||
// export default async function handler(
|
||||
// req: NextApiRequest,
|
||||
// res: NextApiResponse
|
||||
// ) {
|
||||
// if (req.method === "POST") {
|
||||
// const buf = await buffer(req);
|
||||
// const sig = req.headers["stripe-signature"];
|
||||
|
||||
// let event: Stripe.Event;
|
||||
|
||||
// try {
|
||||
// if (!sig) throw new Error("Stripe Signature is not defined.");
|
||||
// event = stripe.webhooks.constructEvent(buf, sig, endpointSecret);
|
||||
// } catch (err) {
|
||||
// console.log(err);
|
||||
// return res.status(400).send({ response: "Error..." });
|
||||
// }
|
||||
|
||||
// // Handle the event
|
||||
// switch (event.type) {
|
||||
// case "customer.subscription.deleted":
|
||||
// const customerSubscriptionDeleted = event.data.object as any;
|
||||
|
||||
// // Revoke all the token under the customers email...
|
||||
|
||||
// const customer = (await stripe.customers.retrieve(
|
||||
// customerSubscriptionDeleted.customer
|
||||
// )) as any;
|
||||
|
||||
// if (customer?.email) {
|
||||
// // Revoke tokens inside the database
|
||||
// }
|
||||
|
||||
// break;
|
||||
// // ... handle other event types
|
||||
// default:
|
||||
// console.log(`Unhandled event type ${event.type}`);
|
||||
// }
|
||||
|
||||
// return res.status(200).send({ response: "Done!" });
|
||||
// }
|
||||
// }
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
import SubmitButton from "@/components/SubmitButton";
|
||||
import TextInput from "@/components/TextInput";
|
||||
import CenteredForm from "@/layouts/CenteredForm";
|
||||
import { signIn } from "next-auth/react";
|
||||
import Link from "next/link";
|
||||
import { useState, FormEvent } from "react";
|
||||
import { toast } from "react-hot-toast";
|
||||
|
||||
interface FormData {
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
const emailEnabled = process.env.NEXT_PUBLIC_EMAIL_PROVIDER;
|
||||
|
||||
export default function Login() {
|
||||
const [submitLoader, setSubmitLoader] = useState(false);
|
||||
|
||||
const [form, setForm] = useState<FormData>({
|
||||
username: "",
|
||||
password: "",
|
||||
});
|
||||
|
||||
async function loginUser(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
|
||||
if (form.username !== "" && form.password !== "") {
|
||||
setSubmitLoader(true);
|
||||
|
||||
const load = toast.loading("Authenticating...");
|
||||
|
||||
const res = await signIn("credentials", {
|
||||
username: form.username,
|
||||
password: form.password,
|
||||
redirect: false,
|
||||
});
|
||||
|
||||
console.log(res);
|
||||
|
||||
toast.dismiss(load);
|
||||
|
||||
setSubmitLoader(false);
|
||||
|
||||
if (!res?.ok) {
|
||||
toast.error("Invalid login.");
|
||||
}
|
||||
} else {
|
||||
toast.error("Please fill out all the fields.");
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<CenteredForm text="Sign in to your account">
|
||||
<form onSubmit={loginUser}>
|
||||
<div className="p-4 mx-auto flex flex-col gap-3 justify-between max-w-[30rem] min-w-80 w-full bg-slate-50 dark:bg-neutral-800 rounded-2xl shadow-md border border-sky-100 dark:border-neutral-700">
|
||||
<p className="text-3xl text-black dark:text-white text-center font-extralight">
|
||||
Enter your credentials
|
||||
</p>
|
||||
|
||||
<hr className="border-1 border-sky-100 dark:border-neutral-700" />
|
||||
|
||||
<div>
|
||||
<p className="text-sm text-black dark:text-white w-fit font-semibold mb-1">
|
||||
Username
|
||||
{emailEnabled ? " or Email" : undefined}
|
||||
</p>
|
||||
|
||||
<TextInput
|
||||
autoFocus={true}
|
||||
placeholder="johnny"
|
||||
value={form.username}
|
||||
className="bg-white"
|
||||
onChange={(e) => setForm({ ...form, username: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="w-full">
|
||||
<p className="text-sm text-black dark:text-white w-fit font-semibold mb-1">
|
||||
Password
|
||||
</p>
|
||||
|
||||
<TextInput
|
||||
type="password"
|
||||
placeholder="••••••••••••••"
|
||||
value={form.password}
|
||||
className="bg-white"
|
||||
onChange={(e) => setForm({ ...form, password: e.target.value })}
|
||||
/>
|
||||
{emailEnabled && (
|
||||
<div className="w-fit ml-auto mt-1">
|
||||
<Link
|
||||
href={"/forgot"}
|
||||
className="text-gray-500 dark:text-gray-400 font-semibold"
|
||||
>
|
||||
Forgot Password?
|
||||
</Link>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<SubmitButton
|
||||
type="submit"
|
||||
label="Login"
|
||||
className=" w-full text-center"
|
||||
loading={submitLoader}
|
||||
/>
|
||||
{process.env.NEXT_PUBLIC_DISABLE_REGISTRATION ===
|
||||
"true" ? undefined : (
|
||||
<div className="flex items-baseline gap-1 justify-center">
|
||||
<p className="w-fit text-gray-500 dark:text-gray-400">
|
||||
New here?
|
||||
</p>
|
||||
<Link
|
||||
href={"/register"}
|
||||
className="block text-black dark:text-white font-semibold"
|
||||
>
|
||||
Sign Up
|
||||
</Link>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</form>
|
||||
</CenteredForm>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user