From 0abc12d89588a0512bd7e363b0b776bacda0ffa4 Mon Sep 17 00:00:00 2001 From: Pavan Soratur Date: Sat, 19 Oct 2024 16:56:57 -0700 Subject: [PATCH] Add Vercel Deployments (#7) Co-authored-by: Pavan Soratur --- .env.example => .env.local.example | 1 + .gitignore | 3 ++- actions/auth/login.ts | 2 +- app/(protected)/dashboard/page.tsx | 4 ++-- components/auth/RegisterForm.tsx | 4 ++-- constants/routes.ts | 6 ------ middleware.ts | 17 +++++------------ next.config.mjs | 8 +++++++- types/types.ts | 3 +-- 9 files changed, 21 insertions(+), 27 deletions(-) rename .env.example => .env.local.example (83%) diff --git a/.env.example b/.env.local.example similarity index 83% rename from .env.example rename to .env.local.example index 27c0c99..85e4a45 100644 --- a/.env.example +++ b/.env.local.example @@ -1,3 +1,4 @@ AUTH_SECRET="" NEON_DATABASE_URL_POOLING="" NEON_DATABASE_URL_NON_POOLING="" +NEXTAUTH_URL="" \ No newline at end of file diff --git a/.gitignore b/.gitignore index d24949a..8a1826e 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,5 @@ next-env.d.ts .env* # Allow .env.example -!.env.example \ No newline at end of file +!.env.example +!.env.local.example \ No newline at end of file diff --git a/actions/auth/login.ts b/actions/auth/login.ts index a827f04..93b2043 100644 --- a/actions/auth/login.ts +++ b/actions/auth/login.ts @@ -32,7 +32,7 @@ export async function login({ return { success: true, - message: 'Successfully logged in!', + message: 'Welcome! You are logged in.', }; } catch (error: unknown) { if (isRedirectError(error)) { diff --git a/app/(protected)/dashboard/page.tsx b/app/(protected)/dashboard/page.tsx index 4ef068d..79af37b 100644 --- a/app/(protected)/dashboard/page.tsx +++ b/app/(protected)/dashboard/page.tsx @@ -1,5 +1,5 @@ import { auth, signOut } from '@/auth'; -import { DEFAULT_LOGIN_REDIRECT } from '@/constants/routes'; +import { DEFAULT_LOGGED_OUT_REDIRECT } from '@/constants/routes'; import { Button } from '@radix-ui/themes'; export default async function DashboardPage() { @@ -15,7 +15,7 @@ export default async function DashboardPage() { 'use server'; await signOut({ - redirectTo: DEFAULT_LOGIN_REDIRECT, + redirectTo: DEFAULT_LOGGED_OUT_REDIRECT, }); }} > diff --git a/components/auth/RegisterForm.tsx b/components/auth/RegisterForm.tsx index 3196619..7d7e98d 100644 --- a/components/auth/RegisterForm.tsx +++ b/components/auth/RegisterForm.tsx @@ -9,7 +9,7 @@ import { RegisterFormData } from '@/types/auth/types'; import toast from 'react-hot-toast'; import { Text, Button, Flex, TextField, Card } from '@radix-ui/themes'; import Link from 'next/link'; -import { DEFAULT_LOGIN_REDIRECT } from '@/constants/routes'; +import { DEFAULT_LOGGED_OUT_REDIRECT } from '@/constants/routes'; import { useRouter } from 'next/navigation'; interface RegisterFormProps { @@ -43,7 +43,7 @@ export const RegisterForm: React.FC = ({ if (success) { toast.success(message); - router.push(DEFAULT_LOGIN_REDIRECT); + router.push(DEFAULT_LOGGED_OUT_REDIRECT); } else { toast.error(message); } diff --git a/constants/routes.ts b/constants/routes.ts index 86511d7..be60f69 100644 --- a/constants/routes.ts +++ b/constants/routes.ts @@ -17,12 +17,6 @@ export const AUTH_ROUTES = ['/auth/login', '/auth/register']; */ export const API_AUTH_PREFIX = '/api/auth'; -/** - * The default route for the login page - '/auth/login'. - * @type {string} - */ -export const DEFAULT_LOGIN_REDIRECT = '/auth/login'; - /** * If the user is logged in, they will be redirected to '/dashboard'. * @type {string} diff --git a/middleware.ts b/middleware.ts index cffad1b..647e396 100644 --- a/middleware.ts +++ b/middleware.ts @@ -10,22 +10,15 @@ import { } from '@/constants/routes'; export async function middleware(req: NextRequest) { - let token; - - try { - token = await getToken({ - req, - secret: process.env.AUTH_SECRET, - }); - } catch (error: unknown) { - console.error('Error verifying user token:', error); - return NextResponse.redirect(new URL(DEFAULT_LOGGED_OUT_REDIRECT, req.url)); - } + const token = await getToken({ + req, + secret: process.env.AUTH_SECRET, + secureCookie: process.env.NODE_ENV === 'production', + }); const { pathname } = req.nextUrl; const isUserLoggedIn = !!token; - const isApiAuthRoute = pathname.startsWith(API_AUTH_PREFIX); const isAuthRoute = AUTH_ROUTES.includes(pathname); const isPublicRoute = PUBLIC_ROUTES.includes(pathname); diff --git a/next.config.mjs b/next.config.mjs index 4678774..8417fca 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -1,4 +1,10 @@ /** @type {import('next').NextConfig} */ -const nextConfig = {}; +const nextConfig = { + env: { + NEXTAUTH_URL: process.env.VERCEL_URL + ? `https://${process.env.VERCEL_URL}` + : process.env.NEXTAUTH_URL, + }, +}; export default nextConfig; diff --git a/types/types.ts b/types/types.ts index c015046..d29ea34 100644 --- a/types/types.ts +++ b/types/types.ts @@ -1,8 +1,7 @@ export type ServerActionResponse = { success: boolean; - data?: T | null; message: string; -}; +} & (T extends void ? { data?: never } : { data: T | null }); export type DatabaseQueryResponse = { success: boolean;