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

Husky Login Requirement #115

Merged
merged 10 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
10 changes: 10 additions & 0 deletions apps/auth-proxy/routes/r/[...auth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,15 @@ export default eventHandler(async (event) =>
clientSecret: process.env.AUTH_GOOGLE_SECRET,
}),
],
callbacks: {
signIn({ user }) {
const email = user.email;

if (!email?.endsWith("@husky.neu.edu")) {
return false;
}
return true;
},
},
}),
);
4 changes: 3 additions & 1 deletion apps/docs/docs/03-backend/auth.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ export async function AuthShowcase() {
The `auth()` function can only be run on the server side so we currently fetch the session information on the server side and pass it to the client side. In the above example, we make use of form actions to trigger the sign-in and sign-out functions.
:::

One thing that we can hope for is that noone will ever have to work on this code since authentication is always a pain.
:::note
We require users to sign in with their @husky.neu.edu email addresses, and if they try to sign in with something else, they are prompted to sign in again.
:::

### `Session` Type

Expand Down
17 changes: 17 additions & 0 deletions apps/web/src/app/(pages)/(dashboard)/redirection/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react";

export default function ErrorPage() {
return (
<div className="flex min-h-screen items-center justify-center">
<div className="text-center">
<h1 className="mb-4 text-2xl font-bold text-gray-900">
Authentication Error
</h1>
<p className="mb-4 text-gray-600">You must log in with husky.neu.edu</p>
<p className="mb-4 text-gray-600">
Click the sign in button to try again
</p>
</div>
</div>
);
}
2 changes: 1 addition & 1 deletion apps/web/src/app/_components/auth/login-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function LoginButton() {
className="rounded-xl border-cooper-blue-400 bg-cooper-blue-400 px-5 py-2.5 text-sm font-semibold text-white hover:border-cooper-blue-200 hover:bg-cooper-blue-200 hover:text-cooper-blue-600 focus:outline-none focus:ring-4 focus:ring-white"
formAction={async () => {
"use server";
await signIn("google");
await signIn("google", { redirectTo: "/" });
}}
>
Sign In
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/app/_components/auth/logout-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function LogoutButton() {
className="rounded-xl border-cooper-blue-400 bg-cooper-blue-400 px-5 py-2.5 text-sm font-semibold text-white hover:border-cooper-blue-200 hover:bg-cooper-blue-200 hover:text-cooper-blue-600 focus:outline-none focus:ring-4 focus:ring-white"
formAction={async () => {
"use server";
await signOut();
await signOut({ redirectTo: "/" });
}}
>
Sign Out
Expand Down
8 changes: 8 additions & 0 deletions packages/auth/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ export const authConfig = {
},
};
},
signIn({ user }) {
const email = user.email;

if (!email?.endsWith("@husky.neu.edu")) {
return "/redirection";
}
return true;
},
Comment on lines +63 to +70
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CLEAN 💯

},
} satisfies NextAuthConfig;

Expand Down
3 changes: 2 additions & 1 deletion packages/auth/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"extends": "@cooper/tsconfig/base.json",
"compilerOptions": {
"jsx": "react",
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
},
"include": ["src", "*.ts"],
"include": ["src", "*.ts", "../../apps/web/error-page.tsx"],
"exclude": ["node_modules"]
}