-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ramin Rezaei
committed
May 28, 2024
1 parent
69aa3cc
commit cccf93d
Showing
12 changed files
with
706 additions
and
12 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
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,6 @@ | ||
import { getDatabase } from "firebase/database"; | ||
|
||
export const DATABASE_USERS_TABLE_NAME = 'USERS'; | ||
export const DATABASE_MESSAGES_TABLE_NAME = 'MESSAGES'; | ||
|
||
export const DB = getDatabase(); |
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,9 @@ | ||
export const FIREBASE_CONFIGS = { | ||
projectId: "memories-counter", | ||
messagingSenderId: "295192400416", | ||
storageBucket: "memories-counter.appspot.com", | ||
authDomain: "memories-counter.firebaseapp.com", | ||
apiKey: "AIzaSyBdnKbIQ7a4Kv_7Nf74ZqX1VmpgJykiR80", | ||
appId: "1:295192400416:web:61c08e58c814a39197a245", | ||
databaseURL: "https://memories-counter-default-rtdb.europe-west1.firebasedatabase.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { notify } from '../utils/notify'; | ||
import { initializeApp } from 'firebase/app'; | ||
import { FIREBASE_CONFIGS } from './constants/firebase'; | ||
import { getAuth, signInAnonymously } from 'firebase/auth'; | ||
|
||
const FIREBASE_APP = initializeApp(FIREBASE_CONFIGS); | ||
|
||
const AUTH = getAuth(FIREBASE_APP); | ||
|
||
export const initFirebase = () => { | ||
signInAnonymously(AUTH) | ||
.then(() => { | ||
console.log('%cDB - SUCCESS!', 'color: green'); | ||
}) | ||
.catch(() => { | ||
notify.error({ message: "Error: We couldn't connect to the Database!" }); | ||
}); | ||
} |
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,53 @@ | ||
import { notify } from '../../utils/notify'; | ||
import { ref, set, get, child, push } from 'firebase/database'; | ||
import { DB, DATABASE_USERS_TABLE_NAME } from '../constants/database'; | ||
|
||
type UserDataType = GUser; | ||
type LoginCallbackType = (user: GUser) => void; | ||
|
||
export const LoginRequest = (code: string, callback: LoginCallbackType = () => {}) => { | ||
get(child(ref(DB), `${DATABASE_USERS_TABLE_NAME}/`)) | ||
.then((snapshot) => { | ||
if (snapshot.exists()) { | ||
const users = Object.entries(snapshot.val()).map(([key, value]) => ({ | ||
id: key, | ||
...value || {}, | ||
isAuthenticated: true | ||
})) as GUser[]; | ||
const user = users.filter((item) => item.code === code); | ||
|
||
if (user.length) { | ||
notify.success({ message: 'Welcome! 😍' }); | ||
callback(user[0]); | ||
} else { | ||
notify.error({ message: 'Oops, Your Code Is Wrong!' }); | ||
} | ||
} | ||
}) | ||
.catch(() => notify.error({ message: 'Oops, Server Error!' })); | ||
}; | ||
|
||
export const RegisterRequest = (userData: UserDataType, callback: LoginCallbackType = () => {}) => { | ||
const userId = push(child(ref(DB), DATABASE_USERS_TABLE_NAME)).key; | ||
set(ref(DB, `${DATABASE_USERS_TABLE_NAME}/${userId}`), userData) | ||
.then(() => { | ||
notify.success({ message: 'Your account created successful.' }); | ||
LoginRequest(userData.code, callback); | ||
}) | ||
.catch(() => notify.error({ message: 'Oops, You cannot create account now!' })); | ||
}; | ||
|
||
export const GetUsersRequest = (callback: (users: GUser[]) => void = () => {}) => { | ||
get(child(ref(DB), `${DATABASE_USERS_TABLE_NAME}/`)) | ||
.then((snapshot) => { | ||
if (snapshot.exists()) { | ||
const users = Object.entries(snapshot.val()).map(([key, value]) => ({ | ||
id: key, | ||
...value || {}, | ||
isAuthenticated: true | ||
})) as GUser[]; | ||
callback(users); | ||
} | ||
}) | ||
.catch(() => notify.error({ message: 'Oops, Server Error!' })); | ||
}; |
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,16 @@ | ||
import { notify } from '../../utils/notify'; | ||
import { ref, get, child } from 'firebase/database'; | ||
import { DB, DATABASE_MESSAGES_TABLE_NAME } from '../constants/database'; | ||
|
||
type GetMessagesRequestProps = (messages?: GMessage[]) => void; | ||
|
||
export const GetMessagesRequest = (callback: GetMessagesRequestProps = () => {}) => { | ||
get(child(ref(DB), `${DATABASE_MESSAGES_TABLE_NAME}/`)) | ||
.then((snapshot) => { | ||
if (snapshot.exists()) { | ||
console.log('>>> Messages Updated'); | ||
callback(Object.values(snapshot.val()) as GMessage[]); | ||
} | ||
}) | ||
.catch(() => notify.error({ message: "Error: Messages couldn't be updated." })); | ||
}; |
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,9 @@ | ||
import { notify } from '../../utils/notify'; | ||
import { ref, set } from 'firebase/database'; | ||
import { DB, DATABASE_MESSAGES_TABLE_NAME } from '../constants/database'; | ||
|
||
export const PostMessageRequest = (userId: string, data: any, callback = () => {}) => { | ||
set(ref(DB, `${DATABASE_MESSAGES_TABLE_NAME}/${userId}`), data) | ||
.then(callback) | ||
.catch(() => notify.error({ message: "Error: Your messages couldn't be saved." })); | ||
}; |
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.