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

[RM-41]마이페이지 userInfo #78

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4,472 changes: 2,236 additions & 2,236 deletions .pnp.cjs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/components/Common/Input/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface InputProps {
export const labels: { [key: string]: string } = {
id: "아이디",
name: "이름",
displayName: "이름",
email: "이메일",
phone: "전화번호",
password: "비밀번호",
Expand Down
8 changes: 7 additions & 1 deletion src/components/MyPage/UserInfo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { ChangeEvent, useState } from "react";
import React, { ChangeEvent, useState, useContext } from "react";
import AuthContext from "context/AuthContext";
import Input from "../Input";
import { UserInfoProps } from "./types";
import { getAuth, deleteUser } from "firebase/auth";
Expand All @@ -14,6 +15,11 @@ const UserInfo = () => {
password: "",
passwordCheck: "",
});

const { user } = useContext(AuthContext);

console.log(user);

const auth = getAuth(app);
const router = useRouter();

Expand Down
34 changes: 34 additions & 0 deletions src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React, { ReactNode, createContext, useEffect, useState } from "react";
import { User, getAuth, onAuthStateChanged } from "firebase/auth";
import { app } from "firebaseApp";

interface AuthProps {
children: ReactNode;
}

const AuthContext = createContext({
user: null as User | null,
});

export const AuthContextProvider = ({ children }: AuthProps) => {
const auth = getAuth(app);
const [currentUser, setCurrentUser] = useState<User | null>(null);

useEffect(() => {
onAuthStateChanged(auth, (user) => {
if (user) {
setCurrentUser(user);
} else {
setCurrentUser(user);
}
});
}, [auth]);

return (
<AuthContext.Provider value={{ user: currentUser }}>
{children}
</AuthContext.Provider>
);
};

export default AuthContext;
2 changes: 2 additions & 0 deletions src/firebaseApp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Import the functions you need from the SDKs you need
import { initializeApp, FirebaseApp, getApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
// import { getAnalytics } from "firebase/analytics";

export let app: FirebaseApp;
Expand All @@ -24,4 +25,5 @@ try {
const firebase = initializeApp(firebaseConfig);
// const analytics = getAnalytics(firebase);

export const db = getFirestore(app);
export default firebase;
13 changes: 8 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import ReactDOM from "react-dom/client";
import { BrowserRouter as Router } from "react-router-dom";
import { AuthContextProvider } from "context/AuthContext";
import App from "./pages/App";
import firebase from "./firebaseApp";
import "../src/styles/global.css";
Expand All @@ -15,11 +16,13 @@ if (rootElement) {
const root = ReactDOM.createRoot(rootElement);
root.render(
<React.StrictMode>
<SectionProvider>
<Router>
<App />
</Router>
</SectionProvider>
<AuthContextProvider>
<SectionProvider>
<Router>
<App />
</Router>
</SectionProvider>
</AuthContextProvider>
</React.StrictMode>,
);
} else {
Expand Down
22 changes: 15 additions & 7 deletions src/pages/SignupPage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { getAuth, createUserWithEmailAndPassword } from "firebase/auth";
import { getAuth, createUserWithEmailAndPassword, updateProfile } from "firebase/auth";
import React, { useState, ChangeEvent, FormEvent } from "react";
import { collection, addDoc } from "firebase/firestore";
import { toast } from "react-toastify";
import { InputProps } from "../../components/Common/Input/types";
import Input from "../../components/Common/Input";
import { useRouter } from "../routing";
import { app } from "../../firebaseApp";
import { app, db } from "../../firebaseApp";

function SignupPage() {
const [registerError, setRegisterError] = useState<string>("");
Expand Down Expand Up @@ -33,7 +34,7 @@ function SignupPage() {
if (targetId === "name") {
const nameRegex = /^[가-힣a-zA-Z]+$/;

if (!nameRegex.test(targetId) || credentials.name.length < 1) {
if (!nameRegex.test(value) || value.length < 1) {
setErrors({ ...errors, name: "올바른 이름을 입력해주세요." });
} else {
setErrors({ ...errors, name: "" });
Expand Down Expand Up @@ -74,19 +75,26 @@ function SignupPage() {
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
console.log("Form Submitted:", credentials);
const { email, password } = credentials;

const { name, email, password } = credentials;
try {
const auth = getAuth(app);
await createUserWithEmailAndPassword(auth, email, password);

const userCredential = await createUserWithEmailAndPassword(auth, email, password);

await addDoc(collection(db, "userInfo"), {
name: name,
email: email,
registrationDate: new Date()
});

toast.success("회원가입에 성공했습니다.");
router.push("/login");
} catch (error: any) {
handleFirebaseError(error);
console.log(error);
}
};


function handleFirebaseError(error: { code: string; message: string }) {
let message = "";
Expand Down
Loading