Skip to content

Commit 07d2cc4

Browse files
Merge pull request #39 from m4sc0/9-ask-user-for-name-on-signup
Ask user for name on signup
2 parents 864253b + e9bb91a commit 07d2cc4

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

app/(config-wrapper)/settings/account/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export default function AccountSettingsPage() {
116116
<div className="content grid grid-cols-[auto_1fr_auto] font-medium gap-2 items-center">
117117
<section className="frosted flex rounded-lg justify-center col-span-full p-2 items-center gap-6">
118118
<FontAwesomeIcon icon={faCircleUser} className="text-4xl" />
119-
<span>Lorem, ipsum.</span>
119+
<span>{JSON.parse(localStorage.getItem('pb_user') ?? "").name ?? 'Lorem ipsum'}</span>
120120
</section>
121121

122122
<h2 className="text-xl col-span-full">Authentication</h2>

app/api/v1/auth/signup/route.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { promises as fs } from 'fs';
55

66
export async function POST(request: Request) {
77
try {
8-
const { email, password, passwordConfirm } = await request.json();
8+
const { name, email, password, passwordConfirm } = await request.json();
99

1010
if (!email || !password || !passwordConfirm) {
1111
return new NextResponse(JSON.stringify({ error: 'All fields are required' }), {
@@ -21,10 +21,26 @@ export async function POST(request: Request) {
2121
});
2222
}
2323

24+
// using localpart of email as fallback
25+
let newName;
26+
if (name === "" || !name && typeof email === "string") {
27+
const localPart = email.split("@")[0];
28+
// a little transformation can't be missing
29+
newName = localPart
30+
.replace(/[._-]+/g, ' ')
31+
.trim()
32+
.split(' ')
33+
.map(w => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()) // capitalize
34+
.join(' ');
35+
} else {
36+
newName = name;
37+
}
38+
2439
const pb = getServerPB();
2540

2641
// 1. Create the user
2742
const user = await pb.collection('users').create({
43+
newName,
2844
email,
2945
password,
3046
passwordConfirm,

components/auth/SignupForm.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { faCircleCheck, faExclamationTriangle } from "@fortawesome/free-solid-sv
1313
import { setTimeout } from "timers"
1414

1515
export default function SignupCard() {
16+
const [name, setName] = useState("");
1617
const [email, setEmail] = useState("")
1718
const [password, setPassword] = useState("")
1819
const [confirmPassword, setConfirmPassword] = useState("")
@@ -46,6 +47,7 @@ export default function SignupCard() {
4647
method: "POST",
4748
headers: { "Content-Type": "application/json" },
4849
body: JSON.stringify({
50+
name,
4951
email,
5052
password,
5153
passwordConfirm: confirmPassword,
@@ -61,6 +63,7 @@ export default function SignupCard() {
6163

6264
setTimeout(() => {
6365
// Clear the form fields
66+
setName("")
6467
setEmail("")
6568
setPassword("")
6669
setConfirmPassword("")
@@ -106,6 +109,16 @@ export default function SignupCard() {
106109
</Alert>
107110
)}
108111

112+
<div className="grid gap-2">
113+
<Label htmlFor="name">Name</Label>
114+
<Input
115+
id="name"
116+
type="text"
117+
placeholder="John Doe"
118+
value={name}
119+
onChange={(e) => setName(e.target.value)}
120+
/>
121+
</div>
109122
<div className="grid gap-2">
110123
<Label htmlFor="email">Email</Label>
111124
<Input

0 commit comments

Comments
 (0)