-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Auth page when user login #107
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b9ef96d
feat: convert `forgot-password` `join` `signin` to rsc
jsun969 a6ce115
fix: add clerk fallback url
jsun969 518ad4d
fix: check user before `signin` / `join`
jsun969 e0ed6a3
chore(env): add comment for clerk redundant urls
jsun969 b8afd2b
refactor(settings): use `authRoutes`
jsun969 691f045
chore: Update comment on Clerk envs
rayokamoto b957be7
style: Run prettier
rayokamoto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
'use client'; | ||
|
||
import Button from '@/components/Button'; | ||
import ControlledField from '@/components/ControlledField'; | ||
import FancyRectangle from '@/components/FancyRectangle'; | ||
import { useSignIn } from '@clerk/nextjs'; | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { useState } from 'react'; | ||
import { useForm } from 'react-hook-form'; | ||
import { z } from 'zod'; | ||
import { handleClerkErrors } from '../helpers'; | ||
import { codeSchema, emailSchema, passwordSchema } from '../schemas'; | ||
|
||
const sendCodeSchema = z.object({ | ||
email: emailSchema, | ||
}); | ||
const resetPasswordSchema = z | ||
.object({ | ||
code: codeSchema, | ||
password: passwordSchema, | ||
confirmPassword: z.string().min(1, { message: 'Please confirm password' }), | ||
}) | ||
.refine((data) => data.password === data.confirmPassword, { | ||
message: 'Passwords do not match', | ||
path: ['confirmPassword'], | ||
}); | ||
|
||
const STEP_INSTRUCTIONS = [ | ||
'', // Step start from 1 | ||
'Enter your email to receive a reset code.', | ||
'Enter your new password and the code received in your email.', | ||
'Password reset complete!', | ||
] as const; | ||
|
||
export default function ForgotPassword() { | ||
const [step, setStep] = useState(1); | ||
const { isLoaded, signIn, setActive } = useSignIn(); | ||
|
||
const sendCodeForm = useForm<z.infer<typeof sendCodeSchema>>({ | ||
defaultValues: { email: '' }, | ||
resolver: zodResolver(sendCodeSchema), | ||
}); | ||
const resetPasswordForm = useForm<z.infer<typeof resetPasswordSchema>>({ | ||
defaultValues: { code: '', password: '' }, | ||
resolver: zodResolver(resetPasswordSchema), | ||
}); | ||
|
||
const [sendCodeLoading, setSendCodeLoading] = useState(false); | ||
const [resetPasswordLoading, setResetPasswordLoading] = useState(false); | ||
|
||
const handleSendCode = sendCodeForm.handleSubmit(async ({ email }) => { | ||
if (!isLoaded) return; | ||
|
||
setSendCodeLoading(true); | ||
|
||
try { | ||
const result = await signIn.create({ | ||
strategy: 'reset_password_email_code', | ||
identifier: email, | ||
}); | ||
if (result) { | ||
setStep(2); | ||
} | ||
} catch (error) { | ||
handleClerkErrors(error, sendCodeForm, [ | ||
{ | ||
code: 'form_identifier_not_found', | ||
field: 'email', | ||
message: | ||
"Couldn't find account with given email. Please create an account first.", | ||
}, | ||
{ | ||
code: 'form_conditional_param_value_disallowed', | ||
field: 'email', | ||
message: 'Account was created through Google. Please sign in using Google.', | ||
}, | ||
]); | ||
} | ||
|
||
setSendCodeLoading(false); | ||
}); | ||
|
||
const handleResetPassword = resetPasswordForm.handleSubmit(async ({ code, password }) => { | ||
if (!isLoaded) return; | ||
|
||
setResetPasswordLoading(true); | ||
|
||
try { | ||
const resetResult = await signIn.attemptFirstFactor({ | ||
strategy: 'reset_password_email_code', | ||
code, | ||
password, | ||
}); | ||
if (resetResult.status === 'complete') { | ||
setActive({ session: resetResult.createdSessionId }); | ||
setStep(3); | ||
} | ||
} catch (error) { | ||
handleClerkErrors(error, resetPasswordForm, [ | ||
{ | ||
code: 'form_password_not_strong_enough', | ||
field: 'password', | ||
message: | ||
'Given password is not strong enough. For account safety, please use a different password.', | ||
}, | ||
{ | ||
code: 'form_password_not_strong_enough', | ||
field: 'password', | ||
message: | ||
'Password has been found in an online data breach. For account safety, please use a different password.', | ||
}, | ||
{ | ||
code: 'form_code_incorrect', | ||
field: 'code', | ||
message: 'Incorrect Code. Please enter the code from your email.', | ||
}, | ||
]); | ||
} | ||
|
||
setResetPasswordLoading(false); | ||
}); | ||
|
||
return ( | ||
<section className="w-full max-w-lg"> | ||
<FancyRectangle colour="purple" offset="8" filled fullWidth> | ||
<div className="z-0 w-full border-4 border-black bg-white p-8 text-black md:p-12"> | ||
<h3 className="text-3xl font-bold">Forgot Your Password?</h3> | ||
<p className="mb-8 text-xl">{STEP_INSTRUCTIONS[step]}</p> | ||
{step === 1 && ( | ||
<form onSubmit={handleSendCode}> | ||
<ControlledField | ||
label="Email" | ||
control={sendCodeForm.control} | ||
name="email" | ||
/> | ||
<Button | ||
colour="orange" | ||
width="w-[19rem] md:w-[25rem]" | ||
type="submit" | ||
loading={sendCodeLoading} | ||
> | ||
Send Code | ||
</Button> | ||
</form> | ||
)} | ||
|
||
{step === 2 && ( | ||
<form onSubmit={handleResetPassword}> | ||
<ControlledField | ||
label="New password" | ||
type="password" | ||
control={resetPasswordForm.control} | ||
name="password" | ||
/> | ||
<ControlledField | ||
label="Confirm password" | ||
type="password" | ||
control={resetPasswordForm.control} | ||
name="confirmPassword" | ||
/> | ||
<ControlledField | ||
label="Reset code" | ||
control={resetPasswordForm.control} | ||
name="code" | ||
/> | ||
<Button | ||
colour="orange" | ||
width="w-[19rem] md:w-[25rem]" | ||
type="submit" | ||
loading={resetPasswordLoading} | ||
> | ||
Reset Password | ||
</Button> | ||
</form> | ||
)} | ||
|
||
{step === 3 && ( | ||
<Button href="/" colour="orange" width="w-[19rem] md:w-[25rem]"> | ||
Return to Home Page | ||
</Button> | ||
)} | ||
</div> | ||
</FancyRectangle> | ||
</section> | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want this to be the home page or should it be
/settings
in the membership tab so that people have direct access to pay?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aftersignin here is not the main logic. if you want this, we have to do it code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is for, like google login, redirect url
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, okay