Skip to content
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

feat/authentication #2

Merged
merged 37 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
3e66745
wip: create auth wrapper component
Saba-Var Sep 30, 2022
f2e2534
markup: build log in page
Saba-Var Sep 30, 2022
d4a18e8
chore: translate log in page
Saba-Var Sep 30, 2022
ece7684
chore: move log in form into separate component
Saba-Var Sep 30, 2022
a3a5c4a
chore: change hero section responsive design
Saba-Var Oct 1, 2022
347794f
markup: build ui of sign up page
Saba-Var Oct 1, 2022
b83a5f7
styles: change language selector visibility
Saba-Var Oct 3, 2022
651ef40
chore: remove unused files from Authentication folder
Saba-Var Oct 3, 2022
50a6e60
chore: change page component folder names
Saba-Var Oct 3, 2022
9454c7c
feat: validate sign up page with formik
Saba-Var Oct 3, 2022
a4efad1
chore: change yup schema name of sign up page
Saba-Var Oct 4, 2022
2e8d565
chore: create log in form validation schema
Saba-Var Oct 4, 2022
1b0a092
feat: validate log in page
Saba-Var Oct 4, 2022
490ac2c
chore: change custom hooks file extension
Saba-Var Oct 4, 2022
9822e26
chore: create custom hook for sign up form
Saba-Var Oct 4, 2022
397890b
feat: add arrow icon to navigate to home page
Saba-Var Oct 4, 2022
cc7ed76
feat: integrate react-query and create axios instance
Saba-Var Oct 14, 2022
d7680ee
feat: register new users and show rate limit exceed modal
Saba-Var Oct 14, 2022
9375828
chore: use react query for registering user
Saba-Var Oct 14, 2022
f5de346
feat: show success modal after registration
Saba-Var Oct 14, 2022
88362bc
feat: redirect users to request exceed page after 429 status code
Saba-Var Oct 15, 2022
7c5506b
chore: remove google authentication stuff
Saba-Var Oct 15, 2022
c20844f
styles: change indigo color with blue
Saba-Var Oct 15, 2022
2b17e49
chore: change styling of authentication forms
Saba-Var Oct 16, 2022
405ea56
chore: replace custom icons with heroicons
Saba-Var Oct 16, 2022
0493bc8
feat: submit sign in form and show errors
Saba-Var Oct 16, 2022
baae156
feat: create private axios instance and integrate redux toolkit
Saba-Var Oct 16, 2022
f4b720b
refactor: add response interceptor to axios instance for 429 status
Saba-Var Oct 18, 2022
c3234ec
feat: set cookie after sign in according to checkbox value
Saba-Var Oct 18, 2022
ac0e6fc
feat: create account activation page
Saba-Var Oct 19, 2022
13f216e
chore: rename variable of jwt
Saba-Var Oct 22, 2022
a7a6d29
feat: show account activation page content conditionally
Saba-Var Oct 23, 2022
17ad899
chore: check for 404 status code during activation
Saba-Var Oct 23, 2022
c5051e8
feat: redirect user according to selected language
Saba-Var Oct 23, 2022
fbea60a
fix: check language cookie value
Saba-Var Oct 24, 2022
89a5a47
feat: add action button to account activation page
Saba-Var Oct 24, 2022
8d7cddb
chore: disable log in button after click
Saba-Var Oct 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: submit sign in form and show errors
  • Loading branch information
Saba-Var committed Oct 16, 2022
commit 0493bc8e821d6995650e08f33467d63b26981623
2 changes: 1 addition & 1 deletion components/log-In/ForgetPassword/ForgetPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTranslate } from 'hooks'
const ForgetPassword = () => {
return (
<div className='text-sm'>
<a className='font-medium cursor-pointer text-blue-600 hover:text-blue-500'>
<a className='font-medium cursor-pointer text-blue-600 hover:underline hover:text-blue-700'>
{useTranslate('auth:forget-password')}
</a>
</div>
Expand Down
4 changes: 3 additions & 1 deletion components/log-In/LogInForm/LogInForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import {
} from 'components'

const LogInForm = () => {
const { formInitialValues, submitHandler } = useLogInForm()
const { formInitialValues, submitHandler, fetchError } = useLogInForm()

return (
<Formik
validateOnChange={fetchError ? false : true}
validateOnBlur={fetchError ? false : true}
initialValues={formInitialValues}
validationSchema={logInSchema}
onSubmit={submitHandler}
Expand Down
38 changes: 36 additions & 2 deletions components/log-In/LogInForm/useLogInForm.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
import { FormProperties, SignInFormData } from 'types'
import { useMutation } from 'react-query'
import { authorization } from 'services'
import { useRouter } from 'next/router'
import { useState } from 'react'

export const useLogInForm = () => {
const [fetchError, setFetchError] = useState(false)

const { mutate } = useMutation(authorization)
const { push } = useRouter()

const formInitialValues = {
password: '',
email: '',
}

const submitHandler = () => {}
const submitHandler = (
formData: SignInFormData,
{ setFieldError }: FormProperties
) => {
mutate(formData, {
onSuccess: () => {},
onError: (error: any) => {
const status = error.response.status
if (status === 401) {
setFieldError('email', 'incorrect-credentials')
setFieldError('password', 'incorrect-credentials')
} else if (status === 403) {
setFieldError('email', 'inactive-account')
setFieldError('password', 'inactive-account')
} else if (status === 429) {
push('/requests-exceed')
} else {
alert(error.message)
}

setFetchError(true)
},
})
}

return { formInitialValues, submitHandler }
return { formInitialValues, submitHandler, fetchError }
}
17 changes: 9 additions & 8 deletions components/shared/SuccessModal/SuccessModal.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { CheckIcon } from '@heroicons/react/24/outline'
import { Dialog, Transition } from '@headlessui/react'
import { Fragment, useRef, useState } from 'react'
import { SuccessModalProps } from './types.d'
import { Fragment, useRef } from 'react'
import { useTranslate } from 'hooks'

const SuccessModal = () => {
const [open, setOpen] = useState(true)
const SuccessModal: React.FC<SuccessModalProps> = (props) => {
const { setSignUpSuccess, signUpSuccess } = props

const cancelButtonRef = useRef(null)

return (
<Transition.Root show={open} as={Fragment}>
<Transition.Root show={signUpSuccess} as={Fragment}>
<Dialog
as='div'
className='relative z-10'
initialFocus={cancelButtonRef}
onClose={setOpen}
onClose={setSignUpSuccess}
>
<Transition.Child
as={Fragment}
Expand All @@ -41,9 +42,9 @@ const SuccessModal = () => {
>
<Dialog.Panel className='relative transform overflow-hidden rounded-lg bg-white px-4 pt-5 pb-4 text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-lg sm:p-6'>
<div>
<div className='mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-blue-100'>
<div className='mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-emerald-100'>
<CheckIcon
className='h-6 w-6 text-green-600 text-blue-600'
className='h-6 w-6 text-emerald-600'
aria-hidden='true'
/>
</div>
Expand Down Expand Up @@ -72,7 +73,7 @@ const SuccessModal = () => {
<button
type='button'
className='mt-3 inline-flex w-full justify-center rounded-md border border-gray-300 bg-white px-4 py-2 text-base font-medium text-gray-700 shadow-sm hover:bg-gray-50 sm:col-start-1 sm:mt-0 sm:text-sm'
onClick={() => setOpen(false)}
onClick={() => setSignUpSuccess(false)}
ref={cancelButtonRef}
>
{useTranslate('common:close')}
Expand Down
6 changes: 6 additions & 0 deletions components/shared/SuccessModal/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { SetState } from 'types'

export type SuccessModalProps = {
setSignUpSuccess: SetState<boolean>
signUpSuccess: boolean
}
10 changes: 8 additions & 2 deletions components/sign-up/SignUpForm/SignUpForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { signUpSchema } from 'schemas'
import { Form, Formik } from 'formik'

const SignUpForm = () => {
const { formInitialValues, submitHandler, signUpSuccess } = useSignUpForm()
const { formInitialValues, submitHandler, signUpSuccess, setSignUpSuccess } =
useSignUpForm()

return (
<Formik
Expand All @@ -15,7 +16,12 @@ const SignUpForm = () => {
{() => {
return (
<>
{signUpSuccess && <SuccessModal />}
{signUpSuccess && (
<SuccessModal
setSignUpSuccess={setSignUpSuccess}
signUpSuccess={signUpSuccess}
/>
)}

<Form className='flex flex-col gap-1'>
<InputField name='username' type='text' />
Expand Down
2 changes: 2 additions & 0 deletions components/sign-up/SignUpForm/useSignUpForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export const useSignUpForm = () => {
setFieldError('email', 'email-exists')
} else if (status === 429) {
push('/requests-exceed')
} else {
alert(error.message)
}
},
})
Expand Down
4 changes: 3 additions & 1 deletion public/locales/en/inputs.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@
"log-in": "Log in",
"sign-up": "Sign up",

"email-exists": "Email is already in use"
"email-exists": "Email is already in use",
"incorrect-credentials": "Email or password is incorrect",
"inactive-account": "Account is not activated"
}
4 changes: 3 additions & 1 deletion public/locales/ka/inputs.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@
"log-in": "შესვლა",
"sign-up": "რეგისტრაცია",

"email-exists": "ეს ელ-ფოსტა უკვე გამოყენებულია"
"email-exists": "ეს ელ-ფოსტა უკვე გამოყენებულია",
"incorrect-credentials": "ელ-ფოსტა ან პაროლი არასწორია",
"inactive-account": "ანგარიში არ არის გააქტიურებული"
}
10 changes: 8 additions & 2 deletions services/authService.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { SignUpFormData, Status } from 'types'
import { SignUpFormData, SignInFormData, Message, AccessToken } from 'types'
import { AxiosResponse } from 'axios'
import axios from './axios'

export const registerUSer = (
data: SignUpFormData
): Promise<AxiosResponse<Status>> => {
): Promise<AxiosResponse<Message>> => {
return axios.post('/authentication/sign-up', data)
}

export const authorization = (
data: SignInFormData
): Promise<AxiosResponse<AccessToken>> => {
return axios.post('/authentication/sign-in', data)
}
9 changes: 9 additions & 0 deletions types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export type SignUpFormData = {
email: string
}

export type SignInFormData = {
password: string
email: string
}

export type AccessToken = {
accessToken: string
}
Expand All @@ -24,6 +29,10 @@ export type Status = {
status: number
}

export type Message = {
message: string
}

export type FormProperties = {
setFieldError: (field: string, message: string) => void
setFieldValue: (field: string, value: string) => void
Expand Down