-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from P-r4Tes/feat/firebase-authentication
Feat/firebase authentication
- Loading branch information
Showing
15 changed files
with
2,412 additions
and
2,031 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
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
/node_modules | ||
/.pnp | ||
.pnp.js | ||
.pnp.cjs | ||
.yarn/install-state.gz | ||
|
||
# testing | ||
|
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 |
---|---|---|
|
@@ -25,9 +25,10 @@ describe("User functions", () => { | |
jest.clearAllMocks(); | ||
}); | ||
|
||
test("name이 비어있으면 postUser는 에러를 반환해야 한다", () => { | ||
const body = { name: "", groups: ["g1"], personalSchedules: ["s1"] }; | ||
expect(() => postUser(body)).toThrow("Invalid name"); | ||
test("email 비어있으면 postUser는 에러를 반환해야 한다", () => { | ||
const id = "TEST_USER_ID"; // id 추가 | ||
const body = { email: "", name: "name", groups: ["g1"], personalSchedules: ["s1"] }; | ||
expect(() => postUser(id, body)).toThrow("Invalid email"); | ||
}); | ||
|
||
test("id가 비어있지 않으면 getUser는 getFirebase를 호출해야 한다", () => { | ||
|
@@ -42,10 +43,11 @@ describe("User functions", () => { | |
expect(firebaseModule.deleteFirebase).toHaveBeenCalledWith("users", id); | ||
}); | ||
|
||
test("name이 비어있지 않으면 postUser는 postFirebase를 호출해야 한다", () => { | ||
const body = { name: "CSKIM", groups: ["g1"], personalSchedules: ["s1"] }; | ||
postUser(body); | ||
expect(firebaseModule.postFirebase).toHaveBeenCalledWith("users", body); | ||
test("email이 비어있지 않으면 postUser는 postFirebase를 호출해야 한다", () => { | ||
const id = "TEST_USER_ID"; // id 추가 | ||
const body = { email: "[email protected]", name: "CSKIM", groups: ["g1"], personalSchedules: ["s1"] }; | ||
postUser(id, body); | ||
expect(firebaseModule.postFirebase).toHaveBeenCalledWith("users", id, body); | ||
}); | ||
|
||
test("전달인자가 유효하지 않다면 pushUserPersonalSchedule는 에러를 반환해야 한다", () => { | ||
|
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { useState } from "react"; | ||
import { signInWithEmailAndPassword, GoogleAuthProvider, signInWithPopup } from "firebase/auth"; | ||
import { auth } from "@/lib/firebaseAuth"; | ||
import { doc, setDoc } from "firebase/firestore"; | ||
import { db } from "@/lib/firebaseConfig"; | ||
|
||
export default function LoginForm() { | ||
const [email, setEmail] = useState(""); | ||
const [password, setPassword] = useState(""); | ||
|
||
const handleEmailLogin = async e => { | ||
e.preventDefault(); | ||
try { | ||
await signInWithEmailAndPassword(auth, email, password); | ||
alert("이메일 로그인에 성공했습니다!"); | ||
} catch (error) { | ||
alert("이메일 로그인에 실패했습니다."); | ||
} | ||
}; | ||
|
||
const handleGoogleLogin = async () => { | ||
const provider = new GoogleAuthProvider(); | ||
try { | ||
const result = await signInWithPopup(auth, provider); | ||
const user = result.user; | ||
await setDoc( | ||
doc(db, "users", user.uid), | ||
{ | ||
email: user.email, | ||
groups: [], | ||
name: user.displayName, | ||
personalSchedules: [], | ||
}, | ||
{ merge: true } | ||
); | ||
alert("구글 로그인에 성공했습니다!"); | ||
} catch (error) { | ||
alert("구글 로그인에 실패했습니다."); | ||
console.log(error.message); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="max-w-md mx-auto p-5 shadow-md"> | ||
<h2 className="text-lg font-bold mb-4">로그인</h2> | ||
<form onSubmit={handleEmailLogin} className="flex flex-col"> | ||
<div className="mb-5"> | ||
<input | ||
type="email" | ||
placeholder="이메일" | ||
value={email} | ||
onChange={e => setEmail(e.target.value)} | ||
className="p-2.5 text-base rounded-md border border-gray-300" | ||
/> | ||
</div> | ||
<div className="mb-5"> | ||
<input | ||
type="password" | ||
placeholder="비밀번호" | ||
value={password} | ||
onChange={e => setPassword(e.target.value)} | ||
className="p-2.5 text-base rounded-md border border-gray-300" | ||
/> | ||
</div> | ||
<button | ||
type="submit" | ||
className="p-2.5 text-base bg-blue-500 text-white rounded-md border-none cursor-pointer hover:bg-blue-700" | ||
> | ||
이메일로 로그인 | ||
</button> | ||
<button | ||
type="button" | ||
onClick={handleGoogleLogin} | ||
className="p-2.5 text-base bg-blue-500 text-white rounded-md border-none cursor-pointer hover:bg-blue-700 mt-3" | ||
> | ||
구글로 로그인 | ||
</button> | ||
</form> | ||
</div> | ||
); | ||
} |
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,57 @@ | ||
import { useState } from "react"; | ||
import { createUserWithEmailAndPassword } from "firebase/auth"; | ||
import { auth } from "@/lib/firebaseAuth"; | ||
import { doc, setDoc } from "firebase/firestore"; | ||
import { db } from "@/lib/firebaseConfig"; | ||
|
||
export default function SignupForm() { | ||
const [email, setEmail] = useState(""); | ||
const [password, setPassword] = useState(""); | ||
|
||
const handleSignUp = async e => { | ||
e.preventDefault(); | ||
try { | ||
const userCredential = createUserWithEmailAndPassword(auth, email, password); | ||
const user = (await userCredential).user; | ||
await setDoc(doc(db, "users", user.uid), { | ||
email: email, | ||
groups: [], | ||
name: "", | ||
personalSchedules: [], | ||
}); | ||
alert("회원가입에 성공했습니다!"); | ||
} catch (error) { | ||
alert("회원가입에 실패했습니다."); | ||
console.log(error.message); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="max-w-md mx-auto p-5 shadow-md"> | ||
<h2 className="text-lg font-bold mb-4">회원가입</h2> | ||
<form onSubmit={handleSignUp} className="flex flex-col space-y-4"> | ||
<div> | ||
<input | ||
type="email" | ||
placeholder="이메일" | ||
value={email} | ||
onChange={e => setEmail(e.target.value)} | ||
className="w-full p-2 text-base border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" | ||
/> | ||
</div> | ||
<div> | ||
<input | ||
type="password" | ||
placeholder="비밀번호" | ||
value={password} | ||
onChange={e => setPassword(e.target.value)} | ||
className="w-full p-2 text-base border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" | ||
/> | ||
</div> | ||
<button type="submit" className="w-full p-2 bg-blue-500 text-white rounded-md hover:bg-blue-700"> | ||
회원가입 | ||
</button> | ||
</form> | ||
</div> | ||
); | ||
} |
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,4 @@ | ||
import { app } from "./firebaseConfig"; | ||
import { getAuth } from "firebase/auth"; | ||
|
||
export const auth = getAuth(app); |
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
Oops, something went wrong.