Merge pull request #7 from Nez27/feat/add-model-and-common-file

Init user model and common file
This commit is contained in:
Loi Phan
2023-08-29 16:18:48 +07:00
committed by GitHub
7 changed files with 122 additions and 8 deletions
@@ -0,0 +1,5 @@
export const DATABASE_URL =
'https://javascript-training-81f7a-default-rtdb.asia-southeast1.firebasedatabase.app';
export const TIME_OUT_SEC = 3;
export const REGEX_PASSWORD =
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
@@ -0,0 +1,12 @@
export const MESSAGE = {
PASSWORD_NOT_MATCH: 'Password not match! Please try again!',
PASSWORD_NOT_STRONG:
'Password must at least one uppercase, one lowercase letter, one number and one special character!',
ERROR_MESSAGE_DEFAULT: 'Something went wrong!',
};
export const TIME_OUT_ERROR = {
status: 408,
message: 'Connection time out! Please try again!',
};
export const TYPE_FORM = { success: 'success', error: 'error' };
export const MARK_ICON = { success: 'check', error: 'error' };
@@ -0,0 +1,29 @@
import {
TIME_OUT_SEC,
TIME_OUT_ERROR,
REGEX_PASSWORD,
} from '../constants/constant';
import FirebaseService from '../services/firebaseService';
export const validatePassword = (password) => {
return REGEX_PASSWORD.test(password);
};
export const timeout = (s) => {
return new Promise((reject) => {
setTimeout(() => {
reject(TIME_OUT_ERROR);
}, s * 1000);
});
};
export const timeOutConnect = async (action) => {
const result = await Promise.race([action, timeout(TIME_OUT_SEC)]);
if (result && result.status === TIME_OUT_ERROR.status) {
FirebaseService.disconnect();
throw new Error(`${result.message}`);
}
return result;
};
-1
View File
@@ -1 +0,0 @@
// TODO
@@ -0,0 +1,11 @@
export default class User {
constructor({ email, password, walletName = '' }) {
this.email = email;
this.password = password;
this.walletName = walletName;
}
static createIdUser() {
return new Date().getTime();
}
}
@@ -0,0 +1,58 @@
import { initializeApp } from 'firebase/app';
import {
getDatabase,
ref,
set,
goOffline,
goOnline,
onValue,
} from 'firebase/database';
import { DATABASE_URL } from '../constants/config';
class FirebaseService {
constructor() {
const firebaseConfig = {
DATABASE_URL,
};
this.app = initializeApp(firebaseConfig);
this.db = getDatabase(this.app);
}
save(data, path) {
return set(ref(this.db, path), data);
}
disconnect() {
goOffline(this.db);
}
reconnect() {
goOnline(this.db);
}
findKeyByPropery(path, property, value) {
return new Promise((resolve) => {
onValue(
ref(this.db, path),
(snapshot) => {
let result = false;
// snapshot is a type of data by Firebase define
snapshot.forEach((childSnapshot) => {
const data = childSnapshot.val();
if (data[property] === value) {
result = true;
}
});
resolve(result);
},
{
onlyOnce: true,
},
);
});
}
}
export default new FirebaseService();
+7 -7
View File
@@ -3,24 +3,23 @@
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="../styles/_main.scss" />
<title>Register - Money Lover</title> <title>Register - Money Lover</title>
</head> </head>
<body> <body>
<!-- <div class="loader"></div> --> <!-- <div class="loader hidden"></div> -->
<div class="overlay"></div> <!-- <div class="overlay"></div>
<div class="modal-box"> <div class="modal-box success-form">
<div class="checkmark"></div> <div class="mark check"></div>
<h2 class="modal-box__title">Register success!</h2> <h2 class="modal-box__title">Register success!</h2>
<p class="modal-box__message">Please login to continue.</p> <p class="modal-box__message">Please login to continue.</p>
<button class="modal-box__redirect-btn">OK!</button> <button class="modal-box__redirect-btn">OK!</button>
</div> </div> -->
<div class="background"> <div class="background">
<div class="wrapper"> <div class="wrapper">
<img class="logo-web" src="../assets/images/logo.svg" alt="Logo web" /> <img class="logo-web" src="../assets/images/logo.svg" alt="Logo web" />
<!-- Register form --> <!-- Register form -->
<form action="#" class="form" method="post"> <form action="#" class="form" method="post" id="registerForm">
<div class="form__container"> <div class="form__container">
<h1 class="form__title">Register</h1> <h1 class="form__title">Register</h1>
<p class="form__description">Using Money Lover account</p> <p class="form__description">Using Money Lover account</p>
@@ -55,5 +54,6 @@
<!-- End form --> <!-- End form -->
</div> </div>
</div> </div>
<script type="module" src="../js/controller.js"></script>
</body> </body>
</html> </html>