initial commit
This commit is contained in:
47
components/auth/GitHubSignInButton.tsx
Normal file
47
components/auth/GitHubSignInButton.tsx
Normal file
@@ -0,0 +1,47 @@
|
||||
'use client'
|
||||
import { useState } from 'react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Github } from 'lucide-react'
|
||||
|
||||
interface GitHubSignInButtonProps {
|
||||
className?: string
|
||||
disabled?: boolean
|
||||
}
|
||||
|
||||
export function GitHubSignInButton({ className, disabled }: GitHubSignInButtonProps) {
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
|
||||
const handleGitHubSignIn = async () => {
|
||||
try {
|
||||
setIsLoading(true)
|
||||
|
||||
// Get GitHub OAuth URL from our API
|
||||
const response = await fetch('/api/auth/github')
|
||||
const data = await response.json()
|
||||
|
||||
if (data.success && data.data.authURL) {
|
||||
// Redirect to GitHub OAuth
|
||||
window.location.href = data.data.authURL
|
||||
} else {
|
||||
console.error('Failed to get GitHub auth URL:', data.error)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('GitHub sign-in error:', error)
|
||||
} finally {
|
||||
setIsLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={handleGitHubSignIn}
|
||||
disabled={disabled || isLoading}
|
||||
className={className}
|
||||
>
|
||||
<Github className="w-4 h-4 mr-2" />
|
||||
{isLoading ? 'Signing in...' : 'Continue with GitHub'}
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
63
components/auth/GoogleSignInButton.tsx
Normal file
63
components/auth/GoogleSignInButton.tsx
Normal file
@@ -0,0 +1,63 @@
|
||||
'use client'
|
||||
import { useState } from 'react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
|
||||
interface GoogleSignInButtonProps {
|
||||
className?: string
|
||||
disabled?: boolean
|
||||
}
|
||||
|
||||
export function GoogleSignInButton({ className, disabled }: GoogleSignInButtonProps) {
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
|
||||
const handleGoogleSignIn = async () => {
|
||||
try {
|
||||
setIsLoading(true)
|
||||
|
||||
// Get Google OAuth URL from our API
|
||||
const response = await fetch('/api/auth/google')
|
||||
const data = await response.json()
|
||||
|
||||
if (data.success && data.data.authURL) {
|
||||
// Redirect to Google OAuth
|
||||
window.location.href = data.data.authURL
|
||||
} else {
|
||||
console.error('Failed to get Google auth URL:', data.error)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Google sign-in error:', error)
|
||||
} finally {
|
||||
setIsLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={handleGoogleSignIn}
|
||||
disabled={disabled || isLoading}
|
||||
className={className}
|
||||
>
|
||||
<svg className="w-4 h-4 mr-2" viewBox="0 0 24 24">
|
||||
<path
|
||||
fill="currentColor"
|
||||
d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"
|
||||
/>
|
||||
<path
|
||||
fill="currentColor"
|
||||
d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
|
||||
/>
|
||||
<path
|
||||
fill="currentColor"
|
||||
d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"
|
||||
/>
|
||||
<path
|
||||
fill="currentColor"
|
||||
d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
|
||||
/>
|
||||
</svg>
|
||||
{isLoading ? 'Signing in...' : 'Continue with Google'}
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
175
components/auth/LoginForm.tsx
Normal file
175
components/auth/LoginForm.tsx
Normal file
@@ -0,0 +1,175 @@
|
||||
'use client'
|
||||
import { useState } from 'react'
|
||||
import { useForm } from 'react-hook-form'
|
||||
import { zodResolver } from '@hookform/resolvers/zod'
|
||||
import { z } from 'zod'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardFooter,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card'
|
||||
import { useAuth } from '@/contexts/AuthContext'
|
||||
import { GoogleSignInButton } from './GoogleSignInButton'
|
||||
import { GitHubSignInButton } from './GitHubSignInButton'
|
||||
import { Mail, Lock, Eye, EyeOff } from 'lucide-react'
|
||||
import Link from 'next/link'
|
||||
|
||||
const LoginSchema = z.object({
|
||||
emailOrId: z.string().min(1, 'Email or Silicon ID is required'),
|
||||
password: z.string().min(1, 'Password is required'),
|
||||
rememberMe: z.boolean().optional(),
|
||||
})
|
||||
|
||||
type LoginFormData = z.infer<typeof LoginSchema>
|
||||
|
||||
interface LoginFormProps {
|
||||
onSwitchToRegister?: () => void
|
||||
}
|
||||
|
||||
export function LoginForm({ onSwitchToRegister }: LoginFormProps) {
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [showPassword, setShowPassword] = useState(false)
|
||||
const { login, loading } = useAuth()
|
||||
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
formState: { errors },
|
||||
setValue,
|
||||
getValues,
|
||||
trigger,
|
||||
} = useForm<LoginFormData>({
|
||||
resolver: zodResolver(LoginSchema),
|
||||
})
|
||||
|
||||
const onSubmit = async (data: LoginFormData) => {
|
||||
try {
|
||||
setError(null)
|
||||
await login(data.emailOrId, data.password, data.rememberMe)
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Login failed')
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Card className="w-full max-w-md mx-auto">
|
||||
<CardHeader>
|
||||
<CardTitle>Sign In</CardTitle>
|
||||
<CardDescription>Enter your email and password to access your account</CardDescription>
|
||||
</CardHeader>
|
||||
<form method="post" onSubmit={handleSubmit(onSubmit)}>
|
||||
<CardContent className="space-y-4">
|
||||
{error && (
|
||||
<div className="p-3 text-sm text-red-600 bg-red-50 border border-red-200 rounded-md">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="emailOrId">Email or Silicon ID</Label>
|
||||
<div className="relative">
|
||||
<Mail className="absolute left-3 top-3 h-4 w-4 text-muted-foreground" />
|
||||
<Input
|
||||
id="emailOrId"
|
||||
type="text"
|
||||
placeholder="your@email.com or silicon123"
|
||||
className="pl-10"
|
||||
{...register('emailOrId')}
|
||||
disabled={loading}
|
||||
/>
|
||||
</div>
|
||||
{errors.emailOrId && <p className="text-sm text-red-600">{errors.emailOrId.message}</p>}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<Label htmlFor="password">Password</Label>
|
||||
<Link href="/forgot-password" className="text-sm text-primary hover:underline">
|
||||
Forgot password?
|
||||
</Link>
|
||||
</div>
|
||||
<div className="relative">
|
||||
<Lock className="absolute left-3 top-3 h-4 w-4 text-muted-foreground" />
|
||||
<Input
|
||||
id="password"
|
||||
type={showPassword ? 'text' : 'password'}
|
||||
placeholder="••••••••"
|
||||
className="pl-10 pr-10"
|
||||
{...register('password')}
|
||||
disabled={loading}
|
||||
/>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
|
||||
onClick={() => setShowPassword(!showPassword)}
|
||||
disabled={loading}
|
||||
>
|
||||
{showPassword ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
|
||||
<span className="sr-only">{showPassword ? 'Hide password' : 'Show password'}</span>
|
||||
</Button>
|
||||
</div>
|
||||
{errors.password && <p className="text-sm text-red-600">{errors.password.message}</p>}
|
||||
</div>
|
||||
|
||||
<div className="flex items-center space-x-2">
|
||||
<Checkbox
|
||||
id="rememberMe"
|
||||
checked={getValues('rememberMe') || false}
|
||||
onCheckedChange={(checked) => {
|
||||
setValue('rememberMe', checked as boolean)
|
||||
trigger('rememberMe')
|
||||
}}
|
||||
/>
|
||||
<Label htmlFor="rememberMe" className="text-sm font-normal">
|
||||
Remember me
|
||||
</Label>
|
||||
</div>
|
||||
</CardContent>
|
||||
<CardFooter className="flex flex-col space-y-4 pt-6">
|
||||
<Button type="submit" className="w-full" disabled={loading}>
|
||||
{loading ? 'Signing In...' : 'Sign In'}
|
||||
</Button>
|
||||
|
||||
<div className="relative">
|
||||
<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>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<GoogleSignInButton disabled={loading} />
|
||||
<GitHubSignInButton disabled={loading} />
|
||||
</div>
|
||||
</CardFooter>
|
||||
</form>
|
||||
|
||||
<div className="text-center text-sm text-muted-foreground mt-4">
|
||||
Don't have an account?{' '}
|
||||
{onSwitchToRegister ? (
|
||||
<button
|
||||
onClick={onSwitchToRegister}
|
||||
className="text-primary hover:underline cursor-pointer"
|
||||
>
|
||||
Create account
|
||||
</button>
|
||||
) : (
|
||||
<Link href="/auth?mode=register" className="text-primary hover:underline">
|
||||
Create account
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
308
components/auth/RegisterForm.tsx
Normal file
308
components/auth/RegisterForm.tsx
Normal file
@@ -0,0 +1,308 @@
|
||||
'use client'
|
||||
import { useState } from 'react'
|
||||
import { useForm } from 'react-hook-form'
|
||||
import { zodResolver } from '@hookform/resolvers/zod'
|
||||
import { z } from 'zod'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardFooter,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card'
|
||||
import { useAuth } from '@/contexts/AuthContext'
|
||||
import { GoogleSignInButton } from './GoogleSignInButton'
|
||||
import { GitHubSignInButton } from './GitHubSignInButton'
|
||||
import { User, Mail, Phone, Lock, Eye, EyeOff, Check, X } from 'lucide-react'
|
||||
import Link from 'next/link'
|
||||
|
||||
const RegisterSchema = z
|
||||
.object({
|
||||
name: z.string().min(1, 'Name is required'),
|
||||
email: z.string().email('Invalid email format'),
|
||||
phone: z.string().optional(),
|
||||
password: z
|
||||
.string()
|
||||
.min(6, 'Password must be at least 6 characters')
|
||||
.regex(/[A-Z]/, 'Password must contain at least one uppercase letter')
|
||||
.regex(/[0-9!@#$%^&*]/, 'Password must contain at least one number or special character'),
|
||||
confirmPassword: z.string().min(1, 'Please confirm your password'),
|
||||
agreeTerms: z.boolean().refine((val) => val === true, {
|
||||
message: 'You must agree to the Terms of Service and Privacy Policy',
|
||||
}),
|
||||
})
|
||||
.refine((data) => data.password === data.confirmPassword, {
|
||||
message: "Passwords don't match",
|
||||
path: ['confirmPassword'],
|
||||
})
|
||||
|
||||
type RegisterFormData = z.infer<typeof RegisterSchema>
|
||||
|
||||
interface RegisterFormProps {
|
||||
onSwitchToLogin?: () => void
|
||||
}
|
||||
|
||||
export function RegisterForm({ onSwitchToLogin }: RegisterFormProps) {
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [showPassword, setShowPassword] = useState(false)
|
||||
const [showConfirmPassword, setShowConfirmPassword] = useState(false)
|
||||
const [password, setPassword] = useState('')
|
||||
const { register: registerUser, loading } = useAuth()
|
||||
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
formState: { errors },
|
||||
watch,
|
||||
setValue,
|
||||
getValues,
|
||||
trigger,
|
||||
} = useForm<RegisterFormData>({
|
||||
resolver: zodResolver(RegisterSchema),
|
||||
})
|
||||
|
||||
const watchPassword = watch('password', '')
|
||||
|
||||
// Password strength validation
|
||||
const passwordRequirements = {
|
||||
minLength: watchPassword.length >= 6,
|
||||
hasUppercase: /[A-Z]/.test(watchPassword),
|
||||
hasNumberOrSpecial: /[0-9!@#$%^&*]/.test(watchPassword),
|
||||
}
|
||||
|
||||
const onSubmit = async (data: RegisterFormData) => {
|
||||
try {
|
||||
setError(null)
|
||||
await registerUser(data.email, data.name, data.password)
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Registration failed')
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Card className="w-full max-w-md mx-auto">
|
||||
<CardHeader>
|
||||
<CardTitle>Create Account</CardTitle>
|
||||
<CardDescription>Fill in your details to create a new account</CardDescription>
|
||||
</CardHeader>
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
<CardContent className="space-y-4">
|
||||
{error && (
|
||||
<div className="p-3 text-sm text-red-600 bg-red-50 border border-red-200 rounded-md">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="name">Full Name</Label>
|
||||
<div className="relative">
|
||||
<User className="absolute left-3 top-3 h-4 w-4 text-muted-foreground" />
|
||||
<Input
|
||||
id="name"
|
||||
type="text"
|
||||
placeholder="Enter your full name"
|
||||
className="pl-10"
|
||||
{...register('name')}
|
||||
disabled={loading}
|
||||
/>
|
||||
</div>
|
||||
{errors.name && <p className="text-sm text-red-600">{errors.name.message}</p>}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="email">Email Address</Label>
|
||||
<div className="relative">
|
||||
<Mail className="absolute left-3 top-3 h-4 w-4 text-muted-foreground" />
|
||||
<Input
|
||||
id="email"
|
||||
type="email"
|
||||
placeholder="your@email.com"
|
||||
className="pl-10"
|
||||
{...register('email')}
|
||||
disabled={loading}
|
||||
/>
|
||||
</div>
|
||||
{errors.email && <p className="text-sm text-red-600">{errors.email.message}</p>}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="phone">Phone Number (Optional)</Label>
|
||||
<div className="relative">
|
||||
<Phone className="absolute left-3 top-3 h-4 w-4 text-muted-foreground" />
|
||||
<Input
|
||||
id="phone"
|
||||
type="tel"
|
||||
placeholder="+1 (123) 456-7890"
|
||||
className="pl-10"
|
||||
{...register('phone')}
|
||||
disabled={loading}
|
||||
/>
|
||||
</div>
|
||||
{errors.phone && <p className="text-sm text-red-600">{errors.phone.message}</p>}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="password">Create Password</Label>
|
||||
<div className="relative">
|
||||
<Lock className="absolute left-3 top-3 h-4 w-4 text-muted-foreground" />
|
||||
<Input
|
||||
id="password"
|
||||
type={showPassword ? 'text' : 'password'}
|
||||
placeholder="At least 6 characters"
|
||||
className="pl-10 pr-10"
|
||||
{...register('password')}
|
||||
disabled={loading}
|
||||
/>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
|
||||
onClick={() => setShowPassword(!showPassword)}
|
||||
disabled={loading}
|
||||
>
|
||||
{showPassword ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
|
||||
<span className="sr-only">{showPassword ? 'Hide password' : 'Show password'}</span>
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Password strength indicators */}
|
||||
{watchPassword && (
|
||||
<div className="space-y-2 mt-2">
|
||||
<div className="flex items-center space-x-2 text-sm">
|
||||
{passwordRequirements.minLength ? (
|
||||
<Check className="h-3 w-3 text-green-600" />
|
||||
) : (
|
||||
<X className="h-3 w-3 text-red-500" />
|
||||
)}
|
||||
<span
|
||||
className={passwordRequirements.minLength ? 'text-green-600' : 'text-red-500'}
|
||||
>
|
||||
Minimum 6 characters
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex items-center space-x-2 text-sm">
|
||||
{passwordRequirements.hasUppercase ? (
|
||||
<Check className="h-3 w-3 text-green-600" />
|
||||
) : (
|
||||
<X className="h-3 w-3 text-red-500" />
|
||||
)}
|
||||
<span
|
||||
className={
|
||||
passwordRequirements.hasUppercase ? 'text-green-600' : 'text-red-500'
|
||||
}
|
||||
>
|
||||
One uppercase letter
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex items-center space-x-2 text-sm">
|
||||
{passwordRequirements.hasNumberOrSpecial ? (
|
||||
<Check className="h-3 w-3 text-green-600" />
|
||||
) : (
|
||||
<X className="h-3 w-3 text-red-500" />
|
||||
)}
|
||||
<span
|
||||
className={
|
||||
passwordRequirements.hasNumberOrSpecial ? 'text-green-600' : 'text-red-500'
|
||||
}
|
||||
>
|
||||
One number or special character
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{errors.password && <p className="text-sm text-red-600">{errors.password.message}</p>}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="confirmPassword">Confirm Password</Label>
|
||||
<div className="relative">
|
||||
<Lock className="absolute left-3 top-3 h-4 w-4 text-muted-foreground" />
|
||||
<Input
|
||||
id="confirmPassword"
|
||||
type={showConfirmPassword ? 'text' : 'password'}
|
||||
placeholder="Confirm your password"
|
||||
className="pl-10 pr-10"
|
||||
{...register('confirmPassword')}
|
||||
disabled={loading}
|
||||
/>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
|
||||
onClick={() => setShowConfirmPassword(!showConfirmPassword)}
|
||||
disabled={loading}
|
||||
>
|
||||
{showConfirmPassword ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
|
||||
<span className="sr-only">
|
||||
{showConfirmPassword ? 'Hide password' : 'Show password'}
|
||||
</span>
|
||||
</Button>
|
||||
</div>
|
||||
{errors.confirmPassword && (
|
||||
<p className="text-sm text-red-600">{errors.confirmPassword.message}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="flex items-center space-x-2">
|
||||
<Checkbox
|
||||
id="agreeTerms"
|
||||
checked={getValues('agreeTerms') || false}
|
||||
onCheckedChange={(checked) => {
|
||||
setValue('agreeTerms', checked as boolean)
|
||||
trigger('agreeTerms')
|
||||
}}
|
||||
/>
|
||||
<Label htmlFor="agreeTerms" className="text-sm font-normal">
|
||||
I agree to the{' '}
|
||||
<Link href="/terms" className="text-primary hover:underline">
|
||||
Terms of Service
|
||||
</Link>{' '}
|
||||
and{' '}
|
||||
<Link href="/privacy" className="text-primary hover:underline">
|
||||
Privacy Policy
|
||||
</Link>
|
||||
</Label>
|
||||
</div>
|
||||
{errors.agreeTerms && <p className="text-sm text-red-600">{errors.agreeTerms.message}</p>}
|
||||
</CardContent>
|
||||
<CardFooter className="flex flex-col space-y-4">
|
||||
<Button type="submit" className="w-full" disabled={loading}>
|
||||
{loading ? 'Creating Account...' : 'Create Account'}
|
||||
</Button>
|
||||
|
||||
<div className="relative">
|
||||
<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>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<GoogleSignInButton disabled={loading} />
|
||||
<GitHubSignInButton disabled={loading} />
|
||||
</div>
|
||||
</CardFooter>
|
||||
</form>
|
||||
|
||||
<div className="text-center text-sm text-muted-foreground mt-4">
|
||||
Already have an account?{' '}
|
||||
{onSwitchToLogin ? (
|
||||
<button onClick={onSwitchToLogin} className="text-primary hover:underline cursor-pointer">
|
||||
Sign in
|
||||
</button>
|
||||
) : (
|
||||
<span className="text-primary">Sign in</span>
|
||||
)}
|
||||
</div>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
43
components/auth/RequireAuth.tsx
Normal file
43
components/auth/RequireAuth.tsx
Normal file
@@ -0,0 +1,43 @@
|
||||
'use client'
|
||||
import { useEffect } from 'react'
|
||||
import { useAuth } from '@/contexts/AuthContext'
|
||||
import { useRouter } from 'next/navigation'
|
||||
|
||||
interface RequireAuthProps {
|
||||
children: React.ReactNode
|
||||
redirectTo?: string
|
||||
}
|
||||
|
||||
export function RequireAuth({ children, redirectTo = '/auth' }: RequireAuthProps) {
|
||||
const { user, loading, checkAuth } = useAuth()
|
||||
const router = useRouter()
|
||||
|
||||
useEffect(() => {
|
||||
// Trigger auth check when component mounts
|
||||
checkAuth()
|
||||
}, [checkAuth])
|
||||
|
||||
useEffect(() => {
|
||||
// Redirect if not authenticated after loading
|
||||
if (!loading && !user) {
|
||||
// Store the current path as the return URL
|
||||
const currentPath = window.location.pathname + window.location.search
|
||||
const returnUrl = encodeURIComponent(currentPath)
|
||||
router.push(`${redirectTo}?returnUrl=${returnUrl}`)
|
||||
}
|
||||
}, [user, loading, router, redirectTo])
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="flex items-center justify-center min-h-[400px]">
|
||||
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary"></div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
return null
|
||||
}
|
||||
|
||||
return <>{children}</>
|
||||
}
|
||||
Reference in New Issue
Block a user