-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
417 additions
and
275 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { NextRequest, NextResponse } from 'next/server' | ||
import type { ChangePasswordSchema } from '@/(auth)/change-password/schema' | ||
import { createClient } from '@/(auth)/supabase/with-request' | ||
|
||
const assertVerifyOldPassword = async ( | ||
supabase: ReturnType<typeof createClient>['supabase'], | ||
data: ChangePasswordSchema | ||
): Promise< | ||
| { kind: 'verified' } | ||
| { kind: 'noSession'; message: string } | ||
| { kind: 'wrongPassword'; message: string } | ||
> => { | ||
const { | ||
data: { session }, | ||
error | ||
} = await supabase.auth.getSession() | ||
if (error || !session?.user.email) { | ||
return { kind: 'noSession', message: 'user session is not detected' } | ||
} | ||
|
||
const { | ||
error: signInError, | ||
data: { session: signInSession } | ||
} = await supabase.auth.signInWithPassword({ | ||
email: session.user.email, | ||
password: data.oldPassword | ||
}) | ||
if (signInError || !signInSession) { | ||
return { kind: 'wrongPassword', message: 'Old Password is not correct' } | ||
} | ||
return { kind: 'verified' } | ||
} | ||
|
||
export const POST = async (req: NextRequest): Promise<NextResponse> => { | ||
const { supabase } = createClient(req) | ||
|
||
const data = (await req.json()) as ChangePasswordSchema | ||
const res = await assertVerifyOldPassword(supabase, data) | ||
if (res.kind !== 'verified') { | ||
return NextResponse.json(res.message, { status: 401 }) | ||
} | ||
|
||
const { error } = await supabase.auth.updateUser({ | ||
password: data.newPassword | ||
}) | ||
if (error) { | ||
return NextResponse.json(error.message, { status: 500 }) | ||
} else { | ||
return NextResponse.json('ok', { status: 200 }) | ||
} | ||
} |
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,100 @@ | ||
'use client' | ||
import { useState } from 'react' | ||
import { useForm } from 'react-hook-form' | ||
import { | ||
Box, | ||
Flex, | ||
FormLabel, | ||
FormErrorMessage, | ||
FormControl, | ||
Input, | ||
Heading, | ||
useToast, | ||
useBoolean | ||
} from '@chakra-ui/react' | ||
import { useRouter } from 'next/navigation' | ||
import { | ||
changePasswordResolver, | ||
ChangePasswordSchema | ||
} from '@/(auth)/change-password/schema' | ||
import { PrimaryButton } from '@/components/button' | ||
|
||
export default function ChangePassword() { | ||
const toast = useToast() | ||
const [isLoading, setIsLoading] = useBoolean() | ||
const [toastId, setToastId] = useState<ReturnType<typeof toast> | undefined>( | ||
undefined | ||
) | ||
const router = useRouter() | ||
|
||
const { | ||
register, | ||
handleSubmit, | ||
formState: { errors } | ||
} = useForm<ChangePasswordSchema>({ | ||
resolver: changePasswordResolver | ||
}) | ||
|
||
const changePasswordHandler = handleSubmit( | ||
async (data: ChangePasswordSchema) => { | ||
setIsLoading.on() | ||
const response = await fetch('/change-password/action', { | ||
method: 'POST', | ||
body: JSON.stringify(data) | ||
}) | ||
setIsLoading.off() | ||
if (response.ok) { | ||
router.push('/') | ||
} else if (!toastId) { | ||
const res = toast({ | ||
title: "We're sorry, but you failed to change your password.", | ||
description: await response.json(), | ||
status: 'error', | ||
duration: 5000, | ||
isClosable: true, | ||
position: 'top', | ||
onCloseComplete() { | ||
setToastId(undefined) | ||
} | ||
}) | ||
setToastId(res) | ||
} | ||
} | ||
) | ||
|
||
return ( | ||
<> | ||
<Heading>Change Password</Heading> | ||
<Box as="form" onSubmit={changePasswordHandler}> | ||
<Flex flexDirection={'column'}> | ||
<FormControl isInvalid={!!errors.oldPassword}> | ||
<FormLabel>Old Password</FormLabel> | ||
<Input type="password" {...register('oldPassword')} /> | ||
{errors.oldPassword && ( | ||
<FormErrorMessage>{errors.oldPassword.message}</FormErrorMessage> | ||
)} | ||
</FormControl> | ||
<FormControl isInvalid={!!errors.newPassword}> | ||
<FormLabel>New Password</FormLabel> | ||
<Input type="password" {...register('newPassword')} /> | ||
{errors.newPassword && ( | ||
<FormErrorMessage>{errors.newPassword.message}</FormErrorMessage> | ||
)} | ||
</FormControl> | ||
<FormControl isInvalid={!!errors.confirmNewPassword}> | ||
<FormLabel>Confirm New Password</FormLabel> | ||
<Input type="password" {...register('confirmNewPassword')} /> | ||
{errors.confirmNewPassword && ( | ||
<FormErrorMessage> | ||
{errors.confirmNewPassword.message} | ||
</FormErrorMessage> | ||
)} | ||
</FormControl> | ||
<PrimaryButton isLoading={isLoading} type={'submit'}> | ||
Change Password | ||
</PrimaryButton> | ||
</Flex> | ||
</Box> | ||
</> | ||
) | ||
} |
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,25 @@ | ||
import { zodResolver } from '@hookform/resolvers/zod' | ||
import * as z from 'zod' | ||
|
||
const changePasswordSchema = z | ||
.object({ | ||
// No need for daring to notify users old password policy in terms of security enhancement. | ||
oldPassword: z.string().min(1, { message: 'password is required' }), | ||
newPassword: z | ||
.string() | ||
.min(8, { message: 'Password must contain at least 8 character(s)' }), | ||
confirmNewPassword: z.string().min(8, { | ||
message: 'Password must contain at least 8 character(s)' | ||
}) | ||
}) | ||
.refine((schema) => schema.newPassword === schema.confirmNewPassword, { | ||
path: ['confirmNewPassword'], | ||
message: 'Your password and confirmation password must match' | ||
}) | ||
.refine((schema) => schema.oldPassword !== schema.newPassword, { | ||
path: ['confirmNewPassword'], | ||
message: 'Old password and new password must be different' | ||
}) | ||
|
||
export type ChangePasswordSchema = z.infer<typeof changePasswordSchema> | ||
export const changePasswordResolver = zodResolver(changePasswordSchema) |
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
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
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,28 +1,23 @@ | ||
'use client' | ||
|
||
import { Box, Container, Heading, useColorModeValue } from '@chakra-ui/react' | ||
import { Header, Footer } from '@/components/navigation' | ||
import { FormActivity } from './components' | ||
|
||
export default function CreateActivityPage() { | ||
const bg = useColorModeValue('white', 'gray.800') | ||
const color = useColorModeValue('black', 'gray.300') | ||
|
||
return ( | ||
<> | ||
<Box as="main" minH="100vh" bg={bg} color={color}> | ||
<Header /> | ||
<Container | ||
pt={{ base: '20px', md: '40px' }} | ||
pb={{ base: '40px', md: '70px' }} | ||
> | ||
<Heading as={'h1'} fontSize={{ base: '2xl', md: '4xl' }}> | ||
Create Activity | ||
</Heading> | ||
<FormActivity /> | ||
</Container> | ||
<Footer /> | ||
</Box> | ||
</> | ||
<Box as="main" minH="100vh" bg={bg} color={color}> | ||
<Container | ||
pt={{ base: '20px', md: '40px' }} | ||
pb={{ base: '40px', md: '70px' }} | ||
> | ||
<Heading as={'h1'} fontSize={{ base: '2xl', md: '4xl' }}> | ||
Create Activity | ||
</Heading> | ||
<FormActivity /> | ||
</Container> | ||
</Box> | ||
) | ||
} |
Oops, something went wrong.