Skip to content

Commit

Permalink
Husky Login Requirement (#115)
Browse files Browse the repository at this point in the history
* tried to add check

* added redirection page

* fixed linting

* fixed linting

* fixed linting

* updated documentation

* fixed minor comments

* formatting

---------

Co-authored-by: Alton Banushi <[email protected]>
  • Loading branch information
gpalmer27 and banushi-a authored Feb 2, 2025
1 parent 4949f92 commit a8d4a89
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
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
15 changes: 15 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,15 @@
export default function ErrorPage() {
return (
<div className="flex h-[85vh] flex-col">
<div className="flex h-full items-center justify-center">
<div className="text-center">
<h1 className="text-2xl font-bold text-gray-900">
Authentication Error
</h1>
<p className="text-gray-600">You must log in with husky.neu.edu</p>
<p className="text-gray-600">Click the sign in button to try again</p>
</div>
</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;
},
},
} satisfies NextAuthConfig;

Expand Down

0 comments on commit a8d4a89

Please sign in to comment.