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

😄 Adding auth0 login to frontend #12

Merged
merged 7 commits into from
Nov 13, 2024
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/.pnp
.pnp.js
.yarn/install-state.gz
package-lock.json/
package.json/

# testing
/coverage
Expand Down
18 changes: 18 additions & 0 deletions app/api/auth/[...auth0].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { handleAuth, handleLogout } from '@auth0/nextjs-auth0';
import type { NextApiRequest, NextApiResponse } from 'next';

export default handleAuth({
logout: async (req: NextApiRequest, res: NextApiResponse) => {
try {
await handleLogout(req, res, {
returnTo: process.env.AUTH0_BASE_URL,
logoutParams: {
federated: 'true'
}
});
} catch (error) {
console.error('Error during logout:', error);
res.status(500).end('Internal Server Error');
}
},
});
10 changes: 10 additions & 0 deletions app/components/LoginButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const handleLogin = () => {
// Clear any existing auth tokens/cookies
document.cookie.split(";").forEach((c) => {
document.cookie = c
.replace(/^ +/, "")
.replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/");
});

window.location.href = '/api/auth/login';
};
23 changes: 10 additions & 13 deletions app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type { Metadata } from "next";
import { UserProvider } from '@auth0/nextjs-auth0/client';
import localFont from "next/font/local";
import "./globals.css";
import React from "react";
import { Providers } from './providers';

const geistSans = localFont({
src: "./fonts/GeistVF.woff",
variable: "--font-geist-sans",
weight: "100 900",
});

const geistMono = localFont({
src: "./fonts/GeistMonoVF.woff",
variable: "--font-geist-mono",
Expand Down Expand Up @@ -45,19 +47,14 @@ export const metadata: Metadata = {

export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<head>
<link rel="icon" href="/pawicon.png" type="image/icon" />
</head>
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
{children}
<html lang="en" suppressHydrationWarning>
<body>
<Providers>{children}</Providers>
</body>
</html>
);
}
}
11 changes: 11 additions & 0 deletions app/providers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use client';

import { UserProvider } from '@auth0/nextjs-auth0/client';

export function Providers({ children }: { children: React.ReactNode }) {
return (
<UserProvider>
{children}
</UserProvider>
);
}
17 changes: 17 additions & 0 deletions app/utils/auth0-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { initAuth0 } from '@auth0/nextjs-auth0';

export const auth0 = initAuth0({
baseURL: process.env.AUTH0_BASE_URL,
clientID: process.env.AUTH0_CLIENT_ID,
clientSecret: process.env.AUTH0_CLIENT_SECRET,
issuerBaseURL: process.env.AUTH0_ISSUER_BASE_URL,
secret: process.env.AUTH0_SECRET,
session: {
absoluteDuration: 24 * 60 * 60,
cookie: {
secure: process.env.NODE_ENV === "production",
sameSite: "lax",
domain: process.env.NODE_ENV === "production" ? "your-domain.com" : "localhost",
}
},
});
21 changes: 21 additions & 0 deletions components/BrowserComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use client';

import { useEffect, useState } from 'react';

export function BrowserComponent() {
const [mounted, setMounted] = useState(false);

useEffect(() => {
setMounted(true);
}, []);

if (!mounted) {
return null; // or loading state
}

return (
<div>
{/* Your component content goes here */}
</div>
);
}
46 changes: 46 additions & 0 deletions components/ErrorBoundry.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// components/ErrorBoundary.tsx
'use client';

import { Component, ErrorInfo, ReactNode } from 'react';

interface Props {
children: ReactNode;
}

interface State {
hasError: boolean;
}

export class ErrorBoundary extends Component<Props, State> {
public state: State = {
hasError: false
};

public static getDerivedStateFromError(_: Error): State {
return { hasError: true };
}

public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error('Uncaught error:', error, errorInfo);
}

public render() {
if (this.state.hasError) {
return (
<div className="flex items-center justify-center min-h-screen">
<div className="text-center">
<h2 className="text-2xl font-bold mb-4">Something went wrong</h2>
<button
className="bg-blue-600 text-white px-4 py-2 rounded"
onClick={() => this.setState({ hasError: false })}
>
Try again
</button>
</div>
</div>
);
}

return this.props.children;
}
}
Loading
Loading