Skip to content

Commit

Permalink
Add Vercel Deployments (#7)
Browse files Browse the repository at this point in the history
Co-authored-by: Pavan Soratur <pavansoratur@MacBookAir.lan>
  • Loading branch information
devpavan04 and Pavan Soratur authored Oct 19, 2024
1 parent 03b44ca commit 0abc12d
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 27 deletions.
1 change: 1 addition & 0 deletions .env.example → .env.local.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
AUTH_SECRET=""
NEON_DATABASE_URL_POOLING=""
NEON_DATABASE_URL_NON_POOLING=""
NEXTAUTH_URL=""
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ next-env.d.ts
.env*

# Allow .env.example
!.env.example
!.env.example
!.env.local.example
2 changes: 1 addition & 1 deletion actions/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions app/(protected)/dashboard/page.tsx
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -15,7 +15,7 @@ export default async function DashboardPage() {
'use server';

await signOut({
redirectTo: DEFAULT_LOGIN_REDIRECT,
redirectTo: DEFAULT_LOGGED_OUT_REDIRECT,
});
}}
>
Expand Down
4 changes: 2 additions & 2 deletions components/auth/RegisterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -43,7 +43,7 @@ export const RegisterForm: React.FC<RegisterFormProps> = ({

if (success) {
toast.success(message);
router.push(DEFAULT_LOGIN_REDIRECT);
router.push(DEFAULT_LOGGED_OUT_REDIRECT);
} else {
toast.error(message);
}
Expand Down
6 changes: 0 additions & 6 deletions constants/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
17 changes: 5 additions & 12 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion next.config.mjs
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 1 addition & 2 deletions types/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
export type ServerActionResponse<T = void> = {
success: boolean;
data?: T | null;
message: string;
};
} & (T extends void ? { data?: never } : { data: T | null });

export type DatabaseQueryResponse<T> = {
success: boolean;
Expand Down

0 comments on commit 0abc12d

Please sign in to comment.