113 lines
3.5 KiB
TypeScript
113 lines
3.5 KiB
TypeScript
import { usePasskeyRegister } from '@laravel/passkeys/react';
|
|
import { useState } from 'react';
|
|
import InputError from '@/components/input-error';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Label } from '@/components/ui/label';
|
|
|
|
type Props = {
|
|
onSuccess: () => void;
|
|
};
|
|
|
|
export default function PasskeyRegistration({ onSuccess }: Props) {
|
|
const [name, setName] = useState(() => {
|
|
const ua = navigator.userAgent;
|
|
|
|
const browser = [
|
|
{ pattern: /Edg|Edge/, name: 'Edge' },
|
|
{ pattern: /OPR|Opera|OPiOS/, name: 'Opera' },
|
|
{ pattern: /Firefox|FxiOS/, name: 'Firefox' },
|
|
{ pattern: /Chrome|CriOS/, name: 'Chrome' },
|
|
{ pattern: /Safari/, name: 'Safari' },
|
|
].find(({ pattern }) => pattern.test(ua))?.name;
|
|
|
|
const os = [
|
|
{ pattern: /iPhone/, name: 'iPhone' },
|
|
{ pattern: /iPad|Macintosh(?=.*Mobile)/, name: 'iPad' },
|
|
{ pattern: /Android/, name: 'Android' },
|
|
{ pattern: /Mac/, name: 'Mac' },
|
|
{ pattern: /Windows/, name: 'Windows' },
|
|
].find(({ pattern }) => pattern.test(ua))?.name;
|
|
|
|
return [browser, os].filter(Boolean).join(' on ') || '';
|
|
});
|
|
|
|
const [showForm, setShowForm] = useState(false);
|
|
const { register, isLoading, error, isSupported } = usePasskeyRegister({
|
|
onSuccess: () => {
|
|
setName('');
|
|
setShowForm(false);
|
|
onSuccess();
|
|
},
|
|
});
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
|
|
if (!name.trim()) {
|
|
return;
|
|
}
|
|
|
|
await register(name);
|
|
};
|
|
|
|
const handleCancel = () => {
|
|
setShowForm(false);
|
|
setName('');
|
|
};
|
|
|
|
if (!isSupported) {
|
|
return (
|
|
<div className="text-muted-foreground text-sm">
|
|
{' '}
|
|
Este navegador não suporta chaves de acesso.{' '}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (!showForm) {
|
|
return (
|
|
<Button variant="outline" onClick={() => setShowForm(true)}>
|
|
{' '}
|
|
Adicionar chave de acesso{' '}
|
|
</Button>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<form
|
|
onSubmit={handleSubmit}
|
|
className="border-border bg-muted/50 space-y-4 rounded-lg border p-4"
|
|
>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="passkey-name"> Nome da chave de acesso </Label>
|
|
<Input
|
|
id="passkey-name"
|
|
type="text"
|
|
value={name}
|
|
onChange={(e) => setName(e.target.value)}
|
|
placeholder="Por exemplo, MacBook Pro ou iPhone"
|
|
className="border-foreground/20 mt-1 block w-full"
|
|
autoFocus
|
|
/>
|
|
<p className="text-muted-foreground text-xs">
|
|
{' '}
|
|
O nome permite identificar esta chave mais tarde.{' '}
|
|
</p>
|
|
</div>
|
|
|
|
{error && <InputError message={error} />}
|
|
|
|
<div className="flex gap-2">
|
|
<Button type="submit" disabled={isLoading || !name.trim()}>
|
|
{isLoading ? 'Registering...' : 'Registar chave de acesso'}
|
|
</Button>
|
|
<Button type="button" variant="ghost" onClick={handleCancel}>
|
|
{' '}
|
|
Cancelar{' '}
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
);
|
|
}
|