An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat: add google social login functionality
Yoshiki Miuraa82c2191faf9
diff --git a/app/auth/oauth/route.ts b/app/auth/oauth/route.tsnew file mode 100644index 00000000..f4401746--- /dev/null+++ b/app/auth/oauth/route.ts@@ -0,0 +1,30 @@+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+ const next = searchParams.get('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/error`)+}diff --git a/components/login-form.tsx b/components/login-form.tsxindex 6f5dfbbc..1af54d49 100644--- a/components/login-form.tsx+++ b/components/login-form.tsx@@ -1,22 +1,25 @@'use client'-import { cn } from '@/lib/utils/index'-import { createClient } from '@/lib/supabase/client'import { Button } from '@/components/ui/button'import {Card,CardContent,CardDescription,CardHeader,- CardTitle,+ CardTitle} from '@/components/ui/card'import { Input } from '@/components/ui/input'import { Label } from '@/components/ui/label'+import { createClient } from '@/lib/supabase/client'+import { cn } from '@/lib/utils/index'import Link from 'next/link'import { useRouter } from 'next/navigation'import { useState } from 'react'-export function LoginForm({ className, ...props }: React.ComponentPropsWithoutRef<'div'>) {+export function LoginForm({+ className,+ ...props+}: React.ComponentPropsWithoutRef<'div'>) {const [email, setEmail] = useState('')const [password, setPassword] = useState('')const [error, setError] = useState<string | null>(null)@@ -32,7 +35,7 @@ export function LoginForm({ className, ...props }: React.ComponentPropsWithoutRetry {const { error } = await supabase.auth.signInWithPassword({email,- password,+ password})if (error) throw error// Update this route to redirect to an authenticated route. The user already has an active session.@@ -44,12 +47,36 @@ export function LoginForm({ className, ...props }: React.ComponentPropsWithoutRe}}+ const handleSocialLogin = async () => {+ const supabase = createClient()+ setIsLoading(true)+ setError(null)++ try {+ const { error } = await supabase.auth.signInWithOAuth({+ provider: 'google',+ options: {+ redirectTo: `${location.origin}/auth/oauth`+ }+ })+ if (error) throw error+ } catch (error: unknown) {+ setError(+ error instanceof Error ? error.message : 'An OAuth error occurred'+ )+ } finally {+ setIsLoading(false)+ }+ }+return (<div className={cn('flex flex-col gap-6', className)} {...props}><Card><CardHeader><CardTitle className="text-2xl">Login</CardTitle>- <CardDescription>Enter your email below to login to your account</CardDescription>+ <CardDescription>+ Enter your email below to login to your account+ </CardDescription></CardHeader><CardContent><form onSubmit={handleLogin}>@@ -62,7 +89,7 @@ export function LoginForm({ className, ...props }: React.ComponentPropsWithoutReplaceholder="[email protected]"requiredvalue={email}- onChange={(e) => setEmail(e.target.value)}+ onChange={e => setEmail(e.target.value)}/></div><div className="grid gap-2">@@ -80,17 +107,43 @@ export function LoginForm({ className, ...props }: React.ComponentPropsWithoutRetype="password"requiredvalue={password}- onChange={(e) => setPassword(e.target.value)}+ onChange={e => setPassword(e.target.value)}/></div>{error && <p className="text-sm text-red-500">{error}</p>}<Button type="submit" className="w-full" disabled={isLoading}>{isLoading ? 'Logging in...' : 'Login'}</Button>++ {/* Divider and Social Login */}+ <div className="relative my-4">+ <div className="absolute inset-0 flex items-center">+ <span className="w-full border-t" />+ </div>+ <div className="relative flex justify-center text-xs uppercase">+ <span className="bg-background px-2 text-muted-foreground">+ Or continue with+ </span>+ </div>+ </div>++ <Button+ variant="outline"+ type="button"+ className="w-full"+ onClick={handleSocialLogin}+ disabled={isLoading}+ >+ {/* TODO: Add Google Icon */}+ </Button></div><div className="mt-4 text-center text-sm">Don't have an account?{' '}- <Link href="/auth/sign-up" className="underline underline-offset-4">+ <Link+ href="/auth/sign-up"+ className="underline underline-offset-4"+ >Sign up</Link></div>