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

Passport Facebook Auth #324

Open
mulikaminker opened this issue Aug 11, 2017 · 0 comments
Open

Passport Facebook Auth #324

mulikaminker opened this issue Aug 11, 2017 · 0 comments

Comments

@mulikaminker
Copy link

mulikaminker commented Aug 11, 2017

I created a login system for the site with Facebook via Passport. I keep the token on req.user and transfer it to the store through redux, and I keep it in
local storage.

I'm stuck in the testing phase whether or not the user is logged in while loading the app for the first time. If it comes from the server, the local storage. is not defined.

Another option is to save the token in req.user or req.session, but I can not do dispatch to action what routes.js file.

routes.js:

`/* eslint-disable global-require */
import React from 'react';
import { Route, IndexRoute } from 'react-router';
import App from './modules/App/App';
import * as Actions from './modules/Auth/AuthActions';
import store, {dispatch} from './store.js';

  const requireLoggedIn = (nextState, replace, cb) => {
    let token = null;

    if (req && req.session.token) {
        token = req.session.token;
    }

    const requireLogin = (nextState, replace, cb) => {
        function checkAuth() {
            const { auth: { isAuthenticated }} = store.getState();
            if (!isAuthenticated) {
                replace('/login');
            }
            cb();
        }

        const { auth: { loaded }} = store.getState();
        if (!loaded) {
            store.dispatch(Actions.checkToken(token)).then(checkAuth);
        } else {
            checkAuth();
        }
    };
  };

export default (
  <Route path="/" component={App}>
    <IndexRoute
      getComponent={(nextState, cb) => {
        require.ensure([], require => {
          cb(null, require('./modules/Post/pages/PostListPage/PostListPage').default);
        });
      }}
    />
    <Route
      path="/login"
      getComponent={(nextState, cb) => {
        require.ensure([], require => {
          cb(null, require('./modules/Auth/components/LoginForm').default);
        });
      }}
    />
    <Route
      path="/test"
      getComponent={(nextState, cb) => {
        require.ensure([], require => {
          cb(null, require('./modules/Auth/components/LoginForm').default);
        });
      }}
    />
    <Route
      path="/posts/:slug-:cuid"
      onEnter={requireLoggedIn}
      getComponent={(nextState, cb) => {
        require.ensure([], require => {
          cb(null, require('./modules/Post/pages/PostDetailPage/PostDetailPage').default);
        });
      }}
    />
  </Route>
);
`

Actions.js:

`export function checkAuth () {
  return (dispatch) => {
    dispatch(requestCheckAuth());
    return fetch(`${baseURL}/auth/checkAuth`, {
      method: 'GET',
      credentials: 'same-origin',
      headers: new Headers({
        'Content-Type': 'application/json',
      }),
    })
    .then((response) => response.json())
    .then((response) => {
      console.log(response)
      const { user, message } = response;
      if (!user.ok) {
        dispatch(loginFailure(message));
        return Promise.reject(message);
      }

      localStorage.setItem('token', user.token);
      dispatch(loginSuccess(user));
    })
    .catch((err) => {
      console.log(err);
    });
  };
}`

server, checkAuth:

`export function checkAuth (req, res, next) {
if (req.user) {
return res.json({
message: '',
user: { ok: true, token: generateToken(req.user), user: req.user}
});
}
else {
return res.status(400).send({ msg: 'not authorization' });
}

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants