Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react-admin 3.0 upgrade #67

Merged
merged 1 commit into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
"name": "react-admin-firebase",
"description": "A firebase data provider for the React Admin framework",
"version": "0.9.0",
"peerDependencies": {
"react": "^16.x",
"react-admin": "^3.x",
"react-dom": "^16.x"
},
"dependencies": {
"firebase": "6.1.x",
"path-browserify": "^1.0.0",
"ra-realtime": "^2.8.x",
"react": "^16.x",
"react-admin": "^2.9.x",
"react-dom": "^16.x",
"rxjs": "^6.5.x"
},
"devDependencies": {
Expand Down
7 changes: 4 additions & 3 deletions src-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
"dependencies": {
"ra-input-rich-text": "^2.3.0",
"react": "^16.5.1",
"react-admin": "^3.0.2",
"react-admin-firebase": "../",
"react-dom": "^16.5.1",
"react-firebaseui": "^4.0.0",
"react-scripts": "1.1.5",
"redux-saga": "^0.16.0",
"source-map-loader": "^0.2.4"
},
"homepage": "https://benwinding.github.io/react-admin-firebase/",
"devDependencies": {
"gh-pages": "^2.0.1",
"concurrently": "4.1.1",
"cpx": "1.5.0"
"cpx": "1.5.0",
"gh-pages": "^2.0.1"
},
"scripts": {
"start": "concurrently \"yarn update-raf\" \"yarn start-dev-server\"",
Expand Down
10 changes: 5 additions & 5 deletions src-demo/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@ import { PostList, PostShow, PostCreate, PostEdit } from "./posts";
import { UserList, UserShow, UserCreate, UserEdit } from "./users";
import { Admin, Resource } from "react-admin";
import {
FirebaseRealTimeSaga,
FirebaseDataProvider,
FirebaseAuthProvider
} from "react-admin-firebase";
import firebase from 'firebase';
import UserIcon from '@material-ui/icons/People';

import { firebaseConfig } from './FIREBASE_CONFIG';
import CustomLoginPage from './CustomLoginPage';

const firebaseApp = firebase.initializeApp(firebaseConfig);

const options = {
logging: true,
rootRef: 'rootrefcollection/QQG2McwjR2Bohi9OwQzP',
// app: firebaseAppInstance
app: firebaseApp,
// watch: ['posts'];
// dontwatch: ['comments'];
}

const authProvider = FirebaseAuthProvider(firebaseConfig, options);
const dataProvider = FirebaseDataProvider(firebaseConfig, options);
const firebaseRealtime = FirebaseRealTimeSaga(dataProvider, options);

class App extends React.Component {
render() {
return (
<Admin
loginPage={CustomLoginPage}
loginPage={CustomLoginPage}
dataProvider={dataProvider}
authProvider={authProvider}
customSagas={[firebaseRealtime]}
>
<Resource
name="posts"
Expand Down
22 changes: 21 additions & 1 deletion src-demo/src/CustomLoginPage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
// LoginPage.js
import React from "react";
import { Login, LoginForm } from "react-admin";
import StyledFirebaseAuth from 'react-firebaseui/StyledFirebaseAuth';
import firebase from 'firebase';

// Configure FirebaseUI.
const uiConfig = {
// Popup signin flow rather than redirect flow.
signInFlow: 'popup',
// Redirect to /signedIn after sign in is successful. Alternatively you can provide a callbacks.signInSuccess function.
signInSuccessUrl: '#/',
// We will display Google and Facebook as auth providers.
signInOptions: [
firebase.auth.GoogleAuthProvider.PROVIDER_ID,
firebase.auth.FacebookAuthProvider.PROVIDER_ID
]
};

const SignInScreen = () => <StyledFirebaseAuth uiConfig={uiConfig} firebaseAuth={firebase.auth()}/>;

const CustomLoginForm = props => (
<div>
Expand All @@ -9,11 +26,14 @@ const CustomLoginForm = props => (
<p>Password: password</p>
</div>
<LoginForm {...props} />
<SignInScreen />
</div>
);

const CustomLoginPage = props => (
<Login loginForm={<CustomLoginForm />} {...props} />
<Login {...props}>
<CustomLoginForm {...props}/>
</Login>
);

export default CustomLoginPage;
3 changes: 1 addition & 2 deletions src-demo/src/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
Edit,
Filter,
DateField,
DisabledInput,
SimpleShowLayout,
SimpleForm,
TextField,
Expand Down Expand Up @@ -109,7 +108,7 @@ export const PostCreate = props => (
export const PostEdit = props => (
<Edit {...props}>
<SimpleForm>
<DisabledInput source="id" />
<TextInput disabled source="id" />
<DateField source="createdate" />
<DateField source="lastupdate" />
<TextInput source="title" />
Expand Down
7 changes: 3 additions & 4 deletions src-demo/src/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
Create,
Edit,
Filter,
DisabledInput,
SimpleShowLayout,
SimpleForm,
TextField,
Expand Down Expand Up @@ -61,9 +60,9 @@ export const UserCreate = (props) => (
export const UserEdit = (props) => (
<Edit {...props}>
<SimpleForm>
<DisabledInput source="id" />
<DisabledInput source="createdate" />
<DisabledInput source="lastupdate" />
<TextInput disabled source="id" />
<TextInput disabled source="createdate" />
<TextInput disabled source="lastupdate" />
<TextInput source="name" />
<TextInput source="age" />
</SimpleForm>
Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { RealtimeSaga } from "./providers/RealtimeSaga";
import { DataProvider } from "./providers/DataProvider";
import { AuthProvider } from "./providers/AuthProvider";
import { RAFirebaseOptions } from "./providers/RAFirebaseOptions";

export {
RealtimeSaga as FirebaseRealTimeSaga,
DataProvider as FirebaseDataProvider,
AuthProvider as FirebaseAuthProvider,
RAFirebaseOptions as RAFirebaseOptions
Expand Down
98 changes: 34 additions & 64 deletions src/providers/AuthProvider.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
// import * as firebase from "firebase";
import { FirebaseAuth } from "@firebase/auth-types";

import {
AUTH_LOGIN,
AUTH_LOGOUT,
AUTH_ERROR,
AUTH_CHECK,
AUTH_GET_PERMISSIONS
} from "react-admin";
import { log, CheckLogging } from "../misc/logger";
import { RAFirebaseOptions } from "./RAFirebaseOptions";
import { FirebaseWrapper } from "./database/firebase/FirebaseWrapper";
Expand All @@ -26,58 +18,50 @@ class AuthClient {
public async HandleAuthLogin(params) {
const { username, password } = params;

try {
const user = await this.auth.signInWithEmailAndPassword(
username,
password
);
log("HandleAuthLogin: user sucessfully logged in", { user });
return user
} catch (e) {
log("HandleAuthLogin: invalid credentials", { params });
throw new Error("Login error: invalid credentials");
if (username && password) {
try {
const user = await this.auth.signInWithEmailAndPassword(
username,
password
);
log("HandleAuthLogin: user sucessfully logged in", { user });
return user
} catch (e) {
log("HandleAuthLogin: invalid credentials", { params });
throw new Error("Login error: invalid credentials");
}
} else {
return this.getUserLogin();
}
}

public async HandleAuthLogout(params) {
await this.auth.signOut();
public HandleAuthLogout() {
return this.auth.signOut();
}

public async HandleAuthError(params) { }
public HandleAuthError(error) {
log("HandleAuthLogin: invalid credentials", { error });
return Promise.reject("Login error: invalid credentials");
}

public async HandleAuthCheck(params) {
try {
const user = await this.getUserLogin();
log("HandleAuthCheck: user is still logged in", { user });
} catch (e) {
log("HandleAuthCheck: ", { e });
throw new Error("Auth check error: " + e);
}
public HandleAuthCheck() {
return this.getUserLogin();
}

public async getUserLogin() {
public getUserLogin() {
return new Promise((resolve, reject) => {
this.auth.onAuthStateChanged((user) => {
if (this.auth.currentUser) return resolve(this.auth.currentUser);
const unsubscribe = this.auth.onAuthStateChanged(user => {
unsubscribe();
if (user) {
resolve(user);
} else {
reject("User not logged in");
reject();
}
});
})
});
}

public async HandleGetCurrent() {
try {
const user = await this.getUserLogin();
log("HandleGetCurrent: current user", { user });
return user;
} catch (e) {
log("HandleGetCurrent: no user is logged in", { e });
return null;
}
}

public async HandleGetPermissions() {
try {
const user = await this.getUserLogin();
Expand All @@ -99,26 +83,12 @@ export function AuthProvider(firebaseConfig: {}, options: RAFirebaseOptions) {
const auth = new AuthClient(firebaseConfig, options);
CheckLogging(firebaseConfig, options);

return async (type: string, params: {}) => {
log("Auth Event: ", { type, params });
{
switch (type) {
case AUTH_LOGIN:
return auth.HandleAuthLogin(params);
case AUTH_LOGOUT:
return auth.HandleAuthLogout(params);
case AUTH_ERROR:
return auth.HandleAuthError(params);
case AUTH_CHECK:
return auth.HandleAuthCheck(params);
case "AUTH_GETCURRENT":
return auth.HandleGetCurrent();
case AUTH_GET_PERMISSIONS:
return auth.HandleGetPermissions();
default:
throw new Error("Unhandled auth type:" + type);
}
}
return {
login: params => auth.HandleAuthLogin(params),
logout: () => auth.HandleAuthLogout(),
checkAuth: () => auth.HandleAuthCheck(),
checkError: error => auth.HandleAuthError(error),
getPermissions: () => auth.HandleGetPermissions(),
};
}

Expand Down
Loading