Skip to content

Commit

Permalink
feature: sign up flow
Browse files Browse the repository at this point in the history
  • Loading branch information
alifhaider committed Nov 28, 2024
1 parent 6fc3220 commit 8bc4b30
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions app/routes/signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ import {
CardHeader,
CardTitle,
} from '~/components/ui/card'
import { authSessionStorage } from '~/services/session.server'

const SignupFormSchema = z
.object({
username: UsernameSchema,
email: EmailSchema,
fullname: z.string().optional(),
password: PasswordSchema,
confirmPassword: ConfirmPasswordSchema,
agreeToTermsOfServiceAndPrivacyPolicy: z
Expand All @@ -58,15 +60,30 @@ export async function action({ request }: ActionFunctionArgs) {
// checkHoneypot(formData)
const submission = await parseWithZod(formData, {
schema: SignupFormSchema.superRefine(async (data, ctx) => {
const existingUser = await prisma.user.findUnique({
where: { username: data.username },
const existingEmail = await prisma.user.findFirst({
where: { email: data.email.toLowerCase() },
select: { id: true },
})
if (existingUser) {

if (existingEmail) {
ctx.addIssue({
path: ['email'],
code: z.ZodIssueCode.custom,
message: 'A user already exists with this email',
})
return
}

const existingUsername = await prisma.user.findFirst({
where: { username: data.username.toLowerCase() },
select: { id: true },
})

if (existingUsername) {
ctx.addIssue({
path: ['username'],
code: z.ZodIssueCode.custom,
message: 'A user already exists with this username',
message: 'A user already exists with this username ',
})
return
}
Expand Down Expand Up @@ -97,14 +114,14 @@ export async function action({ request }: ActionFunctionArgs) {

const { user, remember } = submission.value

const cookieSession = await sessionStorage.getSession(
const cookieSession = await authSessionStorage.getSession(
request.headers.get('cookie'),
)
cookieSession.set('userId', user.id)

return redirect('/', {
headers: {
'set-cookie': await sessionStorage.commitSession(cookieSession, {
'set-cookie': await authSessionStorage.commitSession(cookieSession, {
expires: remember === 'true' ? getSessionExpirationDate() : undefined,
}),
},
Expand Down Expand Up @@ -145,7 +162,7 @@ export default function SignupRoute() {
...getInputProps(fields.email, { type: 'email' }),
autoComplete: 'email',
autoFocus: true,
className: 'lowercase',
className: 'lowercase placeholder:capitalize',
}}
errors={fields.email.errors}
/>
Expand All @@ -154,11 +171,23 @@ export default function SignupRoute() {
inputProps={{
...getInputProps(fields.username, { type: 'text' }),
autoComplete: 'username',
className: 'lowercase',
className: 'lowercase placeholder:capitalize',
}}
errors={fields.username.errors}
/>

<Field
labelProps={{
htmlFor: fields.fullname.id,
children: 'Full Name',
}}
inputProps={{
...getInputProps(fields.fullname, { type: 'text' }),
autoComplete: 'name',
}}
errors={fields.fullname.errors}
/>

<Field
labelProps={{ htmlFor: fields.password.id, children: 'Password' }}
inputProps={{
Expand All @@ -175,8 +204,6 @@ export default function SignupRoute() {
}}
inputProps={{
...getInputProps(fields.confirmPassword, { type: 'password' }),

autoComplete: 'new-password',
}}
errors={fields.confirmPassword.errors}
/>
Expand Down Expand Up @@ -224,7 +251,7 @@ export default function SignupRoute() {
<CardFooter>
<p className="mt-4 text-sm">
Already have an account?{' '}
<Link to="/login" className="text-cyan-400 hover:underline">
<Link to="/login" className="text-cyan-400 underline">
Log in
</Link>
</p>
Expand Down
Binary file modified prisma/dev.db
Binary file not shown.

0 comments on commit 8bc4b30

Please sign in to comment.