How can I update a user AFTER they have logged out of my app, then sign back in

时间:2019-01-09 22:05:02

标签: javascript reactjs https google-oauth2

I am currently developing an app that requires login/logout. I am using google-login-react (Google OAuth2) to handle most of the signing in heavy work. From there, I am posting the google access token along with some other stuff (id, email, last login, etc..) to my RESTful API. After the user is authenticated, they are redirected to a dashboard. From there, the user can sign out of the application.

What I am having difficulty with is UPDATING the user and the user object after the user signs back in. Right now, every time the user logs back in, it is posting a new object (and thus a new user) to my API. I'm looking to simply just show the updated LAST LOGIN and to store the existing user in the ID they had already been assigned upon initial log in.

Right now, this is what I have thus far:

PostData.js

export function PostData(type, userData) {
let BaseURL = 'https://app.someApp.com/';

return new Promise((resolve, reject) =>{
    fetch(BaseURL+type, {
        headers:{
            "Accept": "application/json",
            "Content-Type": "application/json"},
            'Access-Control-Allow-Origin':'*',
            'Content-Security-Policy':'upgrade-insecure-requests',
        method: 'POST',
        mode: 'cors',
        body: JSON.stringify(userData)
    })
   .then((response) => response.json())
    .then((res) => {
        resolve(res);
    })
    .catch((error) => {
       reject(error);
    });

    });
    }

Login.js

class Login extends Component {
constructor(props) {
super(props);
   this.state = {
   loginError: false,
   redirect: false
};
   this.signup = this.signup.bind(this);
}
 signup(res, type) {
    let postData;
    var currentTime = new Date();
  if (type === 'google' && res.profileObj.email) {
    postData = {
       email: res.profileObj.email,
       realname: res.profileObj.name,
       googleId: res.profileObj.googleId,
       googleAccessToken: res.Zi.access_token,
       googleImageURL: res.profileObj.imageUrl,
       lastLogin: currentTime
   };
  }


  if (postData) {

  PostData('api/v1/appUsers', postData).then((result) => {
      let responseJson = result;
      localStorage.setItem("userData", JSON.stringify(responseJson));
      this.setState({redirect: true});

   });
   } else {}
 };

  render() {

   if (this.state.redirect || localStorage.getItem('userData')) {
return (<Redirect to={'/'}/>)
   }
   const responseGoogle = (response) => {
       let isSignedIn = true;
        console.log("google console");
       console.log(response);
       console.log(isSignedIn);
       this.signup(response, 'google');
        return isSignedIn;
     }

Home.js

      signOut(e) {
      e.preventDefault()
      if (localStorage.getItem('userData')) {
         localStorage.removeItem('userData');
         this.props.history.push('/login')
         console.log('shit works');
        }
       }

        constructor(props){
           super(props);
            this.state = {
            name:'',
            redirect: false,
        };
      }

      componentDidMount() {
         let data = JSON.parse(localStorage.getItem('userData'));
          console.log(data);
          }

         render() {
          if(!localStorage.getItem('userData') || this.state.redirect){
           return (<Redirect to={'/login'}/>)
      }

**I'm mainly looking for a syntax solution as opposed to the logic solution as I am aware of the logic behind this

0 个答案:

没有答案