Skip to content

Commit 87a1c96

Browse files
authored
Merge pull request #95 from raduwen/feat/firebase-9
feat: migrate firebase 9.x
2 parents 65ffa58 + 21318b4 commit 87a1c96

File tree

10 files changed

+426
-247
lines changed

10 files changed

+426
-247
lines changed

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@
1313
},
1414
"dependencies": {
1515
"@material-ui/core": "^4.12.3",
16-
"firebase": "^8.9.1",
16+
"firebase": "^9.1.2",
1717
"next": "^11.1.2",
1818
"react": "^17.0.2",
1919
"react-dom": "^17.0.2",
2020
"styled-components": "^5.3.1"
2121
},
2222
"devDependencies": {
23-
"@firebase/app-types": "^0.7.0",
24-
"@firebase/auth-types": "^0.11.0",
25-
"@firebase/database-types": "^0.9.1",
2623
"@types/node": "^16.10.3",
2724
"@types/react": "^17.0.27",
2825
"@types/react-dom": "^17.0.9",

src/components/IFrameWidget/editor.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { Component, MouseEvent } from 'react';
22
import styled from 'styled-components';
3+
import { ref, set } from '@firebase/database';
34
import { db } from '@/lib/firebase';
45
import {
56
TextField,
@@ -31,7 +32,7 @@ class IFrameWidgetEditor extends Component<Props, IFrameWidgetProps> {
3132

3233
save(e: MouseEvent<HTMLButtonElement>) {
3334
e.preventDefault();
34-
db.ref(`/widgets/${this.props.id}/props`).set(this.state);
35+
set(ref(db, `/widgets/${this.props.id}/props`), this.state);
3536
}
3637

3738
render() {

src/components/TextWidget/editor.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '@material-ui/core';
99
import styled from 'styled-components';
1010
import { Property } from 'csstype';
11+
import { ref, set } from '@firebase/database';
1112
import { db } from '@/lib/firebase';
1213
import type { TextWidgetProps } from '@/components/TextWidget/types';
1314

@@ -90,7 +91,7 @@ class TextWidgetEditor extends Component<Props, TextWidgetProps> {
9091

9192
save(e: MouseEvent<HTMLButtonElement>) {
9293
e.preventDefault();
93-
db.ref(`/widgets/${this.props.id}/props`).set(this.state);
94+
set(ref(db, `/widgets/${this.props.id}/props`), this.state);
9495
}
9596

9697
render() {

src/components/TimeWidget/editor.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
FormControlLabel,
88
Checkbox,
99
} from '@material-ui/core';
10+
import { ref, set } from '@firebase/database';
1011
import { db } from '@/lib/firebase';
1112
import type { TimeWidgetProps } from './types';
1213

@@ -36,7 +37,7 @@ class TimeWidgetEditor extends Component<Props, TimeWidgetProps> {
3637

3738
save(e: MouseEvent<HTMLButtonElement>) {
3839
e.preventDefault();
39-
db.ref(`/widgets/${this.props.id}/props`).set(this.state);
40+
set(ref(db, `/widgets/${this.props.id}/props`), this.state);
4041
}
4142

4243
render() {

src/components/admin/SignInForm/index.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import React, { VFC, useEffect, useState } from 'react';
1+
import { useEffect, useState } from 'react';
2+
import { useRouter } from 'next/router';
23
import styled from 'styled-components';
4+
import { signInWithEmailAndPassword } from '@firebase/auth';
35
import { TextField, Button } from '@material-ui/core';
46
import { auth } from '@/lib/firebase';
57

@@ -19,14 +21,16 @@ type SignInFormProps = {
1921
redirectTo: string;
2022
};
2123

22-
const SignInForm: VFC<SignInFormProps> = ({ redirectTo }) => {
24+
const SignInForm = ({ redirectTo }: SignInFormProps) => {
25+
const router = useRouter();
2326
const [email, setEmail] = useState("");
2427
const [password, setPassword] = useState("");
2528

2629
const signin = async (e: React.SyntheticEvent) => {
2730
e.preventDefault();
2831
try {
29-
await auth.signInWithEmailAndPassword(email, password);
32+
await signInWithEmailAndPassword(auth, email, password);
33+
router.push(redirectTo);
3034
} catch (err) {
3135
alert(err.message);
3236
}

src/lib/AuthProvider.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { User } from '@firebase/auth-types';
2-
import React, { FC, createContext, useEffect, useState } from 'react';
1+
import { User, onAuthStateChanged } from '@firebase/auth';
2+
import { FC, createContext, useEffect, useState } from 'react';
33
import { auth } from '@/lib/firebase';
44

55
type AuthContextProps = {
@@ -12,7 +12,7 @@ const AuthProvider: FC = ({ children }) => {
1212
const [currentUser, setCurrentUser] = useState<User | null | undefined>(undefined);
1313

1414
useEffect(() => {
15-
auth.onAuthStateChanged((user) => {
15+
onAuthStateChanged(auth, (user) => {
1616
setCurrentUser(user);
1717
});
1818
});

src/lib/firebase.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import 'firebase/auth';
2-
import 'firebase/database';
3-
import firebase from 'firebase/app';
1+
import { getAuth } from 'firebase/auth';
2+
import { getDatabase } from 'firebase/database';
3+
import { initializeApp } from 'firebase/app';
44

55
const config = {
66
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
@@ -12,13 +12,8 @@ const config = {
1212
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
1313
};
1414

15-
let auth;
16-
let db;
17-
if (firebase.apps.length === 0) {
18-
firebase.initializeApp(config);
19-
auth = firebase.app().auth();
20-
db = firebase.database();
21-
}
15+
const app = initializeApp(config);
16+
const auth = getAuth(app);
17+
const db = getDatabase(app);
2218

23-
export default firebase;
24-
export { auth, db };
19+
export { app, auth, db };

src/pages/admin/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
Typography,
1010
Button
1111
} from '@material-ui/core';
12-
import { User } from '@firebase/auth-types';
13-
import { DataSnapshot } from '@firebase/database-types';
12+
import { User } from '@firebase/auth';
13+
import { ref, onValue, DataSnapshot } from '@firebase/database';
1414

1515
import { AuthProvider } from '@/lib/AuthProvider';
1616
import { auth, db } from '@/lib/firebase';
@@ -53,7 +53,8 @@ const Widgets = () => {
5353
const [widgets, setWidgets] = useState<WidgetList>({});
5454

5555
useEffect(() => {
56-
db.ref('/widgets').on('value', (snap: DataSnapshot) => {
56+
const widgetsRef = ref(db, '/widgets');
57+
onValue(widgetsRef, (snap: DataSnapshot) => {
5758
if (snap?.val()) {
5859
setWidgets(snap.val());
5960
}

src/pages/preview/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useState } from 'react';
2-
import { DataSnapshot } from '@firebase/database-types';
2+
import { ref, onValue, DataSnapshot } from '@firebase/database';
33

44
import { db } from '@/lib/firebase';
55
import { TextWidget } from '@/components/TextWidget';
@@ -23,7 +23,8 @@ const PreviewPage = () => {
2323
const [widgets, setWidgets] = useState<WidgetList>({});
2424

2525
useEffect(() => {
26-
db.ref('/widgets').on('value', (snap: DataSnapshot) => {
26+
const widgetsRef = ref(db, '/widgets');
27+
onValue(widgetsRef, (snap: DataSnapshot) => {
2728
if (snap?.val()) {
2829
setWidgets(snap.val());
2930
}

0 commit comments

Comments
 (0)