-
Notifications
You must be signed in to change notification settings - Fork 7
feat: integrate other auth forgot-password reset-password fix bug auth #18
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e6f6970
feat: forget and reset password
masternonnolnw a2fdba4
feat: complete forgot and reset password
masternonnolnw ba6443e
feat: delete when reset password success
masternonnolnw 6eab8fd
feat: verification delete
masternonnolnw e9fe667
feat: toast and toast message for all auth
masternonnolnw 7dc9d15
feat: add token check
masternonnolnw edccc5a
featL complete CR
masternonnolnw 5d44e01
fix: remove and move valiate token to server
masternonnolnw bba35e1
feat: error handler server
masternonnolnw a3a3eeb
Merge branch 'main' into non/auth2
masternonnolnw cc464fd
Merge branch 'main' into non/auth2
masternonnolnw 02245a0
feat: bug auth page
masternonnolnw d9d9714
feat: move forgot and reset password to react
masternonnolnw f2c5246
feat: removeunused page
masternonnolnw da9780f
chore: remove console log
masternonnolnw 3397a4f
feat: fix error
masternonnolnw 11a3081
chore: remove unused
masternonnolnw 527b863
Merge branch 'main' into non/auth2
masternonnolnw 5289af4
fix: ci
saenyakorn aeab5ea
Create light-tigers-doubt.md
saenyakorn 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
87 changes: 87 additions & 0 deletions
87
packages/core/src/auth/handlers/send-email-reset-password.ts
This file contains hidden or 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,87 @@ | ||
| import z from 'zod' | ||
|
|
||
| import { type ApiRouteHandler, type ApiRouteSchema, createEndpoint } from '../../endpoint' | ||
| import { type AuthContext } from '../context' | ||
|
|
||
| interface InternalRouteOptions { | ||
| prefix?: string | ||
| } | ||
|
|
||
| export function sendEmailResetPassword<const TOptions extends InternalRouteOptions>( | ||
| options: TOptions | ||
| ) { | ||
| const schema = { | ||
| method: 'POST', | ||
| path: '/api/auth/send-otp-forgot-password', | ||
| body: z.object({ | ||
| email: z.string(), | ||
| }), | ||
| responses: { | ||
| 200: z.object({ | ||
| status: z.string(), | ||
| }), | ||
| 400: z.object({ | ||
| status: z.string(), | ||
| }), | ||
| }, | ||
| } as const satisfies ApiRouteSchema | ||
|
|
||
| const handler: ApiRouteHandler<AuthContext, typeof schema> = async (args) => { | ||
| if (!args.context.authConfig.resetPassword?.enabled) { | ||
| // TODO: Log not enabled | ||
| return { | ||
| status: 400, | ||
| body: { status: 'reset password not enabled' }, | ||
| } | ||
| } | ||
| let user | ||
| try { | ||
| user = await args.context.internalHandlers.user.findByEmail(args.body.email) | ||
| } catch { | ||
| return { | ||
| status: 400, | ||
| body: { status: 'user not found' }, | ||
| } | ||
| } | ||
|
|
||
| // Generate a secure random token | ||
| const token = crypto.randomUUID() | ||
| const identifier = `reset-password:${token}` | ||
| await args.context.internalHandlers.verification.deleteByUserIdAndIdentifierPrefix( | ||
| user.id, | ||
| 'reset-password:' | ||
| ) | ||
|
|
||
| await args.context.internalHandlers.verification.create({ | ||
| identifier, | ||
| value: user.id, | ||
| expiresAt: new Date( | ||
| Date.now() + (args.context.authConfig.resetPassword?.expiresInMs ?? 1000 * 60 * 60 * 24) // TODO; make config always set default | ||
| ), | ||
| }) | ||
|
|
||
| // TODO: change this to domain config websiteURL | ||
| const resetPasswordLink = `${args.context.authConfig.resetPassword?.resetPasswordUrl ?? 'http://localhost:3000/admin/auth/reset-password'}?token=${token}` | ||
| // Send email | ||
| if (args.context.authConfig.resetPassword?.sendEmailResetPassword) { | ||
| await args.context.authConfig.resetPassword.sendEmailResetPassword( | ||
| user.email, | ||
| resetPasswordLink | ||
| ) | ||
| } else { | ||
| // Fallback to console log for development | ||
| console.warn( | ||
| 'No "auth.resetPassword.sendEmailResetPassword" function provided, using console.log for development purposes.' | ||
| ) | ||
| } | ||
|
|
||
| return { | ||
| status: 200, | ||
| body: { | ||
| status: 'ok', | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| return createEndpoint(schema, handler) | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,30 @@ | ||
| 'use client' | ||
|
|
||
| import { Toaster as ToasterPrimitive, type ToasterProps } from 'sonner' | ||
|
|
||
| import { useTheme } from '../theme-provider' | ||
|
|
||
| const Toast = ({ ...props }: ToasterProps) => { | ||
| const { theme = 'system' } = useTheme() | ||
| return ( | ||
| <ToasterPrimitive | ||
| theme={theme as ToasterProps['theme']} | ||
| className="toaster group" | ||
| richColors | ||
| toastOptions={{ | ||
| classNames: { | ||
| toast: 'toast border-0! inset-ring! inset-ring-fg/10!', | ||
| title: 'title', | ||
| description: 'description', | ||
| actionButton: 'bg-primary! hover:bg-primary/90! text-primary-fg!', | ||
| cancelButton: 'bg-transparent! hover:bg-secondary! hover:text-secondary-fg!', | ||
| closeButton: 'close-button', | ||
| }, | ||
| }} | ||
| {...props} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| export type { ToasterProps } | ||
| export { Toast } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.