Skip to content

Instantly share code, notes, and snippets.

View sakulstra's full-sized avatar
:octocat:
tl;dr;

Lukas sakulstra

:octocat:
tl;dr;
View GitHub Profile
export default function protectedPage(UI) {
return observer(class ProtectedPage extends Component {
componentDidMount() {
this.disposer = autorun(() => {
if (typeof window !== 'undefined'
&& !getAuthStore().authIsPending
&& !getAuthStore().isAuthenticated
) {
Router.push('/access-denied')
@observable user = null
@observable authIsPending = true
constructor () {
this.unwatchAuth = auth.onAuthStateChanged(user => {
this.user = user
this.authIsPending = false
})
}
@action start = () => {
when(
() => this.authStore.user,
() => {
const userId = this.authStore.user.uid;
const ref = db.ref(`/hashes/${userId}`);
ref.on('child_added', (data) => {
this.hashes.set(data.key, data.val());
});
}
@action start = () => {
const disposer = autorun(() => {
if (this.authStore.user) {
// stop autorun
disposer()
// get current userId
const userId = this.authStore.user.uid
// save ref so we cann fire .off() on unmount
this.ref = db.ref(`/hashes/${userId}`)
// get initial values one - so we can distinguish between "no data" and "not loaded"
const HashInput = ({ callback }) => (
<div>
<input type="text" onChange={e => callback(e.target.value)}/>
</div>
);
export default HashInput;
@action start = () => {
const disposer = autorun(() => {
if (this.authStore.user) {
disposer();
const userId = this.authStore.user.uid;
const ref = db.ref(`/hashes/${userId}`);
ref.on('child_added', (data) => {
this.hashes.set(data.key, data.val());
});
}
class Store {
@observable hash = '';
@observable hashes = observable.map([]);
constructor() {
this.authStore = initAuthStore(false);
}
@action start = () => {
const userId = this.authStore.user.uid;
path /hashes/{userid} {
read() {isCurrentUser(userid)}
}
path /hashes/{userid}/{id} is Hash {
read() {isCurrentUser(userid)}
write() {isCurrentUser(userid)}
}
type Hash {
text: String,
path /hashes/{userid} {
read() {isCurrentUser(userid)}
}
path /hashes/{userid}/{id} {
read() {isCurrentUser(userid)}
write() {isCurrentUser(userid)}
}
isCurrentUser(uid) { auth != null && auth.uid == uid }
let store = null;
class Store {
@observable user = null;
constructor() {
auth.onAuthStateChanged(user => {
this.user = user;
});
}