-
Notifications
You must be signed in to change notification settings - Fork 407
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
Current user enanchement #28
Comments
Hi @alex88 :) |
My current setup is this one
however I think it can be vastly improved |
It looks excellent! I did something similar on my Toggl clone project: export default function configRoutes(store) {
const _ensureAuthenticated = (nextState, replace, callback) => {
const { dispatch } = store;
const { session } = store.getState();
const { currentUser } = session;
if (!currentUser && localStorage.getItem('phoenixAuthToken')) {
dispatch(Actions.currentUser());
} else if (!localStorage.getItem('phoenixAuthToken')) {
replace('/sign_in');
}
callback();
};
return (
<Route component={MainLayout}>
<Route path="/sign_up" component={RegistrationsNewView} />
<Route path="/sign_in" component={SessionsNewView} />
<Route path="/" component={AuthenticatedContainer} onEnter={_ensureAuthenticated}>
<IndexRoute component={HomeIndexView} />
<Route path="/reports" component={ReportsIndexView} />
</Route>
</Route>
);
} The main difference is that I create a parent |
I wanted to have a common container to have the authenticated logic too but I'll implement that later. In my project I had to move the callback to where the |
Yeah! I store the socket in the store using the const initialState = {
currentUser: null,
socket: null,
channel: null,
error: null,
};
export default function reducer(state = initialState, action = {}) {
switch (action.type) {
case Constants.CURRENT_USER:
return { ...state, currentUser: action.currentUser, socket: action.socket, channel: action.channel, error: null };
default:
return state;
}
}
|
But, how can you be 100% that the socker connection end channel joining is done before the route has entered in the authenticated route? If the route component during the didMount tries to use the socket after the user has done a full page reload, is the socket already connected and the channel already joined? |
Yeah, you're right. The only way I found to make it work using the store was like this: https://github.com/bigardone/phoenix-toggl/blob/master/web/static/js/views/reports/index.js#L19 I basically use both |
Oh that's nice, so everything that needs the user object to be populated is started only after the first time the user is actually set by the authentication. I have never seen before that willReceiveProps. This can solve my issue of having a blank screen until the user socket is connected and/or the current user object being set. |
Check this https://github.com/bigardone/phoenix-toggl/blob/master/web/static/js/containers/authenticated.js#L24 If the current user is not set, it returns nothing on the It's kind of tricky, but it works :) |
I think it would be nice to have the UI rendered instead of a blank page even if the user object is not set yet, that way you can show some page, start the loading spinning icon and then when you have the user object fetch the data and then do the requests In any other case, when you just need the user authenticated, you can go straight with the localstorage token which should be enough to do an authenticated request |
@alex88 - Any chance you might be interested in doing a PR with your change? I'd love to check it out as I'm extremely interested in Phenix and Elixir. |
@ACPK thank you for the interest, unfortunately I'm not using react anymore and this project has ben re-done using another framework. Anyway, the code that I was using was exactly this one #28 (comment) If you need help with it just ask! |
@alex88 - What framework was the replacement built with? |
@ACPK angular 2, just because I was more familiar with it and I had to deliver the project in a small timeframe |
Currently the logic to fetch the current user is this:
my idea is that it would be nice to fetch the user data before doing any consequent request or route entering, so that if we're in a non-authenticated page we can still have the header populated with the current user and in case we're in an authenticated page the route is loaded only after the user object is loaded in case the page needs some data inside the user object to do some logic. If for some reason the authentication fails it redirects to sign-in page.
What you think?
The text was updated successfully, but these errors were encountered: