Skip to content

Commit 004df67

Browse files
committed
init firebase with authentication
1 parent 4cb9327 commit 004df67

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
REACT_APP_FIREBASE_API_KEY=
2+
REACT_APP_FIREBASE_AUTH_DOMAIN=
3+
REACT_APP_FIREBASE_PROJECT_ID=
4+
REACT_APP_FIREBASE_STORAGE_BUCKET=
5+
REACT_APP_FIREBASE_MESSAGING_SENDER_ID=
6+
REACT_APP_FIREBASE_APP_ID=

src/api/auth.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { auth } from "../firebase/config";
2+
import {
3+
createUserWithEmailAndPassword,
4+
signInWithEmailAndPassword,
5+
signOut as firebaseSignOut,
6+
} from "firebase/auth";
7+
import { createUser } from "./profile";
8+
9+
type SignUpRequest = {
10+
name: string;
11+
email: string;
12+
password: string;
13+
subcomms: string;
14+
contactNum: string;
15+
};
16+
export async function signUp(params: SignUpRequest) {
17+
const { name, email, password, subcomms, contactNum } = params;
18+
19+
const userCredential = await createUserWithEmailAndPassword(
20+
auth,
21+
email,
22+
password
23+
);
24+
25+
await createUser({
26+
credential: userCredential,
27+
profile: {
28+
name,
29+
email,
30+
subcomms,
31+
contactNum,
32+
},
33+
});
34+
}
35+
36+
type LoginRequest = {
37+
email: string;
38+
password: string;
39+
};
40+
export async function login(param: LoginRequest) {
41+
const { email, password } = param;
42+
return signInWithEmailAndPassword(auth, email, password);
43+
}
44+
45+
export async function signOut() {
46+
await firebaseSignOut(auth);
47+
}

src/api/profile.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { UserCredential } from "firebase/auth";
2+
import { auth, db } from "../firebase/config";
3+
import { doc, setDoc, updateDoc } from "@firebase/firestore";
4+
import { UserProfile } from "../types/profile";
5+
6+
const USER_PATH = "users";
7+
8+
export type CreateUserRequest = {
9+
credential: UserCredential;
10+
profile: UserProfile;
11+
};
12+
export async function createUser(params: CreateUserRequest) {
13+
const { credential, profile } = params;
14+
const uid = credential.user.uid;
15+
16+
await setDoc(doc(db, USER_PATH, uid), profile);
17+
}
18+
19+
export type UpdateUserRequest = {
20+
profile: Partial<Omit<UserProfile, "user" | "email">>;
21+
};
22+
export async function updateUserData(params: UpdateUserRequest) {
23+
const { profile } = params;
24+
25+
if (!auth.currentUser) {
26+
console.error("User needs to log in");
27+
return;
28+
}
29+
30+
const uid = auth.currentUser.uid;
31+
32+
await updateDoc(doc(db, USER_PATH, uid), profile);
33+
}

src/firebase/config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { initializeApp } from "firebase/app";
2+
import { getFirestore } from "@firebase/firestore";
3+
import { getAuth } from "firebase/auth";
4+
import { getStorage } from "firebase/storage";
5+
6+
const firebaseConfig = {
7+
apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
8+
authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
9+
projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
10+
storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
11+
messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
12+
appId: process.env.REACT_APP_FIREBASE_APP_ID,
13+
};
14+
15+
const app = initializeApp(firebaseConfig);
16+
export const db = getFirestore(app);
17+
export const auth = getAuth(app);
18+
export const storage = getStorage(app);

src/types/profile.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type UserProfile = {
2+
name: string;
3+
email: string;
4+
contactNum: string;
5+
// role: string; // clarify
6+
subcomms: string;
7+
// status: string; // clarify
8+
};

0 commit comments

Comments
 (0)