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

feat: credential auth #87

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
13 changes: 1 addition & 12 deletions apps/nextjs/src/app/workspace/[workspaceId]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
import { redirect } from "next/navigation";

import { auth } from "@acme/auth";

import routes from "~/app/_lib/routes";
import { WeekList } from "./_components/WeekList";
import WorkspaceHeader from "./_components/WorkspaceHeader";
import { WeekendProvider } from "./providers";

export default async function WorkspaceLayout({
export default function WorkspaceLayout({
children,
}: {
children: React.ReactNode;
}) {
const session = await auth();

if (!session) {
redirect(routes.home);
}

return (
<WeekendProvider>
<section>
Expand Down
13 changes: 1 addition & 12 deletions apps/nextjs/src/app/workspaces/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import { redirect } from "next/navigation";

import { auth } from "@acme/auth";

import Heading from "../_components/heading";
import routes from "../_lib/routes";
import WorkspaceModal from "./_components/WorkspaceModal";

export default async function WorkspaceLayout({
export default function WorkspaceLayout({
children,
}: {
children: React.ReactNode;
}) {
const session = await auth();

if (!session) {
redirect(routes.home);
}

return (
<section className="container">
<header className="mb-12 grid grid-cols-5">
Expand Down
48 changes: 48 additions & 0 deletions apps/nextjs/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { NextResponse } from "next/server";

import { auth } from "@acme/auth";

export const AUTH_PAGES = ["/sign-in"];

const isAuthPages = (url: string) =>
AUTH_PAGES.some((page) => page.startsWith(url));

export default auth(async (req) => {
const { url, nextUrl } = req;
// const { value: token } = cookies.get("token") ?? { value: null };

const session = await auth();

// const hasVerifiedToken = token && (await verifyJwtToken(token));
const isAuthPageRequested = isAuthPages(nextUrl.pathname);

if (isAuthPageRequested && !session?.user) {
const response = NextResponse.next();
response.cookies.delete("token");
return response;
}

if (isAuthPageRequested) {
const response = NextResponse.redirect(new URL(`/workspaces`, url));
return response;
}

if (!session?.user) {
const searchParams = new URLSearchParams(nextUrl.searchParams);
searchParams.set("next", nextUrl.pathname);

const response = NextResponse.redirect(new URL(`/?${searchParams}`, url));

response.cookies.delete("token");

return response;
}

return NextResponse.next();
});

// Optionally, don't invoke Middleware on some paths
// Read more: https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher
export const config = {
matcher: ["/workspaces", "/workspace/:path*"],
};
34 changes: 34 additions & 0 deletions packages/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Credentials from "@auth/core/providers/credentials";
import Github from "@auth/core/providers/github";
// import Google from "@auth/core/providers/google";
import type { DefaultSession } from "@auth/core/types";
Expand Down Expand Up @@ -37,6 +38,39 @@ export const {
clientId: env.GITHUB_CLIENT_ID,
clientSecret: env.GITHUB_CLIENT_SECRET,
}),
Credentials({
name: "Credentials",
credentials: {
email: {
label: "Email: ",
type: "text",
},
password: {
label: "Password: ",
type: "password",
},
},
async authorize(credentials) {
// This is where you need to retrieve user data
// to verify with credentials

const user = {
id: "1",
name: "J Smith",
email: "[email protected]",
password: "123",
};

if (
credentials.email === user.email &&
credentials.password === user.password
) {
return user;
}

return null;
},
}),
],
callbacks: {
session: ({ session, user }) => ({
Expand Down
Loading