Skip to content

Commit 00321c9

Browse files
author
Sung Won Cho
committed
Add EnsureLoggedIn component
1 parent c70e425 commit 00321c9

File tree

6 files changed

+59
-8
lines changed

6 files changed

+59
-8
lines changed

.babelrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": ["es2015"],
2+
"presets": ["es2015", "react"],
33
}

lib/auth_composer.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function authComposer({context}, onData) {
2+
const {Meteor} = context();
3+
4+
onData(null, {
5+
loggedIn: Boolean(Meteor.userId()),
6+
loggingIn: Meteor.loggingIn()
7+
});
8+
}

lib/components/ensure_logged_in.jsx

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react';
2+
3+
const EnsureLoggedIn = ({loggedIn, children, unauthenticatedMessage}) => {
4+
let errorComponent = unauthenticatedMessage || DefaultUnauthenticatedMessage;
5+
6+
return (
7+
<div>
8+
{loggedIn ? children : errorComponent}
9+
</div>
10+
);
11+
};
12+
13+
const DefaultUnauthenticatedMessage = (
14+
<div>
15+
Please log in to view this page.
16+
</div>
17+
);
18+
19+
export default EnsureLoggedIn;

lib/containers/ensure_logged_in.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {useDeps, composeAll, composeWithTracker, compose} from 'mantra-core';
2+
3+
import {authComposer} from '../auth_composer';
4+
import EnsureLoggedin from '../components/ensure_logged_in';
5+
6+
export const composer = ({context}, onData) => {
7+
const {Meteor, Collections} = context();
8+
9+
onData(null, {});
10+
};
11+
12+
export const depsMapper = (context, actions) => ({
13+
context: () => context
14+
});
15+
16+
export default composeAll(
17+
composeWithTracker(authComposer),
18+
composeWithTracker(composer),
19+
useDeps(depsMapper)
20+
)(EnsureLoggedin);

lib/index.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
export function authComposer({context}, onData) {
2-
const {Meteor} = context();
1+
import {authComposer} from './auth_composer';
2+
import EnsureLoggedIn from './containers/ensure_logged_in';
33

4-
onData(null, {
5-
loggedIn: Boolean(Meteor.userId()),
6-
loggingIn: Meteor.loggingIn()
7-
});
8-
}
4+
export {
5+
authComposer,
6+
EnsureLoggedIn
7+
};

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
"babel-cli": "^6.5.1",
2929
"babel-core": "^6.7.4",
3030
"babel-preset-es2015": "^6.5.0",
31+
"babel-preset-react": "^6.5.0",
3132
"chai": "^3.5.0",
3233
"mocha": "^2.4.5"
34+
},
35+
"dependencies": {
36+
"mantra-core": "^1.6.1",
37+
"react": "^15.1.0"
3338
}
3439
}

0 commit comments

Comments
 (0)