-
Notifications
You must be signed in to change notification settings - Fork 7
Auth 컴포넌트 작성, 필요 라이브러리 설치 #55
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 all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
eba76cf
refactor: Pagination 현재 페이지 계산을 useMemo로 메모했습니다.
hanana1253 e6f3294
feat: Auth 컴포넌트 추가 및 이를 위한 패키지(Formik, yup) 설치
hanana1253 cda3787
conf: Formik, yup 설치
hanana1253 72cc149
Merge branch 'feature/auth' into develop
hanana1253 9ef6f47
fix: Styled 파일과 types 파일 잘못 기재한 부분 수정했습니다.
hanana1253 5940faf
conf: gitignore 파일 수정 (firebase관련 파일 추가)
hanana1253 6fe939e
feat: AuthService에 SignIn, SignUp 함수 연결
hanana1253 d727495
Merge branch 'develop' of https://github.com/TeamCooks/TwoSpoon into …
hanana1253 0d33141
chore: Prop type에 스타일을 위한 $warning 프롭 타입 추가
hanana1253 ece8ecf
chore: AuthService 파일에 as const로 객체 형태를 고정시켰습니다.
hanana1253 270a377
fix: utils import 경로 수정
hanana1253 6f8d37f
refactor: usePageNum 훅 작성
hanana1253 8bc11c4
fix: stories 파일들에서 decorator 제거
hanana1253 1ea30cc
refactor: 필요없는 변수 제거
hanana1253 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { initializeApp } from 'firebase/app'; | ||
| import { getFirestore, setDoc, doc, Timestamp } from 'firebase/firestore'; | ||
| import { | ||
| getAuth, | ||
| signInWithEmailAndPassword, | ||
| createUserWithEmailAndPassword, | ||
| signOut, | ||
| onAuthStateChanged, | ||
| } from 'firebase/auth'; | ||
| import { SignInFormValues, SignUpFormValues } from 'components/Auth/Auth.types'; | ||
| import { firebaseConfig } from './firebaseConfig'; | ||
|
|
||
| initializeApp(firebaseConfig); | ||
|
|
||
| const auth = getAuth(); | ||
| const db = getFirestore(); | ||
|
|
||
| export const getAuthStatus = async () => { | ||
| return new Promise((resolve, reject) => { | ||
| try { | ||
| onAuthStateChanged(auth, (user) => resolve(user)); | ||
| } catch (e) { | ||
| reject(e); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| export const signIn = async ({ email, password }: SignInFormValues) => { | ||
| try { | ||
| const { user } = await signInWithEmailAndPassword(auth, email, password); | ||
| return user; | ||
| } catch (error) { | ||
| throw new Error(error.code); | ||
| } | ||
| }; | ||
|
|
||
| export const signUp = async ({ username, email, password }: SignUpFormValues) => { | ||
| try { | ||
| const { user } = await createUserWithEmailAndPassword(auth, email, password); | ||
| const docRef = await setDoc(doc(db, 'users', user.uid), { | ||
| username, | ||
| email, | ||
| createdAt: Timestamp.fromDate(new Date()), | ||
| }); | ||
| return user; | ||
| } catch (error) { | ||
| throw new Error(error.code); | ||
| } | ||
| }; | ||
|
|
||
| export const logOut = () => { | ||
| signOut(auth); | ||
| }; | ||
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,14 @@ | ||
| import { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
| import { Auth } from './Auth'; | ||
|
|
||
| export default { | ||
| title: 'Auth', | ||
| component: Auth, | ||
| args: { currentForm: 'signin' }, | ||
| } as ComponentMeta<typeof Auth>; | ||
|
|
||
| const Template: ComponentStory<typeof Auth> = (args) => <Auth {...args} />; | ||
|
|
||
| export const SignInForm = Template.bind({}); | ||
| export const SignUpForm = Template.bind({}); | ||
| SignUpForm.args = { currentForm: 'signup' }; |
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,19 @@ | ||
| import styled from '@emotion/styled'; | ||
| import { pxToRem } from 'utils'; | ||
| import { StyleInputProps } from './Auth.types'; | ||
|
|
||
| export const StyledFieldError = styled.div` | ||
| height: ${pxToRem(32)}; | ||
| line-height: 1.8; | ||
| font-size: ${pxToRem(16)}; | ||
| color: ${({ theme }) => theme.color.primaryOrange}; | ||
| padding: 0 ${pxToRem(20)}; | ||
| `; | ||
|
|
||
| export const StyledInput = styled.input<StyleInputProps>` | ||
| padding: 0 ${pxToRem(24)}; | ||
| height: ${pxToRem(36)}; | ||
| border: none; | ||
| border-radius: ${pxToRem(5)} ${pxToRem(5)}; | ||
| ${({ $warning, theme }) => $warning && `box-shadow: 0 0 1px 5px ${theme.color.primaryOrange};`} | ||
| `; |
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,42 @@ | ||
| import { FormikProps, withFormik } from 'formik'; | ||
| import { FormValues, FormProps } from './Auth.types'; | ||
| import { StyledInput, StyledFieldError } from './Auth.styled'; | ||
| import { AUTH_FUNC, SCHEMA, INITIAL_VALUES, FIELDS, HEADING, PLACEHOLDER, TYPE } from './AuthServices'; | ||
|
|
||
| const AuthForm = (props: FormProps & FormikProps<FormValues>): JSX.Element => { | ||
| const { currentForm, values, errors, dirty, touched, isValid, handleChange, handleBlur, handleSubmit } = props; | ||
| return ( | ||
| <form onSubmit={handleSubmit}> | ||
| {FIELDS[currentForm].map((field): JSX.Element => ( | ||
| <> | ||
| <label className="a11yHidden" htmlFor={field}> | ||
hanana1253 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| {field.toUpperCase()} | ||
| </label> | ||
| <StyledInput | ||
| id={field} | ||
| $warning={touched[field] && errors[field]} | ||
| name={field} | ||
| placeholder={PLACEHOLDER[field]} | ||
| type={TYPE[field]} | ||
| onChange={handleChange} | ||
| onBlur={handleBlur} | ||
| value={values[field] || ''} | ||
| /> | ||
| <StyledFieldError>{touched[field] && errors[field]}</StyledFieldError> | ||
| </> | ||
| ))} | ||
|
|
||
| <button type="submit" disabled={!dirty || !isValid}> | ||
| {HEADING[currentForm]} | ||
| </button> | ||
| </form> | ||
| ); | ||
| }; | ||
|
|
||
| export const Auth = withFormik<FormProps, FormValues>({ | ||
| mapPropsToValues: ({ currentForm }) => INITIAL_VALUES[currentForm], | ||
| validationSchema: ({ currentForm }: FormProps) => SCHEMA[currentForm], | ||
| handleSubmit(values: FormValues, { props: { currentForm } }) { | ||
| AUTH_FUNC[currentForm](values); | ||
| }, | ||
| })(AuthForm); | ||
hanana1253 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,23 @@ | ||
| export interface SignInFormValues { | ||
| password: string; | ||
| email: string; | ||
| } | ||
|
|
||
| export interface SignUpFormValues extends SignInFormValues { | ||
| username: string; | ||
| passwordConfirm: string; | ||
| } | ||
|
|
||
| export type FormValues = SignInFormValues | SignUpFormValues; | ||
|
|
||
| export interface FormProps { | ||
| initialEmail?: string; | ||
| initialPassword?: string; | ||
| initialPasswordConfirm?: string; | ||
| initialUsername?: string; | ||
| currentForm: 'signin' | 'signup'; | ||
| } | ||
|
|
||
| export interface StyleInputProps { | ||
| $warning: boolean; | ||
| } |
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,69 @@ | ||
| import { signIn, signUp } from 'api/requestAuth'; | ||
| import * as Yup from 'yup'; | ||
|
|
||
| export const TOGGLE_MESSAGE = { | ||
hanana1253 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| signin: 'Not registered yet? Sign up here!', | ||
| signup: 'Already a member? Sign in here!', | ||
| } as const; | ||
|
|
||
| export const HEADING = { | ||
| signin: 'Sign In', | ||
| signup: 'Sign Up', | ||
| } as const; | ||
|
|
||
| export const PLACEHOLDER = { | ||
| username: 'Username', | ||
| email: 'Email', | ||
| password: 'Password', | ||
| passwordConfirm: 'Confirm password', | ||
| } as const; | ||
|
|
||
| export const TYPE = { | ||
| username: 'text', | ||
| email: 'email', | ||
| password: 'password', | ||
| passwordConfirm: 'password', | ||
| } as const; | ||
|
|
||
| export const INITIAL_VALUES = { | ||
| signin: { | ||
| password: '', | ||
| email: '', | ||
| }, | ||
| signup: { | ||
| username: '', | ||
| password: '', | ||
| passwordConfirm: '', | ||
| email: '', | ||
| }, | ||
| } as const; | ||
|
|
||
| export const SCHEMA = { | ||
| signin: Yup.object({ | ||
| password: Yup.string().min(8, 'Must be 8 characters or more').required('Required'), | ||
| email: Yup.string().email('Invalid email address').required('Required'), | ||
| }), | ||
| signup: Yup.object({ | ||
| username: Yup.string().min(3, 'Must be 3 characters or more').required('Required'), | ||
| password: Yup.string().min(8, 'Must be 8 characters or more').required('Required'), | ||
| passwordConfirm: Yup.string() | ||
| .oneOf([Yup.ref('password'), null], 'Passwords must match') | ||
| .required('Required'), | ||
| email: Yup.string().email('Invalid email address').required('Required'), | ||
| }), | ||
| } as const; | ||
|
|
||
| export const AUTH_ERROR_MSG = { | ||
| signin: 'Sign in failed. Please try again.', | ||
| signup: 'Sign up failed. Please try again.', | ||
| } as const; | ||
|
|
||
| export const FIELDS = { | ||
| signin: ['email', 'password'], | ||
| signup: ['username', 'email', 'password', 'passwordConfirm'], | ||
| } as const; | ||
|
|
||
| export const AUTH_FUNC = { | ||
| signin: signIn, | ||
| signup: signUp, | ||
| } as const; | ||
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 |
|---|---|---|
| @@ -1,6 +1,28 @@ | ||
| export interface PaginationProps { | ||
| currentPage: number; | ||
| onClick: (currentPage: number) => void; | ||
| totalResults: number; | ||
| limit: number; | ||
| } | ||
| import styled from '@emotion/styled'; | ||
| import { pxToRem } from 'utils'; | ||
|
|
||
| export const StyledPaginationControl = styled.div` | ||
| display: flex; | ||
| justify-contents: center; | ||
| align-items: center; | ||
| gap: ${pxToRem(20)}; | ||
| ul { | ||
| display: flex; | ||
| justify-contents: center; | ||
| align-items: center; | ||
| gap: ${pxToRem(10)}; | ||
| } | ||
| `; | ||
|
|
||
| export const StyledPageButton = styled.button` | ||
| color: ${({ theme, current }) => (current ? theme.color.white : theme.color.primaryOrange)}; | ||
| border: 1px solid ${({ theme }) => theme.color.primaryOrange}; | ||
| border-radius: ${pxToRem(5)}; | ||
| background-color: ${({ theme, current }) => (current ? theme.color.primaryOrange : theme.color.white)}; | ||
| width: ${pxToRem(32)}; | ||
| height: ${pxToRem(32)}; | ||
| margin: ${pxToRem(6)}; | ||
| &:hover { | ||
| opacity: 0.5; | ||
| } | ||
| `; |
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 |
|---|---|---|
| @@ -1,28 +1,6 @@ | ||
| import styled from '@emotion/styled'; | ||
| import { pxToRem } from '../../utils'; | ||
|
|
||
| export const StyledPaginationControl = styled.div` | ||
| display: flex; | ||
| justify-contents: center; | ||
| align-items: center; | ||
| gap: ${pxToRem(20)}; | ||
| ul { | ||
| display: flex; | ||
| justify-contents: center; | ||
| align-items: center; | ||
| gap: ${pxToRem(10)}; | ||
| } | ||
| `; | ||
|
|
||
| export const StyledPageButton = styled.button` | ||
| color: ${({ theme, current }) => (current ? theme.color.white : theme.color.primaryOrange)}; | ||
| border: 1px solid ${({ theme }) => theme.color.primaryOrange}; | ||
| border-radius: ${pxToRem(5)}; | ||
| background-color: ${({ theme, current }) => (current ? theme.color.primaryOrange : theme.color.white)}; | ||
| width: ${pxToRem(32)}; | ||
| height: ${pxToRem(32)}; | ||
| margin: ${pxToRem(6)}; | ||
| &:hover { | ||
| opacity: 0.5; | ||
| } | ||
| `; | ||
| export interface PaginationProps { | ||
| currentPage: number; | ||
| onClick: (currentPage: number) => void; | ||
| totalResults: number; | ||
| limit: number; | ||
| } |
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.