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

Refactor/auth v5 #142

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions app/[locale]/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import MaybeLink from '~/components/maybe-link';
import { Button, HamburgerButton } from '~/components/ui';
import { getTranslations } from '~/i18n/translations';
import { cn } from '~/lib/utils';
import { getServerAuthSession } from '~/server/auth';
import { getSession } from '~/server/auth';
import { db } from '~/server/db';

export default async function Header({ locale }: { locale: string }) {
Expand Down Expand Up @@ -205,7 +205,7 @@ const AuthAction = async ({
mobile?: boolean;
text: { alt: string; login: string; view: string };
}) => {
const session = await getServerAuthSession();
const session = await getSession();

if (session) {
let id = '';
Expand Down
25 changes: 22 additions & 3 deletions app/[locale]/login/login.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { FcGoogle } from 'react-icons/fc';

import { Input } from '~/components/inputs';
import { Button } from '~/components/ui';
import { getTranslations } from '~/i18n/translations';
import { cn } from '~/lib/utils';
import { signIn } from '~/server/auth';
import { getS3Url } from '~/server/s3';

import LoginButton from './sign-in-with-google-button';

const Login = async ({ locale }: { locale: string }) => {
const text = (await getTranslations(locale)).Login;

Expand Down Expand Up @@ -43,7 +44,25 @@ const Login = async ({ locale }: { locale: string }) => {
locale === 'en' ? "after:content-['OR']" : "after:content-['अथवा']"
)}
/>
<LoginButton loginText={text.signInWithGoogle} />

<form
action={async () => {
'use server';
await signIn('google', { redirectTo: `/${locale}/profile` });
}}
>
<Button
className={cn(
'p-2 sm:p-3 md:p-4',
'w-full space-x-3 bg-neutral-50 hover:bg-neutral-100'
)}
type="submit"
variant="outline"
>
<FcGoogle className="size-4 sm:size-5 md:size-6" />
<span>{text.signInWithGoogle}</span>
</Button>
</form>
</section>
</article>
);
Expand Down
25 changes: 0 additions & 25 deletions app/[locale]/login/sign-in-with-google-button.tsx

This file was deleted.

17 changes: 0 additions & 17 deletions app/[locale]/profile/client-utils.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use client';

import { signOut } from 'next-auth/react';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
import { Suspense } from 'react';
Expand All @@ -20,22 +19,6 @@ import Loading from '~/components/loading';
import { Button } from '~/components/ui';
import { cn } from '~/lib/utils';

export const LogOut = ({
className,
text,
}: {
className?: string;
text: string;
}) => (
<Button
className={className}
onClick={() => signOut({ callbackUrl: '/' })}
variant="ghost"
>
{text}
</Button>
);

export const PathnameAwareSuspense = ({
children,
}: {
Expand Down
4 changes: 2 additions & 2 deletions app/[locale]/profile/clubs/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Image from 'next/image';

import { getTranslations } from '~/i18n/translations';
import { capitalise } from '~/lib/utils';
import { getServerAuthSession } from '~/server/auth';
import { getSession } from '~/server/auth';
import { db } from '~/server/db';

export default async function Clubs({
Expand All @@ -12,7 +12,7 @@ export default async function Clubs({
}) {
const text = (await getTranslations(locale)).Profile.tabs.clubs;

const session = (await getServerAuthSession())!;
const session = (await getSession())!;
const student = (await db.query.students.findFirst({
columns: {},
where: (student, { eq }) => eq(student.id, session.person.id),
Expand Down
18 changes: 14 additions & 4 deletions app/[locale]/profile/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import Image from 'next/image';

import { Button } from '~/components/ui';
import Unauthorized from '~/components/unauthorized';
import { getTranslations } from '~/i18n/translations';
import { cn } from '~/lib/utils';
import { getServerAuthSession } from '~/server/auth';
import { getSession, signOut } from '~/server/auth';
import { db } from '~/server/db';

import { LogOut, PathnameAwareSuspense, Tabs } from './client-utils';
import { PathnameAwareSuspense, Tabs } from './client-utils';

export default async function ProfileLayout({
children,
Expand All @@ -15,7 +16,7 @@ export default async function ProfileLayout({
children: React.ReactNode;
params: { locale: string };
}) {
const session = await getServerAuthSession();
const session = await getSession();
if (!session) return <Unauthorized locale={locale} />;

const text = (await getTranslations(locale)).Profile;
Expand Down Expand Up @@ -78,7 +79,16 @@ export default async function ProfileLayout({
/>
</ol>

<LogOut className="px-3 py-2" text={text.logout} />
<form
action={async () => {
'use server';
await signOut({ redirectTo: '/' });
}}
>
<Button className="px-3 py-2" variant="ghost">
{text.logout}
</Button>
</form>
</article>

<Tabs
Expand Down
4 changes: 2 additions & 2 deletions app/[locale]/profile/personal/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getTranslations } from '~/i18n/translations';
import { getServerAuthSession } from '~/server/auth';
import { getSession } from '~/server/auth';
import { db } from '~/server/db';

export default async function Personal({
Expand All @@ -9,7 +9,7 @@ export default async function Personal({
}) {
const text = (await getTranslations(locale)).Profile.tabs.personal;

const session = (await getServerAuthSession())!;
const session = (await getSession())!;
const person = (await db.query.persons.findFirst({
columns: {
alternateTelephone: true,
Expand Down
9 changes: 2 additions & 7 deletions app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
import NextAuth from 'next-auth';

import { authOptions } from '~/server/auth';

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
import { handlers } from '~/server/auth';
export const { GET, POST } = handlers;
7 changes: 3 additions & 4 deletions lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ export const env = createEnv({

POSTGRES_URL: z.string().url(),

NEXTAUTH_URL: z.string().url(),
NEXTAUTH_SECRET: z.string(),
GOOGLE_CLIENT_ID: z.string(),
GOOGLE_CLIENT_SECRET: z.string(),
AUTH_SECRET: z.string(),
AUTH_GOOGLE_ID: z.string(),
AUTH_GOOGLE_SECRET: z.string(),

AWS_ACCESS_KEY_ID: z.string(),
AWS_ACCESS_KEY_SECRET: z.string(),
Expand Down
Loading