-
Notifications
You must be signed in to change notification settings - Fork 0
Add auth #6
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
Merged
Merged
Add auth #6
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9d7a385
add login and signup files
Shitanshukumar607 52ae4fd
add google login w supabase and style the login page
Shitanshukumar607 c227001
format using prettier
Shitanshukumar607 6b14ba6
Update lib/supabase/server.ts
Shitanshukumar607 224f1e9
Update components/NavbarComponents/AuthButtons.tsx
Shitanshukumar607 115a954
Update app/login/page.tsx
Shitanshukumar607 50448d8
fix eslint and prettier
Shitanshukumar607 235fab5
Merge branch 'add-auth' of https://github.com/Shitanshukumar607/imgTo…
Shitanshukumar607 1345ce8
fix not-found page
Shitanshukumar607 851c89a
Merge branch 'main' into add-auth
Shitanshukumar607 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { NextResponse } from "next/server"; | ||
| // The client you created from the Server-Side Auth instructions | ||
| import { createClient } from "@/lib/supabase/server"; | ||
|
|
||
| export async function GET(request: Request) { | ||
| const { searchParams, origin } = new URL(request.url); | ||
| const code = searchParams.get("code"); | ||
| // if "next" is in param, use it as the redirect URL | ||
| let next = searchParams.get("next") ?? "/"; | ||
| if (!next.startsWith("/")) { | ||
| // if "next" is not a relative URL, use the default | ||
| next = "/"; | ||
| } | ||
|
|
||
| if (code) { | ||
| const supabase = await createClient(); | ||
| const { error } = await supabase.auth.exchangeCodeForSession(code); | ||
| if (!error) { | ||
| const forwardedHost = request.headers.get("x-forwarded-host"); // original origin before load balancer | ||
| const isLocalEnv = process.env.NODE_ENV === "development"; | ||
| if (isLocalEnv) { | ||
| // we can be sure that there is no load balancer in between, so no need to watch for X-Forwarded-Host | ||
| return NextResponse.redirect(`${origin}${next}`); | ||
| } else if (forwardedHost) { | ||
| return NextResponse.redirect(`https://${forwardedHost}${next}`); | ||
| } else { | ||
| return NextResponse.redirect(`${origin}${next}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // return the user to an error page with instructions | ||
| return NextResponse.redirect(`${origin}/auth/auth-code-error`); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { redirect } from "next/navigation"; | ||
|
|
||
| const Page = () => { | ||
| redirect("/"); | ||
| }; | ||
|
|
||
| export default Page; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| "use client"; | ||
|
|
||
| import { Button } from "@/components/ui/button"; | ||
| import { | ||
| Card, | ||
| CardContent, | ||
| CardDescription, | ||
| CardHeader, | ||
| CardTitle, | ||
| } from "@/components/ui/card"; | ||
| import { | ||
| Field, | ||
| FieldDescription, | ||
| FieldGroup, | ||
| FieldLabel, | ||
| FieldSeparator, | ||
| } from "@/components/ui/field"; | ||
| import { Input } from "@/components/ui/input"; | ||
| import { createClient } from "@/lib/supabase/client"; | ||
| import Image from "next/image"; | ||
| import type { Provider } from "@supabase/supabase-js"; | ||
| import Link from "next/link"; | ||
| import { useRouter } from "next/navigation"; | ||
| import { useState } from "react"; | ||
|
|
||
| const Login = () => { | ||
| const [email, setEmail] = useState(""); | ||
| const [password, setPassword] = useState(""); | ||
| const [error, setError] = useState<string | null>(null); | ||
| const [isLoading, setIsLoading] = useState(false); | ||
| const router = useRouter(); | ||
|
|
||
| const supabase = createClient(); | ||
|
|
||
| const handleLogin = async (e: React.FormEvent) => { | ||
| e.preventDefault(); | ||
| setIsLoading(true); | ||
| setError(null); | ||
|
|
||
| try { | ||
| const { error } = await supabase.auth.signInWithPassword({ | ||
| email, | ||
| password, | ||
| }); | ||
| if (error) throw error; | ||
| router.push("/"); | ||
| } catch (error: unknown) { | ||
| setError(error instanceof Error ? error.message : "An error occurred"); | ||
| } finally { | ||
| setIsLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| const handleOAuthLogin = async (provider: Provider) => { | ||
| setIsLoading(true); | ||
| setError(null); | ||
|
|
||
| try { | ||
| const { error } = await supabase.auth.signInWithOAuth({ | ||
| provider, | ||
| options: { | ||
| redirectTo: `${process.env.SITE_URL}/auth/callback`, | ||
| }, | ||
| }); | ||
| if (error) throw error; | ||
| } catch (error: unknown) { | ||
| setError(error instanceof Error ? error.message : "An error occurred"); | ||
| } finally { | ||
| setIsLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="bg-zinc-50 dark:bg-neutral-900/80 flex min-h-svh w-full items-center justify-center p-6 md:p-10"> | ||
| <div className="w-full max-w-sm"> | ||
| <div className="flex flex-col gap-6"> | ||
| <Card> | ||
| <CardHeader className="text-center"> | ||
| <CardTitle className="text-xl">Welcome back</CardTitle> | ||
| <CardDescription> | ||
| Login with your GitHub or Google account | ||
| </CardDescription> | ||
| </CardHeader> | ||
| <CardContent> | ||
| <form onSubmit={handleLogin}> | ||
| <FieldGroup> | ||
| <Field> | ||
| <Button | ||
| onClick={() => handleOAuthLogin("github")} | ||
| variant="outline" | ||
| type="button" | ||
| > | ||
| <Image | ||
| width="24" | ||
| height="24" | ||
| src="https://img.icons8.com/material-outlined/24/github.png" | ||
| alt="github" | ||
| /> | ||
| Login with GitHub | ||
| </Button> | ||
| <Button | ||
| onClick={() => handleOAuthLogin("google")} | ||
| variant="outline" | ||
| type="button" | ||
| > | ||
| <Image | ||
| width="24" | ||
| height="24" | ||
| src="https://img.icons8.com/material-rounded/24/google-logo.png" | ||
| alt="google-logo" | ||
| /> | ||
| Login with Google | ||
| </Button> | ||
| </Field> | ||
| <FieldSeparator className="*:data-[slot=field-separator-content]:bg-card"> | ||
| Or continue with | ||
| </FieldSeparator> | ||
| <Field> | ||
| <FieldLabel htmlFor="email">Email</FieldLabel> | ||
| <Input | ||
| id="email" | ||
| type="email" | ||
| placeholder="[email protected]" | ||
| required | ||
| value={email} | ||
| onChange={(e) => setEmail(e.target.value)} | ||
| /> | ||
| </Field> | ||
| <Field> | ||
| <div className="flex items-center"> | ||
| <FieldLabel htmlFor="password">Password</FieldLabel> | ||
| <Link | ||
| href="/forgot-password" | ||
| className="ml-auto text-sm underline-offset-4 hover:underline" | ||
| > | ||
| Forgot your password? | ||
| </Link> | ||
| </div> | ||
| <Input | ||
| id="password" | ||
| type="password" | ||
| required | ||
| value={password} | ||
| onChange={(e) => setPassword(e.target.value)} | ||
| /> | ||
| </Field> | ||
| {error && <p className="text-sm text-red-500">{error}</p>} | ||
| <Field> | ||
| <Button | ||
| type="submit" | ||
| className="w-full" | ||
| disabled={isLoading} | ||
| > | ||
| {isLoading ? "Logging in..." : "Login"} | ||
| </Button> | ||
| <FieldDescription className="text-center"> | ||
| Don't have an account?{" "} | ||
| <Link href="/signup">Sign up</Link> | ||
| </FieldDescription> | ||
| </Field> | ||
| </FieldGroup> | ||
| </form> | ||
| </CardContent> | ||
| </Card> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default Login; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| const Signup = () => { | ||
| return <div>Signup</div>; | ||
| }; | ||
|
|
||
| export default Signup; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| "use client"; | ||
|
|
||
| import { createClient } from "@/lib/supabase/client"; | ||
| import Link from "next/link"; | ||
| import { useEffect, useState } from "react"; | ||
|
|
||
| const AuthButtons = () => { | ||
| const supabase = createClient(); | ||
| const [user, setUser] = useState<string | null>(null); | ||
| const [loading, setLoading] = useState(true); | ||
|
|
||
| useEffect(() => { | ||
| const fetchUser = async () => { | ||
| const { data } = await supabase.auth.getUser(); | ||
| if (data.user) setUser(data.user.user_metadata.name); | ||
| setLoading(false); | ||
| }; | ||
| fetchUser(); | ||
| }, [supabase.auth]); | ||
|
|
||
| return ( | ||
| <div> | ||
| {user ? ( | ||
| <button className="hidden sm:flex text-xs border border-emerald-500 dark:border-purple-500 px-3 py-1.5 rounded-md hover:bg-emerald-100 dark:hover:bg-violet-900 transition-colors duration-200 items-center gap-1.5"> | ||
| <span>Logout</span> | ||
| {!loading && ( | ||
| <span className="text-xs opacity-70">{`(${user})`}</span> | ||
| )} | ||
| </button> | ||
| ) : ( | ||
| <Link | ||
| href="/login" | ||
| className="text-sm border border-emerald-500 dark:border-purple-500 px-4 py-2 rounded-md hover:bg-emerald-100 dark:hover:bg-violet-900 transition-colors duration-200" | ||
| > | ||
| Login | ||
| </Link> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default AuthButtons; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redirectTo points to /auth/callback and uses process.env.SITE_URL in a client component. Client-side env vars must be prefixed with NEXT_PUBLIC_, and your callback route is implemented at /api/auth/callback. Update to use the correct path and a client-safe origin, e.g.: redirectTo:
${window.location.origin}/api/auth/callback.