Skip to content

Commit

Permalink
add google sign in
Browse files Browse the repository at this point in the history
  • Loading branch information
ansh committed Aug 28, 2024
1 parent eaa78ff commit 97ef49b
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 128 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

.env
Binary file modified bun.lockb
Binary file not shown.
15 changes: 14 additions & 1 deletion next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};
const nextConfig = {
images: {
remotePatterns: [
{
protocol: "https",
hostname: "placehold.co",
port: "",
pathname: "/**",
},
],
dangerouslyAllowSVG: true,
contentSecurityPolicy: "default-src 'self'; script-src 'none'; sandbox;",
},
};

export default nextConfig;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
"lint": "next lint"
},
"dependencies": {
"firebase": "^10.13.0",
"next": "14.2.7",
"react": "^18",
"react-dom": "^18",
"next": "14.2.7"
"react-dom": "^18"
},
"devDependencies": {
"typescript": "^5",
Expand Down
22 changes: 22 additions & 0 deletions src/app/(protected)/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use client";

import { useEffect } from "react";
import { useRouter } from "next/navigation";
import { useAuth } from "../../contexts/AuthContext";

export default function ProtectedLayout({ children }: { children: React.ReactNode }) {
const { user, loading } = useAuth();
const router = useRouter();

useEffect(() => {
if (!loading && !user) {
router.push("/signin");
}
}, [user, loading, router]);

if (loading || !user) {
return null; // Render nothing while checking authentication
}

return <>{children}</>;
}
51 changes: 51 additions & 0 deletions src/app/(protected)/templates/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"use client";

import Link from "next/link";
import Image from "next/image";
import { auth } from "../../../lib/firebase";

export default function Templates() {
const signOut = () => {
auth.signOut().then(() => {
window.location.href = "/";
});
};

return (
<main className="flex min-h-screen flex-col items-center justify-between p-8">
<header className="w-full flex justify-between items-center mb-8">
<h1 className="text-3xl font-bold">Templates</h1>
<button onClick={signOut} className="text-blue-500 hover:underline">
Sign Out
</button>
</header>
<p className="mb-4">This is a protected route. Only authenticated users can see this page.</p>

{/* Template images */}
<div className="grid grid-cols-3 gap-4 mb-8">
<Image
src="https://placehold.co/300x200/orange/white?text=Template+1"
alt="Template 1"
width={300}
height={200}
/>
<Image
src="https://placehold.co/300x200/blue/white?text=Template+2"
alt="Template 2"
width={300}
height={200}
/>
<Image
src="https://placehold.co/300x200/green/white?text=Template+3"
alt="Template 3"
width={300}
height={200}
/>
</div>

<Link href="/" className="text-blue-500 hover:underline">
Back to Home
</Link>
</main>
);
}
20 changes: 5 additions & 15 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import { AuthProvider } from "../contexts/AuthContext";
import "./globals.css";

const inter = Inter({ subsets: ["latin"] });

export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};

export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
<body>
<AuthProvider>{children}</AuthProvider>
</body>
</html>
);
}
159 changes: 56 additions & 103 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,113 +1,66 @@
"use client";

import Image from "next/image";
import Link from "next/link";
import { useAuth } from "../contexts/AuthContext";

export default function Home() {
return (
<main className="flex min-h-screen flex-col items-center justify-between p-24">
<div className="z-10 w-full max-w-5xl items-center justify-between font-mono text-sm lg:flex">
<p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
Get started by editing&nbsp;
<code className="font-mono font-bold">src/app/page.tsx</code>
</p>
<div className="fixed bottom-0 left-0 flex h-48 w-full items-end justify-center bg-gradient-to-t from-white via-white dark:from-black dark:via-black lg:static lg:size-auto lg:bg-none">
<a
className="pointer-events-none flex place-items-center gap-2 p-8 lg:pointer-events-auto lg:p-0"
href="https://vercel.com?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
By{" "}
<Image
src="/vercel.svg"
alt="Vercel Logo"
className="dark:invert"
width={100}
height={24}
priority
/>
</a>
</div>
</div>

<div className="relative z-[-1] flex place-items-center before:absolute before:h-[300px] before:w-full before:-translate-x-1/2 before:rounded-full before:bg-gradient-radial before:from-white before:to-transparent before:blur-2xl before:content-[''] after:absolute after:-z-20 after:h-[180px] after:w-full after:translate-x-1/3 after:bg-gradient-conic after:from-sky-200 after:via-blue-200 after:blur-2xl after:content-[''] before:dark:bg-gradient-to-br before:dark:from-transparent before:dark:to-blue-700 before:dark:opacity-10 after:dark:from-sky-900 after:dark:via-[#0141ff] after:dark:opacity-40 sm:before:w-[480px] sm:after:w-[240px] before:lg:h-[360px]">
<Image
className="relative dark:drop-shadow-[0_0_0.3rem_#ffffff70] dark:invert"
src="/next.svg"
alt="Next.js Logo"
width={180}
height={37}
priority
/>
</div>

<div className="mb-32 grid text-center lg:mb-0 lg:w-full lg:max-w-5xl lg:grid-cols-4 lg:text-left">
<a
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className="mb-3 text-2xl font-semibold">
Docs{" "}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className="m-0 max-w-[30ch] text-sm opacity-50">
Find in-depth information about Next.js features and API.
</p>
</a>
const { user, loading } = useAuth();

<a
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className="mb-3 text-2xl font-semibold">
Learn{" "}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className="m-0 max-w-[30ch] text-sm opacity-50">
Learn about Next.js in an interactive course with&nbsp;quizzes!
</p>
</a>
return (
<main className="flex min-h-screen flex-col items-center justify-between p-8">
<header className="w-full max-w-5xl flex justify-between items-center mb-12">
<h1 className="text-3xl font-bold">TemplateHub</h1>
{!loading &&
(user ? (
<Link href="/templates" className="text-blue-500 hover:underline">
My Templates
</Link>
) : (
<Link href="/signin" className="text-blue-500 hover:underline">
Sign In
</Link>
))}
</header>

<a
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
<section className="text-center mb-16">
<h2 className="text-4xl font-bold mb-4">Welcome to TemplateHub</h2>
<p className="text-xl mb-8">
Discover and use amazing web app templates to kickstart your next project
</p>
<Link
href={user ? "/templates" : "/signin"}
className="bg-blue-500 hover:bg-blue-600 text-white font-bold py-2 px-4 rounded"
>
<h2 className="mb-3 text-2xl font-semibold">
Templates{" "}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className="m-0 max-w-[30ch] text-sm opacity-50">
Explore starter templates for Next.js.
</p>
</a>
{user ? "My Templates" : "Browse Templates"}
</Link>
</section>

<a
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className="mb-3 text-2xl font-semibold">
Deploy{" "}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className="m-0 max-w-[30ch] text-balance text-sm opacity-50">
Instantly deploy your Next.js site to a shareable URL with Vercel.
</p>
</a>
</div>
<section className="w-full max-w-5xl mb-16">
<h3 className="text-2xl font-semibold mb-6">Featured Templates</h3>
<div className="grid grid-cols-1 md:grid-cols-3 gap-8">
{[1, 2, 3].map((i) => (
<div key={i} className="border rounded-lg overflow-hidden shadow-lg">
<Image
src={`https://placehold.co/400x200/${
["orange", "blue", "green"][i - 1]
}/white?text=Template+${i}`}
alt={`Template ${i}`}
width={400}
height={200}
className="w-full"
/>
<div className="p-4">
<h4 className="font-bold mb-2">Template {i}</h4>
<p className="text-sm mb-4">A brief description of Template {i}</p>
<Link href={`/template/${i}`} className="text-blue-500 hover:underline">
Learn More
</Link>
</div>
</div>
))}
</div>
</section>
</main>
);
}
30 changes: 30 additions & 0 deletions src/app/signin/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use client";

import { useEffect } from "react";
import { useRouter } from "next/navigation";
import SignInWithGoogle from "../../components/SignInWithGoogle";
import { useAuth } from "../../contexts/AuthContext";

export default function SignIn() {
const { user, loading } = useAuth();
const router = useRouter();

useEffect(() => {
if (!loading && user) {
router.push("/templates");
}
}, [user, loading, router]);

if (loading || user) {
return null;
}

return (
<main className="flex min-h-screen flex-col items-center justify-center p-8 bg-gray-100">
<div className="bg-white p-8 rounded-lg shadow-md w-full max-w-md">
<h1 className="text-3xl font-bold mb-6 text-center">Sign In</h1>
<SignInWithGoogle />
</div>
</main>
);
}
22 changes: 22 additions & 0 deletions src/components/SignInWithGoogle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use client";

import { signInWithPopup, GoogleAuthProvider } from "firebase/auth";
import { auth } from "../lib/firebase";
import Image from "next/image";

export default function SignInWithGoogle() {
const signIn = () => {
const provider = new GoogleAuthProvider();
signInWithPopup(auth, provider);
};

return (
<button
onClick={signIn}
className="flex items-center justify-center w-full bg-white text-gray-700 font-semibold py-2 px-4 border border-gray-300 rounded-md shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
>
<Image src="/google-logo.png" alt="Google logo" width={20} height={20} className="mr-2" />
Sign in with Google
</button>
);
}
30 changes: 30 additions & 0 deletions src/contexts/AuthContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use client";

import React, { createContext, useContext, useEffect, useState } from "react";
import { auth } from "../lib/firebase";
import { User } from "firebase/auth";

interface AuthContextType {
user: User | null;
loading: boolean;
}

const AuthContext = createContext<AuthContextType>({ user: null, loading: true });

export function AuthProvider({ children }: { children: React.ReactNode }) {
const [user, setUser] = useState<User | null>(null);
const [loading, setLoading] = useState(true);

useEffect(() => {
const unsubscribe = auth.onAuthStateChanged((user) => {
setUser(user);
setLoading(false);
});

return () => unsubscribe();
}, []);

return <AuthContext.Provider value={{ user, loading }}>{children}</AuthContext.Provider>;
}

export const useAuth = () => useContext(AuthContext);
Loading

0 comments on commit 97ef49b

Please sign in to comment.